Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCP via Python Subprocess Popen: "Identity File not accessible"

When executing SCP from subprocess.popen I recieve the error: Warning: Identity File ./id_rsa.key not accessible: no such file or directory. The problem is explained below.

I want to SCP a file from a local machine to a remote machine, using a python script, and I want to specify the key to use when executing SCP (using -i option).

My local machine is Windows 10 Enterprise. My 'remote' machine is currently a Docker container running Alpine linux on my localhost.

When I run the command in an elevated shell, it works:

scp -i ./id_rsa.key -P 2222 ./test.plan [email protected]:/go/

When I execute the command via Python 3.6 subprocess.popen:

SSH = subprocess.Popen(['scp', '-i ./id_rsa.key', '-P 2222', './test.plan', '[email protected]:/go/'],
    cwd = r'C:\ThePathToThisStuff',
    shell = False,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

It prompts me for a password, [email protected]'s password:, and when I enter the password it throws the error below, indicating that it fails to find the RSA key:

<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'> ERROR: [b'Warning: Identity file  ./id_rsa.key not accessible: No such file or directory.\n']

I've tried providing absolute paths to the keys, seperating each command in the arguments list into options and values, different groupings, etc.

like image 731
Mr.Budris Avatar asked Jan 29 '26 00:01

Mr.Budris


1 Answers

You should not combine an option and its argument in the same parameter, they need to be separate list elements.

SSH = subprocess.Popen(['scp', '-i', './id_rsa.key', '-P', '2222', './test.plan', '[email protected]:/go/'],
like image 85
Barmar Avatar answered Jan 30 '26 14:01

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!