Search code examples
pythonsshscpsshpass

SSHPass Works in Terminal, but not in Python Script - Permission Denied, Please Try Again


I'm trying to get a .jpg file from my Pi Zero and transmitting it to my Pi 3B using SCP with SSHPass. Here is my code:

for x in range(0, count):
        client.exec_command('sudo -E python3 startup.py')
        name = "img"+str(x)
        print(name)
        output = subprocess.check_output("pwd", shell=True)
        output = str(output)[2:(len(str(output))-2)]
        os.system("pwd")
        print(output)
        command=("sshpass -p \'raspberry\' scp raspberrypizero.local:img.jpg "+name+".jpg")
        print(command)
        subprocess.Popen('./go.sh')
        print("Running")

And the code in go.sh:

#!/bin/sh
sshpass -p 'raspberry' scp raspberrypizero.local:img.jpg img0.jpg

Whenever I run the go.sh command in a terminal, at the same directory, it works perfectly fine and the image is successfully transmitted. However, whenever I try to run the command in a python script, I get this output:

sshpass -p 'raspberry' scp raspberrypizero.local:img.jpg img1.jpg
Permission denied, please try again.
Running

I have tried to use os.system as well as subprocess.call, however neither of these work.

So, if anyone could let me know or have an idea of why sshpass / scp is working in the terminal, but not when executing it via a python script, that would be greatly appreciated.

Cheers.


Solution

  • You also can use the following command:

    sudo python3 xxxxx.py
    

    and then:

    command=("sudo sshpass -p \'raspberry\' scp raspberrypizero.local:img.jpg "+name+".jpg")