python 脚本实现ssh登录--expect 和 pxssh
expect#!/usr/bin/env pythonimport pexpecthost="x.x.x.x"user="user"password="pwd"command="df -h"child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))child.expect('password:')...
·
expect
#!/usr/bin/env python
import pexpect
host="x.x.x.x"
user="user"
password="pwd"
command="df -h"
child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command))
child.expect('password:')
child.sendline(password)
child.expect(pexpect.EOF)
print child.before
pxssh
#!/usr/bin/env python
import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = raw_input('hostname: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('uptime') # run a command
s.prompt() # match the prompt
print s.before # print everything before the propt.
s.sendline ('ls -l')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)
转载于:https://blog.51cto.com/yayang/1407572
更多推荐
所有评论(0)