Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过paramiko配置cisco路由器_Python_Ssh_Paramiko - Fatal编程技术网

Python 通过paramiko配置cisco路由器

Python 通过paramiko配置cisco路由器,python,ssh,paramiko,Python,Ssh,Paramiko,我正试图通过paramiko配置cisco路由器。 首先我使用ssh连接到路由器,然后运行命令。但当我连接到路由器时,我不能进入配置模式。 我的意思是路由器在用户模式下连接并且运行en或conft不工作 conn = paramiko.SSHClient() conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) conn.connect("20.0.0.1", 22, "cisco", "123") stdin, stdout, s

我正试图通过paramiko配置cisco路由器。 首先我使用ssh连接到路由器,然后运行命令。但当我连接到路由器时,我不能进入配置模式。 我的意思是路由器在用户模式下连接并且运行
en
conft
不工作

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
状态为0,但mystring为空字符串。(结果为:[])

我穿的是软呢帽20


谢谢

您的上述命令在Cisco 881上对我有效(即“show ip int br”正确运行并播放输出)

如果我添加第二个命令,如“conf t”或“enable”,则会失败。这是因为Cisco上的exec_命令只允许一个命令。您需要运行.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
您可能还想查看我一直在使用的这个库。它简化了一些机制:


注意:确保在cisco交换机上启用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")

    

关于
stderr
?关于
en
的stdout和stderr呢?实际上,我是python新手。我是通过搜索找到这段代码的。对不起,我不明白你的问题!连接后重试:
print conn.get_transport()。是否处于活动状态()
so:连接成功了吗?谢谢你的回复。运行
print conn.get_transport().is_active()
的结果是正确的。我发现exec_命令是一个非阻塞调用。所以我编辑了我的代码,但没有任何结果!