读书人

pexpect施用ssh登陆linux并切换到root

发布时间: 2012-12-18 12:43:41 作者: rapoo

pexpect使用ssh登陆linux并切换到root用户
使用pexpect登陆linux系统,并将用户切换到root

#!/usr/bin/env pythonimport pxsshimport 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)



读书人网 >UNIXLINUX

热点推荐