Establish ssh session by giving password using scr

2019-08-06 21:25发布

问题:

I have written a below script to do ssh and providing password .But it still waits for password prompt while running . Searched through many such questions on stack overflow, Everyone suggesting to use paramiko or pexpect but I can not use this as this script has to be run by many people on different machines and can not ask everyone to install these libraries first

Looking for a solution without using such libraries

import subprocess
import sys 

HOST="lab@xxxx"
COMMAND="pwd"

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=False, stdout=subprocess.PIPE, stdin = subprocess.PIPE, stderr=subprocess.PIPE)

ssh.stdin.write('lab\n')
ssh.stdin.flush()
result = ssh.stdout.readlines()
if result == []: 
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    print result

~

标签: python ssh