0

I have two Raspberry Pi's. I am trying to transfer files from one Pi to the other using scp. I am trying to do this through Python because the program that will be transferring files is a python file.

below is the shell script I have for the SCP part (Blurred out the pass and IP):

#!/bin/sh
sshpass -p ######## scp test.txt pi@IP:/home/pi

and below is the Python Script that launches that Shell script.

import subprocess
subprocess.call(['./ssh.sh'])
print("DONE")

For some reason the python script doesnt kick back any errors and hits the print line but the file is not transferred. When i run the scp command outside of python the file transfers just fine. Am I doing something incorrect here?

****EDIT****
I cant even get Subprocess to work with this which is why i ended up using na shell script. Here is my attempt with Subprocess:

import subprocess
subprocess.call("sshpass -p ######## scp test.txt pi@IP:/home/pi")
print"DONE"

Again I get no errors, but the file is not transferred


****EDIT #2****
So I found out that because sshpass is being used, scp isnt prompting me to add the IP to known hosts, as a result the file simply isnt trnasferred at all. I need a way to add this acceptance into the script IE I ge the following if I launch the command without sshpass:

The authenticity of host 'IP (IP)' can't be established. ECDSA key fingerprint is 13:91:24:8e:6f:21:98:1f:5b:3a:c8:42:7a:88:e9:91. Are you sure you want to continue connecting (yes/no)?


I want to communicate to pass "yes\n" to this prompt as well as the password afterwards. Is this possible?

bladexeon
  • 696
  • 3
  • 10
  • 31
  • Capture the return value like this `res = subprocess.call("...")` and see what it says there. Or use `check_call`, which raises exceptions to see what happened. – ipinak Jul 19 '16 at 20:38
  • @ipinak I've updated the question with my findings – bladexeon Jul 19 '16 at 20:51

3 Answers3

0

For the first query

You can use 'subprocess.popen' to get output(STDOUT) and error(STDERR) for the executed command.

import subprocess
cmd = 'sshpass -p ****** scp dinesh.txt root@256.219.210.135:/root'
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print "Output is ",out
print "Error is ",err

If you execute above code with wrong password, the you will get below output:

[root@centos /]# python code.py
Output is
Error is  Permission denied, please try again.

In this case, if the file is successfully transferred, then there is no output. If you execute command like 'ls -l' then output will be printed.

For your second query (****EDIT #2****)

Options are :

  1. Password less SSH. Check this.
  2. Pexpect
Community
  • 1
  • 1
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37
0

I found a much easier way of tackling all of this

sshpass -p ###### scp -o StrictHostKeyChecking=no test.txt  pi@IP:/home/pi

The -o switch allows me to auto store the IP into known hosts thus I do not need to communicate with the shell at all. The interaction from Python to Shell works with that addition; Doing this solely through subprocess also works.

bladexeon
  • 696
  • 3
  • 10
  • 31
0

If you don't mind to try other approaches it worth to use SCPClient from scp import.

pm1359
  • 622
  • 1
  • 10
  • 31