实验6 推送备份文件到FTP服务器

这段代码演示了如何使用Python的paramiko库远程登录多个华为交换机,并通过FTP将配置文件从192.168.1.8上传到各个交换机。操作过程中涉及到了SSH连接、FTP会话以及文件传输。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟器里的IP 已经变动

import paramiko
import time
username = 'python'
password = '123asd'
iplist =['192.168.1.9', '192.168.1.10', '192.168.1.11']
for ip in iplist:
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=ip, username=username, password=password, look_for_keys=False)
    print('=-=-=-=-=-=-=-=-=-=-=-=-=-=')
    print('已经成功登陆交换机 ' + ip)
    command = ssh_client.invoke_shell()
    command.send('ftp 192.168.1.8\n')
    time.sleep(0.5)
    command.send('root\n')
    time.sleep(0.5)
    command.send('Lxf940709.\n')
    time.sleep(0.5)
    command.send('bin\n')
    command.send('put vrpcfg.zip '+ip +'_vrpcfg.zip'+'\n')
    time.sleep(0.5)
    command.send('quit\n')
    time.sleep(0.5)
    output = command.recv(65535).decode('GB2312')
    print(output)
ssh_client.close()

在您的代码中,有几个潜在的问题和改进点,特别是在处理多个SSH连接时。以下是一些关键点和改进建议:

  1. SSH连接关闭:您的代码中只在最后关闭了一个SSH客户端实例(ssh_client.close()),但您为每个IP地址都创建了一个新的SSH客户端。这会导致内存泄漏和潜在的连接问题。您应该在循环内部关闭每个SSH客户端。

  2. 异常处理:网络编程中经常会发生连接失败或命令执行失败的情况。添加异常处理可以使您的脚本更加健壮。

  3. 命令执行的确认:您的脚本发送了一系列命令到SSH会话,但没有检查这些命令是否成功执行。例如,FTP登录可能失败,或者文件传输可能未成功。

  4. 输出处理:您打印了接收到的输出,但可能需要根据输出内容进一步处理或检查。

  5. 编码问题:您假设输出是GB2312编码,这通常是正确的,但如果服务器配置不同,可能会导致解码错误。

  6. 命令发送与接收的延迟:您已经添加了延迟(time.sleep(0.5)),这通常是必要的,但具体延迟时间可能需要根据实际情况调整。

C:\Users\82127\.conda\envs\pythonProject\python.exe E:/log/python/pythonProject/Demo06.py
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 192.168.1.9

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-06-01 23:34:42.
<Huawei>ftp 192.168.1.8
Trying 192.168.1.8 ...
Press CTRL+K to abort
Connected to 192.168.1.8.
220 (vsFTPd 3.0.3)
User(192.168.1.8:(none)):root
331 Please specify the password.
Enter password:
230 Login successful.

[ftp]bin
200 Switching to Binary mode.

[ftp]put vrpcfg.zip 192.168.1.9_vrpcfg.zip
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
FTP: 602 byte(s) sent in 0.170 second(s) 3.54Kbyte(s)/sec.

[ftp]quit

221 Goodbye.

<Huawei>
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 192.168.1.10

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-06-01 23:34:45.
<Huawei>ftp 192.168.1.8
Trying 192.168.1.8 ...
Press CTRL+K to abort
Connected to 192.168.1.8.
220 (vsFTPd 3.0.3)
User(192.168.1.8:(none)):root
331 Please specify the password.
Enter password:
230 Login successful.

[ftp]bin
200 Switching to Binary mode.

[ftp]put vrpcfg.zip 192.168.1.10_vrpcfg.zip
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
FTP: 603 byte(s) sent in 0.160 second(s) 3.76Kbyte(s)/sec.

[ftp]quit

221 Goodbye.

<Huawei>
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 192.168.1.11

Info: The max number of VTY users is 5, and the number
      of current VTY users on line is 1.
      The current login time is 2022-06-01 23:34:48.
<Huawei>ftp 192.168.1.8
Trying 192.168.1.8 ...
Press CTRL+K to abort
Connected to 192.168.1.8.
220 (vsFTPd 3.0.3)
User(192.168.1.8:(none)):root
331 Please specify the password.
Enter password:
230 Login successful.

[ftp]bin
200 Switching to Binary mode.

[ftp]put vrpcfg.zip 192.168.1.11_vrpcfg.zip
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
FTP: 603 byte(s) sent in 0.190 second(s) 3.17Kbyte(s)/sec.

[ftp]quit

221 Goodbye.

<Huawei>

进程已结束,退出代码0

FTP服务器

[root@testhost ~]# ll
total 20
-rw-r--r--  1 root root  603 Jun  2 07:34 192.168.1.10_vrpcfg.zip
-rw-r--r--  1 root root  603 Jun  2 07:34 192.168.1.11_vrpcfg.zip
-rw-r--r--  1 root root  602 Jun  2 07:34 192.168.1.9_vrpcfg.zip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

期待未来的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值