使用python可以用pexpect来调用ssh进行登录

pexpect只能在Linux下才可用,

以下是一个事例:

#!/usr/bin/env python
import pexpect

ssh_newkey = ‘Are you sure you want to continue connecting’
# my ssh command line
p=pexpect.spawn(‘ssh [email protected] apt-get update’)

i=p.expect([ssh_newkey,’password:’,pexpect.EOF])
if i==0:
print “I say yes”
p.sendline(‘yes’)
i=p.expect([ssh_newkey,’password:’,pexpect.EOF])
if i==1:
print “I give password”,
p.sendline(“xuemei19851014”)
p.expect(pexpect.EOF)
elif i==2:
print “I either got key or connection timeout”
pass
p.sendline=(‘ls -a’)
print p.before # print out the result

类后面的命令是不可少的,少了以后会报错

会用到的命令有:tar zxvf    python setup.py install