赞
踩
前提条件: 你需要拥有一台或者两台能够SSH登录的路由器!
导入并使用Lab9制作的ping模块(可以适当修改)和Lab10制作的SSH模块(可以适当修改)制作一个函数,你可以同时传入多个IP地址,首先尝试Ping这个路由器,如果能Ping通!就登陆此设备采集接口信息,如果不能Ping通就跳过此设备
- import lab_ssh
- import lab_ping
- import re
- from pprint import pprint
-
-
-
- def get_info(ip_list, username, passwd, cmd='dis ip int br'):
- router_dic = {}
- for ip in ip_list:
- if not lab_ping.ping(ip):
- print(ip + '网络不可达')
- else:
- info = lab_ssh.ssh(ip, 'user1', '12345678')
- ssh_result = re.search('Interface +IP +Address/Mask +Physical +Protocol[\s\S]+', info).group().split(
- '\r\n')[1:-1]
- ssh_dic = {}
- for i in ssh_result:
- if 'unassigned' not in i:
- ssh_info = re.split(' +', i)
- ssh_dic[ssh_info[0]] = ssh_info[1]
- router_dic[ip]=ssh_dic
- return router_dic
-
- if __name__ == '__main__':
- ip_list = ['192.168.80.1', '192.168.80.101', '192.168.80.102']
- result = get_info(ip_list, 'user1', '12345678')
- pprint(result)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。