1

我正在尝试开发一个 python 脚本,它使用 netmiko(ConnectHandler())通过另一个路由器连接到 cisco 路由器。SSH 到 R1,然后从 R1 SSH 到 R2。

我建立了到 R1 的第一个连接,我使用 Netmiko ssh 到路由器 R2 但我无法建立 cnx。 这是字典的创建

def sh_route(self,alias):
    global verif
    self.device = self._cache.switch(alias)
    device_info = {
                'device_type': 'cisco_ios',
                'ip': self.device.hostname,
                'username': self.device.username,
                'password': self.device.password,}

使用 ConnectHandler 和创建的字典连接到 R1

#connect to device1 from linux
    net_connect = ConnectHandler(**device_info)
    ldp= self.get_ldp_neighbors(alias)
    for  x in ldp.keys():
        addr_list = ldp[x]['addresses'] #List of ldp ip adresses  
        
        src_ip_addr= ldp[x]['SrcIPaddr'] #source ip addr

写 ssh 'src_ip_addr using write_channel() function' (src_ip_addr 是我要 ssh 的地址)

            net_connect.write_channel('ssh'+src_ip_addr+'\n')
        time.sleep(1)
        output = net_connect.read_channel()

对上面的用户名和密码使用循环处理 密码好像没有写,所以cnx失败了。

        i = 1
        while i <= 20:
            try:   
                if 'Username' in output:
                    net_connect.write_channel(net_connect.username + '\r\n')
                    time.sleep(1)
                    output = net_connect.read_channel()
                if 'password' in output:
                    net_connect.write_channel(net_connect.password + '\n')
                    time.sleep(1)
                    output = net_connect.read_channel()
                i += 1
            except EOFError:
                pass
            print(output=

验证 cnx 是否已建立

print ("SSH prompt: {}".format(net_connect.find_prompt()))

然后使用调度功能并将命令发送到目标路由器 R2

redispatch(net_connect, device_type='cisco_ios')
    net_connect.enable()
    new_output = net_connect.send_command('show version', use_textfsm=True)

然后我尝试运行命令 sh ip route connected 和 sh route local

        version= new_output[0]['rommon']
    if  version == 'IOS-XE':
        new_output = net_connect.send_command('show ip route connected')
        return new_output
    if  version == 'IOS-XR':
        new_output = net_connect.send_command('show route local')
        return new_output

问题出在用户名和密码处理部分。

这是终端上的输出

ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
ssh 192.168.247.2
Password: 
SSH prompt: Password:

这是完整的日志:

这是日志

userauth 是 OK INFO:paramiko.transport:Authentication (password) 成功!DEBUG:paramiko.transport:[chan 0] 最大数据包输入:32768 字节 DEBUG:paramiko.transport:[chan 0] 最大数据包输出:4096 字节 DEBUG:paramiko.transport:Secsh 通道 0 已打开。DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok DEBUG:netmiko:write_channel: b'terminal width 511\n' DEBUG:netmiko:Pattern is : 终端宽度 511 DEBUG:netmiko:_read_channel_expect read_data: CCCCCCCC

抄送


| | | BNET-A101 在布拉索夫 vLAB | | | | 这是一个由 ORANGE BUSINESS SERVICES 管理的私人系统 | | | | 访问此设备需要针对 | 的身份验证 | | | >> 实验室基础设施 TACACS 服务 << | | | | 你的“土星” 此处应使用登录名 | | | | | |_______________________________________________________________________________|

调试:netmiko:_read_channel_expect read_data:CCCCCCCC

CCCCCCCCCC CC C ________________________________________________________________ | | | 记录并可能监控此设备上的活动 | |________________________________________________________________|

Bnet-A101#端子宽度5

调试:netmiko:_read_channel_expect read_data:11

调试:netmiko:找到的模式:终端宽度 511 CCCCCCCC

抄送


| | | BNET-A101 在布拉索夫 vLAB | | | | 这是一个由 ORANGE BUSINESS SERVICES 管理的私人系统 | | | | 访问此设备需要针对 | 的身份验证 | | | >> 实验室基础设施 TACACS 服务 << | | | | 你的“土星” 此处应使用登录名 | | | | | |_______________________________________________________________________________| CCCCCCCC CCCCCCCCCC CC C ________________________________________________________________ | | | 记录并可能监控此设备上的活动 | |________________________________________________________________| | |_______________________________________________________________________________| CCCCCCCC CCCCCCCCCC CC C ________________________________________________________________ | | | 记录并可能监控此设备上的活动 | |________________________________________________________________| | |_______________________________________________________________________________| CCCCCCCC CCCCCCCCCC CC C ________________________________________________________________ | | | 记录并可能监控此设备上的活动 | |________________________________________________________________|

Bnet-A101#端子宽度511

DEBUG:netmiko:In disable_paging DEBUG:netmiko:Command: 终端长度 0

DEBUG:netmiko:write_channel: b'terminal length 0\n' DEBUG:netmiko:Pattern is: terminal\length\ 0 DEBUG:netmiko:_read_channel_expect read_data: Bnet-A101# DEBUG:netmiko:_read_channel_expect read_data: terminal len DEBUG:netmiko: _read_channel_expect read_data: gth 0

DEBUG:netmiko:Pattern found: terminal\length\ 0 Bnet-A101#terminal length 0

DEBUG:netmiko:Bnet-A101#终端长度 0

DEBUG:netmiko:Exiting disable_paging DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko: read_channel:调试:netmiko:write_channel:b'\n'调试:netmiko:read_channel:调试:netmiko:write_channel:b'\n'调试:netmiko:read_channel:调试:netmiko:write_channel:b'\n'调试:netmiko :read_channel: 调试:netmiko:write_channel: b'\n' 调试:netmiko:read_channel: Bnet-A101#

Bnet-A101# Bnet-A101# Bnet-A101# Bnet-A101# Bnet-A101# Bnet-A101#

DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: 提示是 Bnet-A101# DEBUG:netmiko:write_channel: b'ssh 192.168.247.2\n' DEBUG:netmiko:read_channel: ssh 192.168.247.2

密码:

调试:netmiko:read_channel: 调试:netmiko:write_channel: b'\n' 调试:netmiko:read_channel:

DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n ' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\ n' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel: CCCCAuthentication failed,请重试!

密码:

DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: prompt is Password: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:Pattern is: Bnet-A101 DEBUG:netmiko:_read_channel_expect read_data: CCCCAuthentication failed , 请再试一次!

密码:

DEBUG:netmiko:_read_channel_expect read_data: CCCCAuthentication failed,请重试!密码:

调试:netmiko:_read_channel_expect read_data:

调试:netmiko:_read_channel_expect read_data:

[到192.168.247.2的连接被国外主机关闭] Bnet-A101# Bnet-A101#

DEBUG:netmiko:Pattern found: Bnet-A101 CCCCAuthentication failed,请重试!

密码:CCCC认证失败,请重试!密码:

[到192.168.247.2的连接被国外主机关闭] Bnet-A101# Bnet-A101#

调试:netmiko:read_channel:

Bnet-A101# Bnet-A101# Bnet-A101# Bnet-A101# Bnet-A101#

DEBUG:netmiko:Clear buffer 检测通道中的数据 DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'\n' DEBUG:netmiko:read_channel:

Bnet-A101#

DEBUG:netmiko:read_channel: DEBUG:netmiko:[find_prompt()]: prompt is Bnet-A101# DEBUG:netmiko:read_channel: DEBUG:netmiko:write_channel: b'show version\n' DEBUG:netmiko:Pattern is: show\版本 DEBUG:netmiko:_read_channel_expect read_data: s DEBUG:netmiko:_read_channel_expect read_data: 如何版本

DEBUG:netmiko:Pattern found: show\ version show version

调试:netmiko:read_channel:调试:netmiko:read_channel:调试:netmiko:read_channel:调试:netmiko:read_channel:调试:netmiko:read_channel:调试:netmiko:read_channel:调试:netmiko:read_channel:调试:netmiko:read_channel:调试: netmiko:read_channel: 调试:netmiko:read_channel:

在 R1 而非 R2 下执行的连接的船舶 ip 路由的输出

4

0 回答 0