import paramiko
import time
def client():
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname="192.168.56.100",port=22,username="huawei",password="Admin@123")
cmd = client.invoke_shell()
cmd.send("screen-length 0 temporary\n")
time.sleep(1)
cmd.send("sys imm\n")
time.sleep(1)
cmd.send("interface g1/0/0\n")
time.sleep(1)
cmd.send("undo shutdown\n")
time.sleep(1)
cmd.send("undo portswitch\n")
time.sleep(1)
cmd.send("int lo0\n")
time.sleep(1)
cmd.send("ip add 2.2.2.2 32\n")
time.sleep(1)
print(cmd.recv(65535).decode("utf-8"))
cmd.close()
def read_txt():
client2 = paramiko.SSHClient()
client2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client2.connect(hostname="192.168.56.100",port=22,username="huawei",password="Admin@123")
cmd2 = client2.invoke_shell()
f = open("cmd.txt","r")
mes = f.readlines()
for line in mes:
cmd2.send(line)
time.sleep(1)
print(cmd2.recv(65535).decode("utf-8"))
cmd2.close()
read_txt()
def down():
trs = paramiko.Transport(("192.168.56.100",22))
trs.connect(username="huawei",password="Admin@123")
sftp = paramiko.SFTPClient.from_transport(trs)
remote = "device.sys"
local = "new.sys"
sftp.get(remote,local)
trs.close()
down()
def up():
trs = paramiko.Transport(("192.168.56.100",22))
trs.connect(username="huawei",password="Admin@123")
sftp = paramiko.SFTPClient.from_transport(trs)
remote = "new.txt"
local = "cmd.txt"
sftp.put(local,remote)
trs.close()
up()
SSH 连接、下发配置、上传、下载
Python使用Paramiko操作Linux设备
最新推荐文章于 2025-12-05 17:02:52 发布
4741

被折叠的 条评论
为什么被折叠?



