簡體   English   中英

通過 paramiko 配置 cisco 路由器

[英]config a cisco router through paramiko

我正在嘗試通過 paramiko 配置 cisco 路由器。 首先,我 ssh 到路由器,然后運行命令。 但是當我連接到路由器時,我無法進入配置模式。 我的意思是路由器以用戶模式連接並運行enconf t不起作用!

conn = paramiko.SSHClient()
conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn.connect("20.0.0.1", 22, "cisco", "123")
stdin, stdout, stderr = conn.exec_command('show ip int br')
status = stdout.channel.exit_status_ready()
if status==0:
 mystring = stdout.read()
 print mystring

status 為 0,但 mystring 為空字符串。(結果為:[])

我在 Fedora 20 上。

謝謝

注意:請務必在您的思科交換機上啟用 ssh。 我使用了 Cisco VIRL ISO。 還要根據您的 ssh 更改用戶名和密碼。

import paramiko
import time
def attempt():

    IP = "192.168.0.52"
    USER = "cisco"
    Password = "cisco"
    PORT=22
      
    try:

        ssh = paramiko.SSHClient()
        #ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
        try:
            ssh.connect(IP , port=PORT, username=USER, password=Password)
            print ("Connected successfully.")

            connection = ssh.invoke_shell()
            connection.send("enable\n")
            time.sleep(.5)
            connection.send("cisco\n")
            time.sleep(.5)
           #connection.send("conf t\n")
            connection.send("show ip int brief\n")
            #connection.send("int loop 2\n")
           #connection.send("ip address 2.3.1.1 255.255.255.255\n")
            
            time.sleep(.5)
            router_output = connection.recv(65535)
            time.sleep(.5)
            print("\n\n")
            print(str(router_output) + "\n")
            time.sleep(.5)

            connection.send("end\n")

        except paramiko.AuthenticationException:
            print ("Incorrect password: "+Password)

        except socket.erro:
            print ("Socket Error")
    except:
        print("Something Went Wrong")

    

您的上述命令在 Cisco 881 上對我有效(即“show ip int br”運行正確並播放輸出。

如果我添加第二個命令,比如“conf t”或“enable”,它就會失敗。 這是因為 Cisco 上的 exec_command 只允許一個命令。 您需要運行 .invoke_shell() 方法來執行多個命令。

這是一個例子:

import paramiko
from getpass import getpass
import time

ip = raw_input("Please enter your IP address: ")
username = raw_input("Please enter your username: ")
password = getpass()

remote_conn_pre=paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(ip, port=22, username=username,  
                        password=password,
                        look_for_keys=False, allow_agent=False)

remote_conn = remote_conn_pre.invoke_shell()
output = remote_conn.recv(65535)
print output

remote_conn.send("show ip int brief\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print output

remote_conn.send("conf t\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print output

remote_conn.send("end\n")
time.sleep(.5)
output = remote_conn.recv(65535)
print output

您可能還想看看我一直在研究的這個庫。 它簡化了其中一些機制:

https://pynet.twb-tech.com/blog/automation/netmiko.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM