扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
折腾了一天的装饰器,貌似理解了其中的一点点...
运执行上面的代码,运行效果如下:
以下代码用类也可以实现相一样的结果,可以对比一下那个方便
#!/usr/bin/env python3 #coding=utf-8 import getpass from netmiko import ConnectHandler from netmiko.ssh_exception import NetMikoTimeoutException,NetMikoAuthenticationException class NWBackup(): def __init__(self): self.ip = input('IPAddress:') self.username = input('Username:') self.password = getpass.getpass() self.device={'device_type': 'cisco_ios', 'username': self.username, 'password': self.password, 'ip':self.ip } try: self.connect = ConnectHandler(**self.device) self.connect.enable() except (EOFError, NetMikoTimeoutException): print(u" 网络设备%s: 无法连接!请确认该设备IPAddress是否可达!" %self.ip) return except (EOFError, NetMikoAuthenticationException): sprint(u" 网络设备%s: 用户名与密码错误!请确认账号与密码!" %self.ip) return def disVersion(self): cmd = 'show version' print(self.connect.send_command(cmd)) self.connect.disconnect() def disInterface(self): cmd = 'show ip interface brief' print(self.connect.send_command(cmd)) self.connect.disconnect() def runComment(self): disfunc = int(input('pls input num: ')) if disfunc == 1: self.disVersion() if disfunc == 2: self.disInterface() if __name__ == '__main__': run = NWBackup() run.runComment()另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流