Python:Paramiko模块在尝试嵌套ssh连接时挂起

时间:2015-01-31 05:27:15

标签: python linux networking ssh paramiko

我不想让它太长,因为阅读会很乏味,所以如果你不想阅读它我基本上想知道如何运行嵌套的ssh并将输出接收到变量。注意:我的脚本示例都是实验性的,我对它们做了很多改动,以至于它们可能完全错误。

编写python脚本以完成位于overthewire.com的Bandit游戏时遇到问题。

我已经成功完成了前12个关卡但是在13级游戏要求你在当前关卡中使用私钥和ssh。这意味着我需要在paramiko模块中使用嵌套的ssh。我一直在大多数其他级别使用exec_command,但是在输入任何与网络有关的命令时(例如Netcat,telnet,ssh,openssl connect等)当使用普通的exec_command时脚本挂起

我做了一些研究并发现了ssh.invoke_shell命令,该命令启动了一个不可见的shell。这种方式似乎工作,但我需要获取输入,这将返回我下一级别的密码。抓取输入时我不确定如何接收全部输入,因为它只显示一定量的字节(channel.recv(9999))并且非常不一致。

如何在paramiko中嵌套SSH。我很困惑要循环什么。此方法使用ssh.invoke_shell()

import paramiko

def connect(server,user,pwd):
try:
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    ssh.connect(server,username=user,password=pwd)
except paramiko.AuthenticationException:
    print "Invalid password!"
    sys.exit()    
except:
    print "Please check your internet connection!"
    sys.exit()

if re.search('^bandit[0-9]$|^bandit1[0-9]$|^bandit2[0-5]', UserInput):

   if UserInput == 'bandit13':
       pwd = '8ZjyCRiBWFYkneahHwxCv3wb2a1ORpYL'
       connect(server,user,pwd)
       chan = ssh.invoke_shell()
       data = ' '
       while True:
          if chan.recv_ready():
             resp = chan.recv(9999)
             data += resp

          else:
               continue

          if data.endswith("bandit13@melinda~$:"):
             chan.send("ssh -o 'StrictHostKeyChecking=no' -i sshkey.private bandit14@localhost\n") 
          if data.endswith("bandit14@melinda~$:"): 
             channel.send("cat /etc/bandit_pass/bandit14\n")
             data += chan.recv(9999)
             break
          else:
             break
       print data
    ssh.close()

我将运行的其他代码只是尝试接收所有输出将是非常不一致的例如:

   chan.send('''ssh -o 'StrictHostKeyChecking=no' -i sshkey.private bandit14@localhost''')
    data = ' '
    for c in range(1,50):
        if chan.recv_ready:
            resp = chan.recv(9999)
            data += resp
    print data
    ssh.close()

输出:

Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.18.1-x86_64-linode50 x86_64)

 * Documentation:  https://help.ubuntu.com/

Welcome to the OverTheWire games machine !

Please read /README.txt for more information on how to play the levels
on this gameserver.

0 packages can be updated.
0 updates are security updates.

*** System restart required ***

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

@lodit13@melinda:~$ ssh -o 'StrictHostKeyChecking=no' -i sshkey.private bandit14

这基本上告诉我,我不打印完整输出。这是非常不一致的,因为有时脚本会挂起。

0 个答案:

没有答案