如何使用pexpect登录远程主机系统中的超级用户(root)?

时间:2013-02-15 13:28:38

标签: pexpect

如何使用pexpect登录远程主机系统中的超级用户(root)?

user = root user
password = 'pass'
child = pexpect.spawn('ssh %s@%s'%(user,host,))

2 个答案:

答案 0 :(得分:1)

你也可以像普通人一样在ssh上登录用户 然后像往常一样在终端中发送命令登录到root。

#log into user account
child = pexpect.spawn('ssh clientuser@localhost')
child.expect('Password:')
child.sendline('password')
#then log into root account
child.sendline('su')
child.expect('Password:')
child.sendline('sudopassword1234')

这只是更长和更多的代码,并且可能只适用于我编写它的方式。但是你可以使用这个,如果直接ssh到root也不行。

答案 1 :(得分:0)

我会得到答案

child = pexpext.spawn('ssh root@host')