从循环中返回所有数据

By simon at 2019-05-15 • 0人收藏 • 32人看过

我有登录到设备的代码。 我可以打印通知从设备在循环罚款。 但是我只能返回"不打印"列表中最后一个设备的数据。 我怎样才能返回所有的数据从所有设备的循环?

从烧瓶进口烧瓶,jsonify,要求

从 netmiko.ssh 自动检测从 netmiko.ssh 异常导入 NetMikoTimeoutException 导入时间

Appflask (名称)

@ app. 路由('/ firewall',方法'GET','POST','DELETE'])

Def firewall () :

# Authentication headers request.headers auth headers auth headers.get ("xxxxx") if auth'xxxx': data request.get json (force True) fw a data ["DeviceAddressList"] src a data ['SourceAddressList'] src p data ['SourcePortList'] dst a data ['destationresslist'] dst p data ["PolicyAllow"] p data ['Protocol'] p data ['Protocol ] p show data ['show'] p push ['push'] p data [ config ] # per Normalize the data for command line"({ interface })"'. 格式('|'。 (src a + src p + dst a + dst p)用户名"xxxx"密码"Pxxxx"try: ip list fw a for ip in ip list: # print (ip) device {"device type":"autodetect","username": username,"host": ip,"password": } SSHDetect (* * device) best match guessser.autodetect () print (best match) if"None"in str (best match) : continue else: if"true"in (p show) and"per jujujupmatch"in str (best match) device ["type device"] best match netminos。 Connecthandler (* * device,) connection.find prompt (delay factor 2) time.sleep (1) connection.enable () resp connection.send command ('show configuration | display xml | match'+ str (juniper command) ,delay factor 2) print (ip +' n'+ best match + resp) if"true"in str (p push) and"juniper junos"in str (best match) : device ["device type"] best match netmiko. Connecthandler (* * Device) connection.find prompt (delay factor 2) time.sleep (1) connection.enable () push resp connection.send command (config,delay factor 2) connection.disconnect () print (push resp) return ip +' n'+ best match + resp except NetMikoTimeoutException: 返回"This Network Device is not"else:

返回 jsonify ({"message":"ERROR: Unauthorized"}) ,401

Blockquote

1 个回复 | 最后更新于 2019-05-15
2019-05-15   #1

代码示例: 通过 ips 进行循环,获取每个 ip 想要返回的值,并将其推入 dict 中。 将 dict 返回给"防火墙"函数的调用者

Def firewall () : result dict () for ip in ip list: push resp dummy get push resp () result [ ip ] push resp return result

登录后方可回帖

Loading...