15

この投稿をよく読んで、Exscript、paramiko、Fabric、および pxssh を調査しましたが、まだCisco ルーターへの永続的な ssh セッションが失われています。私はPythonスクリプトを初めて使用します。

Cisco デバイスに SSH で接続し、「show version」を実行し、結果をメモ帳に表示してスクリプトを終了するスクリプトを Python で作成しようとしています。

ユーザーがデバイスを操作する必要のない 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 デバイスはユーザーがスペース バーを押して続行することを想定しているため、スクリプトがタイムアウトします。Return キーを押して次の行を表示するか、任意のキーを押してコマンド ラインに戻ります。

show version コマンドを実行し、スペースバーを 2 回押して show version コマンドの出力全体を表示し、Python で出力するにはどうすればよいですか?

ありがとうございました!!!!

4

3 に答える 3

21

terminal length 0を実行する前に実行してみてくださいshow version。例えば:

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

于 2011-08-21T20:31:13.323 に答える
7

最初の実行

terminal length 0

ページングを無効にします。

于 2011-08-21T20:29:32.117 に答える
2

私はちょうど同じことを尋ねました.以下のコードはリストから実行され、あなたが求めている情報を取得します.

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()
于 2016-09-06T09:54:37.497 に答える