折腾了一天的装饰器,貌似理解了其中的一点点...
![](/upload/ad_content/xuanchuantu-23.jpg)
成都创新互联服务项目包括尧都网站建设、
尧都网站制作、尧都网页制作以及尧都网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,
尧都网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到尧都省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!#!/usr/bin/env python3
#coding=utf-8
import getpass
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException,NetMikoAuthenticationException
def auth(Conn):
def wrapper(ip,username,password):
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
try:
connect = ConnectHandler(**device)
connect.enable()
except (EOFError, NetMikoTimeoutException):
print(u" 网络设备%s: 无法连接!请确认该设备IPAddress是否可达!"%ip)
return
except (EOFError, NetMikoAuthenticationException):
print(u" 网络设备%s: 用户名与密码错误!请确认账号与密码!" %ip)
return
Conn(ip,username,password,connect)
return Conn
return wrapper
@auth
def showInterface(ip,username,password,connect):
res = connect.send_command('show ip int brief')
print(res)
connect.disconnect()
@auth
def showVersion(ip,username,password,connect):
res = connect.send_command('show version')
print(res)
connect.disconnect()
def main():
print(u"选项请输入数字,如: 1、2")
print(u"1. 查询设备接口信息")
print(u"2. 查询设备版本信息")
print("")
runFunc = int(input(u'请输入需要执行命令的选项: '))
input_ip = input("IPAddress: ")
ipaddress = input_ip.split(",")
username = input("Username: ")
password = getpass.getpass()
for ip in ipaddress:
if runFunc == 1:
showInterface(ip,username,password)
if runFunc == 2:
showVersion(ip,username,password)
if __name__ == '__main__':
main()
运执行上面的代码,运行效果如下:
![Python: 装饰器的小例子](/upload/otherpic9/116712.jpg)
以下代码用类也可以实现相一样的结果,可以对比一下那个方便
#!/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许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:Python:装饰器的小例子-创新互联
网站URL:
http://whjierui.cn/article/dcieii.html