Home > other >  SCP and sshpass - Can't copy from remote source to local destination using script on PIs - debi
SCP and sshpass - Can't copy from remote source to local destination using script on PIs - debi

Time:05-25

I am struggling to copy files from a remote source to my local destination I am using scp and I have tried adding sshpass to send the password

I have a script that copies from my local source to a remote destination which works:

sudo sshpass -p "pi" ssh -o StrictHostKeyChecking=no pi@$VAR_IP ls /some_dir

this just connects to it without having to put in additional commands to accept the connection if it is the first time

sudo sshpass -p "pi" scp /path_to_app/$VAR_APP pi@$VAR_IP:/home/pi/$VAR_APP/

this successfully copies from my local source to my remote destination

Now... Even though the scp documentation says I can scp remote source to local destination I can't seem to get it to work, here is how I am trying to do it in a different script:

sudo sshpass -p "pi" ssh -o StrictHostKeyChecking=no pi@$VAR_IP ls /some_dir

this is just to initialize not to have to accept connection, same as the last script

sudo sshpass -p "pi" scp pi@$VAR_IP:/home/pi/$VAR_APP/logs/file /some_local_dir/

This gives me the error: scp: /home/pi/App_Name/logs/file: No such file or directory

the path doesn't exist on local but does on remote, so it seems it is trying to find it locally instead of remotely, any ideas on this?

I looked at all the related posts about this and the man pages but can't find an answer to my specific case

I cannot do the cert key thing as I have too many sites, it would take forever

I saw in one of the posts someone tried it without sshpass, I tried it too like this:

sudo scp pi:pi@$VAR_IP:/home/pi/$VAR_APP/logs/file /some_local_dir/

This gave me the error: ssh: Could not resolve hostname pi: Name or service not known I don't think it works like that so I didn't go further down that vein

I hope I gave enough info with clarity any help would really be appreciated

thank you so much for your time and input

CodePudding user response:

You mention that this command is not working sudo sshpass -p "pi" scp pi@$VAR_IP:/home/pi/$VAR_APP/logs/file /some_local_dir/

Did you check this? sudo sshpass -p "pi" ssh pi@$VAR_IP 'ls -l /home/pi/$VAR_APP/logs/file /some_local_dir/' to check the directory is exist

If that issue is still there, I recommend you to try pssh and pscp which are parallel ssh that could do the same thing as sshpass

CodePudding user response:

I managed to fix it, for anyone that comes across this problem Here is how I found the fix:

The file I was looking for was a root file but I was sshing as pi.

Even though I sudoed the script, and sudoed sshpass That does not mean scp is sudo, so each command in a line needs its own sudo eg:

sudo sshpass -p "pi" scp pi@IP:/file /local_dir/

This doesn't work because sshpass has sudo but scp does not, however

sudo sshpass -p "pi" sudo scp pi@IP:/file /local_dir/

This works perfectly because scp now has sudo rights

  • Related