python - Paramiko - ssh 到控制台服务器,必须按回车键才能继续执行脚本

标签 python paramiko

仅使用我的第一个 paramiko 脚本,我们就有了一个 opengear 控制台服务器,所以我试图自动设置我们插入的任何设备。

open gear 监听端口上的 ssh 连接,例如端口 1 中的设备将是 3001。我连接到端口 8 上的设备,它工作并且我的脚本运行,但出于某种原因,在我得到“已建立交互式 SSH session ”消息,我需要在 session 上按回车键以使其运行(所以我有一个 ssh session ,脚本也有,它是共享的)。

它只是在那里等待,直到我点击返回,我已经尝试发送返回,如您所见,但它们不起作用,只有手动返回才有效,这很奇怪,因为从技术上讲它们是同一件事?

import paramiko
import time

def disable_paging(remote_conn):
    '''Disable paging on a Cisco router'''
    remote_conn.send("terminal length 0\n")
    time.sleep(1)
    # Clear the buffer on the screen
    output = remote_conn.recv(1000)
    return output

if __name__ == '__main__':
    # VARIABLES THAT NEED CHANGED
    ip = '192.168.1.10'
    username = 'root'
    password = 'XXXXXX'
    port = 3008

    # Create instance of SSHClient object
    remote_conn_pre = paramiko.SSHClient()

    # Automatically add untrusted hosts (make sure okay for security policy in your environment)
    remote_conn_pre.set_missing_host_key_policy(
         paramiko.AutoAddPolicy())

    # initiate SSH connection
    remote_conn_pre.connect(ip, username=username, password=password,port=port, look_for_keys=False, allow_agent=False)
    print "SSH connection established to %s" % ip

    # Use invoke_shell to establish an 'interactive session'
    remote_conn = remote_conn_pre.invoke_shell()
    print "Interactive SSH session established"
    time.sleep(1)
    remote_conn.send("\n")

    # Strip the initial router prompt
    #output = remote_conn.recv(1000)

    # See what we have
    #print output

    # Turn off paging
    #disable_paging(remote_conn)

    # clear any config sessions
    is_global = remote_conn.recv(1024)
    if ")#" in is_global:
        remote_conn.send("end\n")
        time.sleep(2)
    # if not in enable mode go to enable mode
    is_enable = remote_conn.recv(1024)
    if ">" in is_enable:
        remote_conn.send("enable\n")
        time.sleep(1)
    remote_conn.send("conf t\n")
    remote_conn.send("int g0/0/1\n")
    remote_conn.send("ip address 192.168.1.21 255.255.255.0\n")
    remote_conn.send("no shut\n")
    remote_conn.send("end\n")
    # Wait for the command to complete
    time.sleep(2)
    remote_conn.send("ping 192.168.1.1\n")
    time.sleep(1)

    output = remote_conn.recv(5000)
    print output

最佳答案

我试过这个,看到了那个

is_global = remote_conn.recv(1024)

挂起, 您确定“192.168.1.10”发送了要接收的内容吗? 尝试设置超时

remote_conn.settimeout(3) 

例如3秒,在这一行之后做:

remote_conn = remote_conn_pre.invoke_shell()

这样 recv func 不会挂起并在超时到期时继续

对我有用

关于python - Paramiko - ssh 到控制台服务器,必须按回车键才能继续执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36016046/

相关文章:

python - Paramiko SSH 连接错误 : socket. gaierror : [Errno 8] nodename nor servname provided, or not known

python - Pyreverse 没有检测到父类是 Composite...为什么?

python - Flask with Jinja 在第一页之后出现分页问题

python - Paramiko 和伪 tty 分配

python - 如何使用 Paramiko 从 SFTP 服务器下载最新文件?

python - Paramiko scp 从远程机器正则表达式复制

python - chromedriver 在 html 中选择带有 h1 和 p 部分的元素

python - 解包函数返回到 pandas 数据框列

python - 源 toastr 启动失败,错误启动 django 尽管它存在

python - Paramiko 远程文件中的解码错误