SCP script with Python

Feb 13, 2017 11 Replies

On 2017 Feb 13 04:04:46, you wrote to All:

JL> I have an SCP script that auto completes just fine from the terminal JL> window in Pi. When I use either subprocess or os to try and have it JL> run under Python it does not do the file transfer from Pi to my Ubuntu JL> machine. What am I doing wrong? Here is the script:

JL> from subprocess import call JL> cmd='scp -i id_rsa sample.txt jack@192.168.1.103 /home/jack/pi_data' JL> call (cmd.split ())

how are you feeding it the password? the password is being requested, isn't it?

)\/(ark

Always Mount a Scratch Monkey Do you manage your own servers? If you are not running an IDS/IPS yer doin' it wrong... ... Well! I'm impressed.

I have an SCP script that auto completes just fine from the terminal window in Pi. When I use either subprocess or os to try and have it run under Py thon it does not do the file transfer from Pi to my Ubuntu machine. What a m I doing wrong? Here is the script:



from subprocess import call cmd='scp -i id_rsa sample.txt jack@192.168.1.103 /home/jack/pi_data' call (cmd.split ())



Thank you


Wrong scp syntax, should be jack@192.168.1.103:/home/jack/pi_data

http://www.greenend.org.uk/rjk/

Sorry, mistyped, the colon was in there, and it does work ok under the terminal window.

Sorry, I mistyped the script. I did use the colon, and it works ok under the terminal window.

Can you be more specific about the environment - are you running the Python program from a terminal window, or in some other way (e.g. cron job)? What authentication is scp using?

OK, please copy+paste the actual script into a posting, plus a transcript showing it running. It?s not practical to debug something without being able to see it.

http://www.greenend.org.uk/rjk/

My guess is it is not finding the id_rsa file because the script isn't picking up the current working directory. Try with the full path to the file. (and the full path to scp too maybe)

Theo

I went through the key-gen process to generate a private and public key. That is what the "id_rsa" is part of the command is looking at, so yes, it does auto complete without manual password entry.

I'm guessing, but since it works from the command line but not from a non-interactive mode, you might try this: scp -i ~/.ssh/id_rsa

Consulting Minister for Consultants, DNRC I can please only one person per day. Today is not your day. Tomorrow isn't looking good, either. I am BOFH. Resistance is futile. Your network will be assimilated.

Yes, that's what I thought: use the complete path to the id file (maybe even don't use "~", to avoid all potential ambiguity).

JL> from subprocess import call JL> cmd='scp -i id_rsa sample.txt jack@192.168.1.103 /home/jack/pi_data' JL> call (cmd.split ())

are you using ssh keys for authentication?

I've got some python working to transfer data. here is a command. use the subprocess.

subprocess.call("scp -q %s user@host:/path/file" %(inputfile, s), shell = True)

you will need to make sure you have keys on the target system basically the host.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required