修改Python 3代码以处理RSA密钥指纹

时间:2018-09-28 14:17:55

标签: rsa-key-fingerprint

由于以下原因,以下代码无法连接到Cisco交换机:

RSA密钥指纹为3e:b7:7b:55:6b:a3:xx:xx:xx:xx
您确定要继续连接(是/否)吗?是的

#!/usr/bin/env python

from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'output_twinax.log','w') # Where you want the file to save to.
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'username' # edit to reflect
password = 'password' # edit to reflect

ip_add_file = open(r'IP-list','r') # a simple list of IP addresses you want to connect to each one o
n a new line

for host in ip_add_file:
    host = host.strip()
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    find_hostname = device.find_prompt()
    hostname = find_hostname.replace(">","")
    print(hostname)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable') #Editable to be what ever is needed
    output = device.send_command('sh int status | i SFP')
    print(output)
fd.close()

请帮助对其进行修改以说明RSA密钥。非常感谢。

1 个答案:

答案 0 :(得分:0)

您尝试使用use_keys关键字参数吗?

#!/usr/bin/env python

from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'output_twinax.log','w') # Where you want the file to save to.
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'username' # edit to reflect
password = 'password' # edit to reflect

# List of IP addresses in each line
ip_add_file = open(r'IP-list','r') 
  key_file = "./rsa_key.txt"

for host in ip_add_file:
  host = host.strip()
  device = ConnectHandler(device_type=platform, 
                          ip=host, 
                          username=username,
                          key_file=key_file,
                          use_keys=True)
  find_hostname = device.find_prompt()
  hostname = find_hostname.replace(">","")
  print(hostname)
  output = device.send_command('terminal length 0')
  output = device.send_command('enable')
  output = device.send_command('sh int status | i SFP')
  print(output)
fd.close()