Python:通过 SSH 连接到 Cisco 设备并运行 show 命令

发布于 2024-11-30 16:48:50 字数 925 浏览 0 评论 0原文

我已经广泛阅读了这篇文章,并研究了 Exscript、paramiko、Fabric 和 pxssh,但我仍然迷失与 Cisco 路由器的持久 ssh 会话。我是 python 脚本编写的新手。

我正在尝试用 Python 编写一个脚本,该脚本将通过 SSH 连接到 Cisco 设备,运行“show version”,在记事本中显示结果,然后结束脚本。

我可以使用不需要用户与设备交互的 show 命令来实现这一点。例如:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              
conn = SSH2()                       
conn.connect('192.168.1.11')     
conn.login(account)                 

conn.execute('show ip route')
print conn.response

conn.send('exit\r')               
conn.close()                        

上面的脚本将显示“show ip route”的结果。

如果我尝试 conn.execute('show version') 脚本会超时,因为 Cisco 设备希望用户按空格键继续,按回车键显示下一行或按任意键返回命令行。

如何执行show version命令,按两次空格键显示show version命令的整个输出,然后在python中打印它?

谢谢你!!!!

I have read over this post extensively and have researched Exscript, paramiko, Fabric and pxssh and I am still lost Persistent ssh session to Cisco router . I am new to python scripting.

I am attempting to write a script in Python that will SSH into a Cisco device, run "show version", display the results in notepad, then end the script.

I can get this working with show commands that do not require the user to interact with the device. For example:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              
conn = SSH2()                       
conn.connect('192.168.1.11')     
conn.login(account)                 

conn.execute('show ip route')
print conn.response

conn.send('exit\r')               
conn.close()                        

The above script will display the results of "show ip route".

If I try conn.execute('show version') the script times out because the Cisco device is expecting the user to press space bar to continue, press return to show the next line or any key to back out to the command line.

How can I execute the show version command, press space bar twice to display the entire output of the show version command, then print it in python?

Thank you!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-12-07 16:48:50

在运行 show version 之前尝试执行 terminal length 0。例如:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              
conn = SSH2()                       
conn.connect('192.168.1.11')     
conn.login(account)  

conn.execute('terminal length 0')           

conn.execute('show version')
print conn.response

conn.send('exit\r')               
conn.close()  

从 Cisco 终端文档: http://www.cisco.com/en/US/docs/ios/12_1/configfun/command/reference/frd1003.html#wp1019281

Try executing terminal length 0 before running show version. For example:

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              
conn = SSH2()                       
conn.connect('192.168.1.11')     
conn.login(account)  

conn.execute('terminal length 0')           

conn.execute('show version')
print conn.response

conn.send('exit\r')               
conn.close()  

From the Cisco terminal docs: http://www.cisco.com/en/US/docs/ios/12_1/configfun/command/reference/frd1003.html#wp1019281

明媚如初 2024-12-07 16:48:50

首先执行

terminal length 0

禁用分页。

First execute

terminal length 0

to disable paging.

楠木可依 2024-12-07 16:48:50

我刚刚问了同样的问题,下面的代码将从列表中运行并获取您所要求的信息。

from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\NewdayTest.txt','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'C:\IPAddressList.txt','r') # a simple list of IP addresses you want to connect to each one on a new line

for host in ip_add_file:
    host = host.strip()
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable') #Editable to be what ever is needed
    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()

I just asked the same thing and the below code will run from a list and obtain the information you are asking for.

from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\NewdayTest.txt','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'C:\IPAddressList.txt','r') # a simple list of IP addresses you want to connect to each one on a new line

for host in ip_add_file:
    host = host.strip()
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable') #Editable to be what ever is needed
    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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文