实验5 异常处理

import paramiko
import time
import getpass
import sys
import socket
username = input("Username: ")
password = getpass.getpass("Password: ")
ip_file = sys.argv[1]
cmd_file = sys.argv[2]
# 存放认证失败的设备信息
switch_with_authentication_issue = []
# 存放⽹络不通的设备信息
switch_not_reachable = []
iplist = open(ip_file,'r')
for line in iplist.readlines():
    try:
        ip = line.strip()
        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()
        cmdlist = open(cmd_file, 'r')
        cmdlist.seek(0)
        for line in cmdlist.readlines():
            each_command = line.strip()
            command.send(each_command + '\n')
            time.sleep(3)
        cmdlist.close()
        output = command.recv(65535).decode('ASCII')
        print(output)
    except paramiko.ssh_exception.AuthenticationException:
        print(ip + "⽤户验证失败!")
        switch_with_authentication_issue.append(ip)
    except socket.error:
        print(ip + "⽬标不可达!")
        switch_not_reachable.append(ip)
iplist.close()
ssh_client.close()
# 已将202端口down了

E:\log\python\pythonProject\testdemo> python Demo05.py g2_ip.txt g1_comm.txt
Username: python
Password:
192.168.8.202⽬标不可达!
=-=-=-=-=-=-=-=-=-=-=-=-=-=
已经成功登陆交换机 192.168.8.203

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-05-31 11:35:39.
<Huawei>sys
Enter system view, return user view with Ctrl+Z.
[Huawei]dis cu
#
sysname Huawei
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

期待未来的男孩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值