日期:2014-05-16  浏览次数:20747 次

pexpect使用ssh登陆linux并切换到root用户
使用pexpect登陆linux系统,并将用户切换到root
#!/usr/bin/env python
import pxssh
import getpass  
try: 
    ssh = pxssh.pxssh() 
    hostname = IP
    username = UNAME  
    password = PWD  
    ssh.login (hostname, username, password)
    ssh.sendline ('uptime')  # run a command  
    ssh.prompt()             # match the prompt  
    print ssh.before         # print everything before the propt.  
    ssh.sendline ('su - root') #切换到root
    ssh.expect(['Password:'])
    ssh.sendline('****')#root密码
    ssh.prompt()
    print ssh.before
    ssh.sendline('df -h')
    ssh.prompt()
    print ssh.before
    ssh.logout() 
except pxssh.ExceptionPxssh,e:  
    print "pxssh failed on login." 
    print str(e)