连接到putty,使用相同的会话通过python登录到第二个linux服务器。

问题描述 投票:0回答:2

有没有一种方法可以 1.打开puttyplink会话并通过python登录到linux服务器 2.在当前会话中通过python登录到另一个linux服务器 3.让putty会话保持打开状态,让用户可以手动运行其他命令?

注意:脚本需要在访问Windows的机器上运行。 - 脚本需要在通过putty访问linux服务器的Windows机器上运行。- 1用subprocess.Popen()很简单。 - 我被下一次登录服务器时发送密码的问题卡住了。RSA ssh密钥在我们的服务器上受到限制。

有什么办法吗?除了python脚本,可能还有其他的选择吗?

python putty
2个回答
1
投票

是的,你可以这样做。使用 pexpect.

但是我必须注意,你不能在没有安装cygwin的情况下在windows上使用pexpect。当你想在没有安装cygwin的情况下直接在windows上运行你的程序时,你需要使用 winexpect (https:/bitbucket.orggeertjwinpexpectwiki首页。).

PexpectWinexpect用法 例子:

#!/usr/bin/env python
import pexpect

ssh_newkey = 'Are you sure you want to continue connecting'
# my ssh command line
p=pexpect.spawn('ssh [email protected] uname -a')

i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==0:
    print "I say yes"
    p.sendline('yes')
    i=p.expect([ssh_newkey,'password:',pexpect.EOF])
if i==1:
    print "I give password",
    p.sendline("mypassword")
    p.expect(pexpect.EOF)
elif i==2:
    print "I either got key or connection timeout"
    pass
print p.before # print out the result

在你的情况下,你必须使用 plink 而不是 sshwinexpect 而不是 pexpect.

© www.soinside.com 2019 - 2024. All rights reserved.