简体   繁体   中英

How can i send password using subprocess while logging in to unix box using ssh from windows using python

I am trying to connect to unix box using subprocess module of python from windows machine.Please Note : Strictly want to use subprocess module(plz dont suggest pexpect,paramiko as i cant run them from windows to connect to unix as per my knowledge also have environment restricitons) Here is my code that i have tried and i always get interactive keyboard authentication message with password prompt: i try to send password to that prompt but unable to do so.

import subprocess
cmd='plink -ssh username@hostname -pw password'
sp=subprocess.Popen(cmd,stdin=subprocess.pipe,stdout=subprocess.pipe,shell=False)
sp.stdin.write('password \n')
sp.stdin.flush()
error,out=sp.communicate
print error
print out

Using -pw in command doesnt work (have used one at a time- either -pw option or stdin.write), also have tried to stdin.write to console but couldn't do that also. I have some limitations on my execution environment too, also pexpect require termios which isnt present in windows

This works fine on my Windows box to connect to a FreeBSD one:

>>> import subprocess
>>> cmd = r'c:\path\to\plink -ssh user@machine -pw pass'
>>> p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
             stderr=subprocess.PIPE)
>>> p.stdin.write(b'ls -al\nexit\n')
12
>>> p.stdin.flush()
>>> err = p.stderr.read()
>>> out = p.stdout.read()
>>> print(err)
b'Using username "user".\r\n'
>>> print(out.decode())
Last login: Mon Nov 20 17:16:18 2017 from xxx
... remaining of login message
$ ls -al
total 240
... listing of home directory
$ exit

user , pass and machine should be set per your own Linux or Unix box...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM