[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to use ssh-agent in windows in python?

? 2019-05-27 11:19, Cameron Simpson ??:

> The output of "ssh-agent -s" is Bourne shell variable assignment syntax. 
> You need to parse that and then install those values in so.environ 
> before calling the second command. The subprocess module has methods for 
> collecting the output of a command (see the communicate method).

   I want to rewrite the shell script to python script so I have had 
changed the "ssh-agent -s" to 'ssh-agent cmd' in python script for the 
consistence on windows.
   There are two inputs in the two commands. I tried code use 
communicate method and write method and got errors:
Cannot send input after starting communication
ValueError: write to closed file
   I found the communicate method just run one times.

The manual input process is as follows:
1. run cmd.exe
2. input 'ssh-agent cmd' press return, the window display a new prompt.
3. input 'ssh-add id_rsa' and press return, it will ask the id_rsa 
password. After input correctly password, it will display 'Identity 
added: id_rsa (id_rsa)' and a new prompt.

import subprocess

p = subprocess.Popen('cmd', stdin = subprocess.PIPE, stdout = 
subprocess.PIPE, stderr = subprocess.PIPE, shell = True, 
universal_newlines = True)
outinfo, errinfo = p.communicate('ssh-agent cmd\n')
#p.stdin.write('ssh-add id_rsa\n')
print('outinfo is \n %s' % stdoutinfo)
print('errinfo is \n %s' % stderrinfo)

   After a lot of searching and learning I still do not know how to code 
it, I realy need help.

Thanks a lot.