Python 使用Paramiko写入Cisco shell的命令不会执行
我正在尝试使用Paramiko远程配置Cisco路由器的IP地址,但我所做的一切似乎都不起作用!请有人看看我的代码,告诉我我做错了什么 提前感谢:) 您必须使用enter/new行(Python 使用Paramiko写入Cisco shell的命令不会执行,python,ssh,paramiko,cisco,Python,Ssh,Paramiko,Cisco,我正在尝试使用Paramiko远程配置Cisco路由器的IP地址,但我所做的一切似乎都不起作用!请有人看看我的代码,告诉我我做错了什么 提前感谢:) 您必须使用enter/new行(\n)来“提交”命令: 相关问题: import paramiko import socket import sys import time ip_address = "192.168.56.101" port = 22 username = "cisco" passw
\n
)来“提交”命令:
相关问题:
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()
session_shell.send("config terminal\n")