使用 Paramiko 写入 Cisco shell 的命令不会执行

Commands written to Cisco shell with Paramiko do not get executed

我正在尝试使用 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()
      

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

session_shell.send("config terminal\n")

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