【问题标题】:Commands written to Cisco shell with Paramiko do not get executed使用 Paramiko 写入 Cisco shell 的命令不会被执行
【发布时间】:2021-02-28 20:30:02
【问题描述】:

我正在尝试使用 Paramiko 远程配置 Cisco 路由器的 IP 地址,但我所做的一切似乎都不起作用!请有人看看我的代码并告诉我我做错了什么?

提前致谢:)

import paramiko
import socket 
import sys
import time

ip_address = "192.168.56.101"
port = 22
username = "cisco"
password = "cisco123!"
session = ""

def SSHConnect():
    global session
    print("\n------Attempting to connect to remote server------\n")
    session = paramiko.SSHClient() #stores ssh client as var "session"
    session.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #adds missing policy automatically on first connection
    try:
        session.connect(ip_address, port,username,password,timeout=5)
    except socket.error:
        print("---Error! Unable to connect using supplied IP address / port - Program is now exiting...")
        sys.exit()
    except paramiko.ssh_exception.AuthenticationException:
        print("---Error! Supplied login information rejected by remote server - Program is now exiting...")
        sys.exit()

    print("Connected successfully!")
    
def conf_router():
    print("configuring router....")
    session_shell = session.invoke_shell()
    session_shell.send("config terminal")
    time.sleep(1)

    session_shell.send("int gigabitEthernet1")
    time.sleep(1)

    session_shell.send("ip address 192.168.5.2 255.255.255.0")
    time.sleep(1)

    
if __name__ == "__main__":
    SSHConnect()
    conf_router()
      

【问题讨论】:

    标签: python ssh paramiko cisco


    【解决方案1】:

    您必须使用 enter/new-line (\n)“提交”您的命令:

    session_shell.send("config terminal\n")
    

    一个相关问题:
    Paramiko exec_command with multiple commands on Cisco router not providing any output

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-13
      • 2018-12-04
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多