`
flyeagle
  • 浏览: 211876 次
  • 来自: ...
社区版块
存档分类
最新评论

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)



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics