Unable to ssh and list files in local directory from remote.


#!/bin/bash
script_work_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_work_area"
script_config_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_config"
dpi_sourceServerList=$script_config_dir"/dpi_sourceServerList"
dpi_srvr_42="rtm@1.1.1.1"
trnsnt_landingFilesDir="/DPICDR/sadiq_rtm/PORT22"
dpi_42_sourceFilesDir="/var/source/logging/target/d2/logging/target"
dpi42_landing_files=$trnsnt_landingFilesDir"/dpi42_landing_files"
dpi_42_fixed_currentFileList=$script_work_dir"/dpi_42_fixed_currentFileList"
dpi_42_fixed_toBePulledFileList=$script_work_dir"/dpi_42_fixed_toBePulledFileList"
dpi_42_fixed_pulledFileList=$script_work_dir"/dpi_42_fixed_pulledFileList"
logger=$script_work_dir/connection.out


get_dpi42_currentFiles()
{    
truncate --size 0 $dpi_42_fixed_currentFileList
##ssh $dpi_srvr_42 -p 22 ls $dpi_42_sourceFilesDir/fixed | grep '\.tgz$'" > $dpi_42_fixed_currentFileList 2>/dev/null
    ssh -o "BatchMode=yes" -p 22 $dpi_srvr_42 
    status=$?
    if [ $status ==0 ];
    then
    ls $dpi_42_sourceFilesDir/fixed | grep "\.tgz$" > $dpi_42_fixed_currentFileList 2>/dev/null;
    else
    echo "Connection refused $dpi_srvr_42" >> $logger
    cp  $dpi_42_fixed_pulledFileList $trnsnt_dpi42_fixed_currentList
    break;
    fi 
}

pulling_dpi42Files()
{
while true
do
    get_dpi42_currentFiles
done
}
pulling_dpi42Files


------ Post updated at 09:22 AM ------

Error :

pwd
/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22
ls: cannot access /var/source/logging/target/d2/logging/target/fixed: No such file or directory

ls is working in local directory and not doing at remote server.
Kindly help how can i fix this.

Requirement is if connection work then ls the file into a current list and if connection failed then cp from old pulled file to current list

------ Post updated at 09:53 AM ------

[CODE]

ls: cannot access /var/source/logging/target/d2/logging/target/fixed: No such file or directory

To make a wild guess, there's no such file or directory.

Can you login manually and confirm the existence of such a directory?

1 Like

Yes directory is there.

but ls is trying to run on local path after ssh so its unable to find the path.

My requirement is ssh connection established
if true then list files of /var/source/logging/target/d2/logging/target/fixed and copied to $dpi_42_fixed_currentFileList (local directory )
if connection failed
then
copy $dpi_42_fixed_pulledFileList $trnsnt_dpi42_fixed_currentList

Kindly guide how can i achieve that.

I would strongly suggest running your script with tracing enabled. In other words, add the line:

set -xv

immediately after the first line of your script:

#!/bin/bash

Your inconsistent variable naming convention (sometimes using dpi_42 , sometimes using dpi42 ), the fact that you define some variables you never use, and the fact that you misspell "destination" with three "i"s instead of an "e" and two "i"s makes it likely that you might have used a variable that hasn't been defined when you intended to use a variable that had been defined with a similar, but slightly different, spelling.

One might also consider adding:

set -u

to cause your script to get an error whenever it tries to expand a variable that hasn't been set (instead of silently expanding to an empty string).

1 Like
#!/bin/bash
script_work_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_work_area"
script_config_dir="/home/websys/TEST_dpi_42_file_trnsfer_engine/PORT22/script_config"
dpi_srvr_42="rtm@1.1.1.1"
trnsnt_landingFilesDir="/DPICDR/sadiq_rtm/PORT22"
dpi_42_sourceFilesDir="/var/source/logging/target/d2/logging/target"
dpi42_landing_files=$trnsnt_landingFilesDir"/dpi42_landing_files"
dpi_42_fixed_currentFileList=$script_work_dir"/dpi_42_fixed_currentFileList"
dpi_42_fixed_pulledFileList=$script_work_dir"/dpi_42_fixed_pulledFileList"
logger=$script_work_dir/connection.out

#-----------------------------------------------------------------------------------------------------
mkdir -p $script_work_dir

if [ ! -e $dpi_42_fixed_currentFileList ] ; then
    touch $dpi_42_fixed_currentFileList
fi

if [ ! -e $dpi_42_fixed_pulledFileList ] ; then
    touch $dpi_42_fixed_pulledFileList
fi
#--------------------------------------------------------------------------------------------------------
get_dpi42_currentFiles()
{    
    
    truncate --size 0 $dpi_42_fixed_currentFileList
  ##ssh $dpi_srvr_42 -p 22 "ls $dpi_42_sourceFilesDir/fixed" > $dpi_42_fixed_currentFileList 2>/dev/null
  ##ssh $dpi_srvr_42 -p 22 ls $dpi_42_sourceFilesDir/fixed | grep '\.tgz$'" > $dpi_42_fixed_currentFileList 2>/dev/null
    ssh -n -o "BatchMode=yes" -p 2233 $dpi_srvr_42 "echo 2>&1" 
    if [ $? -eq 0 ];
    then 
    ls $dpi_42_sourceFilesDir/fixed | grep '\.tgz$'  > $dpi_42_fixed_currentFileList 2>/dev/null;
    else
    echo "Connection refused $dpi_srvr_42" >> $logger
    cp  $dpi_42_fixed_pulledFileList $dpi_42_fixed_currentFileList
    break;
    fi 
}
get_dpi42_currentFiles

------ Post updated at 11:11 AM ------

I had not posted the full code, so kindly ignored the unused variables.

Are you aware that your ssh login does an echo 2>&1 and immediately quits? The entire if ... fi branch is executed on the local node.

1 Like

In post#1 you had an attempt to run a remote command and redirect its output to a local file:

ssh -nqx $dpi_srvr_42 -p 22 "
  ls $dpi_42_sourceFilesDir/fixed | grep '\.tgz$'
" > $dpi_42_fixed_currentFileList

The following does the opposite, it runs a local command and redirect its output to a remote file:

ls $dpi_42_sourceFilesDir/fixed | grep '\.tgz$' | ssh -qx $dpi_srvr_42 "
  cat > $dpi_42_fixed_currentFileList
"

This one needs a command on the remote site, in this case cat that does nothing than copying from stdin to stdout. The stdin is what is piped to ssh.

1 Like

Thank you very much, but i need to test the connection and then

cat 

else copied .
so how can i achieve that.

truncate --size 0 $dpi_42_fixed_currentFileList

i need to perform at every run its important as per the requirement.

------ Post updated at 05:29 PM ------

kindly guide RudiC

------ Post updated at 05:33 PM ------

Thanks @MadeInGermany you almost solved my concern, but i need to validate the connection.
if connection happen successfully then only ls or cat should run else copy the file from mentioned file to there.
kindly guide.

If you want to write to a local file from an SSH session, you would need to pass that to STDOUT within the SSH and then redirect the output of the whole SSH to a file on your local machine, something like this:-

ssh user@server "ls /path/to/files" > /tmp/file_list

If you have multiple statements to execute, you would need to do something more like this:-

ssh user@server "ls /path/to/files
date
uname -a" > /tmp/file_list

Given that you have this in a function, you could also remove any redirection form the function and then call it capturing all the output from the function call:-

ssh user@server "ls /path/to/files" > /tmp/file_list

Do any of these options help? If you really need a good chunk of remote processing to return some sort of filtered output and there may be other output you don't want, then you might have to write a file on the remote server and then copy it back to your local machine in a separate step to interrogate it. If you have SSH keys all in place, it shouldn't be too hard.

I hope that these help.
Let me know if I've completely missed the point.
Robin