SSH 连接、下发配置、上传、下载

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值