将SSH输出从Cisco路由器打印到文本fi

2024-05-20 22:03:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和编程界的新手。经过过去两天的一些研究,我现在能够成功地SSH到Cisco路由器并执行一组命令。然而,我最初的目标是将结果输出打印到文本文件中。查看了论坛成员的许多帖子,这些帖子帮助我构建代码,但我无法将结果打印到文本文件中。请帮忙。

这是我的代码:

import paramiko
import sys
import os

dssh = paramiko.SSHClient()
dssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
dssh.connect('10.0.0.1', username='cisco', password='cisco')  
stdin, stdout, stderr = dssh.exec_command('sh ip ssh')
print stdout.read()
f = open('output.txt', 'a')
f.write(stdout.read())
f.close()
dssh.close()

Tags: 代码importparamikocloseread编程stdout路由器
2条回答

stdout.read()将读取内容并向前移动文件指针。因此,后续调用将无法再次读取内容。因此,如果要打印内容并将其写入文件,则应先将其存储在变量中,然后打印并写入该变量。


Instead of mentioning the IP address directly on the code, is it possible for me to fetch it from list of IP addresses (mentioned line by line) in a text file?

您可以从如下文件中读取行:

with open('filename') as f:
    for line in f:
        # Each line will be iterated; so you could call a function here
        # that does the connection via SSH
        print(line)

我知道这已经很晚了,但是下面的代码正是我用来做被要求做的事情的代码。

from __future__ import print_function
from netmiko import ConnectHandler

import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Users\NewdayTest.txt','w') 
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'Username'
password = 'Password'

ip_add_file = open(r'C:\Users\\IPAddressList.txt','r') 

for host in ip_add_file:
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable')
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
    output = device.send_command('sh run')
    print(output)
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
    output = device.send_command('sh ip int br')
    print(output) 
    print('##############################################################\n')

fd.close()

或者,如果要从单个主机打印,请使用此小编辑。这只会删除查找列表以获取IP地址:

from __future__ import print_function
from netmiko import ConnectHandler

import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Users\NewdayTest.txt','w') 
old_stdout = sys.stdout   
sys.stdout = fd
host = '10.10.10.10'
platform = 'cisco_ios'
username = 'Username'
password = 'Password'
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
output = device.send_command('enable')
print('##############################################################\n')
print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
output = device.send_command('sh run')
print(output)
print('##############################################################\n')
print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
output = device.send_command('sh ip int br')
print(output) 
print('##############################################################\n')

fd.close()

相关问题 更多 >