-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux-ip-tool.py
executable file
·121 lines (104 loc) · 4.04 KB
/
linux-ip-tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python
import argparse
import subprocess
import json
import re
import ipaddress
import os
def list_all_ips():
"""
list all the ip addresses in the node
Return:
the ip addresses in map
"""
out = subprocess.check_output( ['ip', 'a'] )
result = {}
for line in out.split( "\n" ):
pos = line.find( ':' )
if pos != -1 and line[0:pos].isdigit():
words = line.split()
dev_name = words[1][0:-1]
result[dev_name] = {}
else:
line = line.strip()
words = line.split()
if len(words) > 2 and words[0] == 'inet':
ipv4 = words[1]
if 'ipv4' not in result[dev_name]:
result[dev_name]['ipv4'] = []
result[dev_name]['ipv4'].append( ipv4 )
elif len(words) > 2 and words[0] == 'inet6':
ipv6 = words[1]
if 'ipv6' not in result[dev_name]:
result[dev_name]['ipv6'] = []
result[dev_name]['ipv6'].append( ipv6 )
return result
def print_all_ips( args ):
print json.dumps( list_all_ips(), indent = 4 )
def extract_ip_addr( all_ips ):
cidr_ips = [ ip for dev in all_ips for ip_type in all_ips[dev] for ip in all_ips[dev][ip_type] ]
return [ ip.split('/')[0].strip() for ip in cidr_ips ]
def get_ip_addr( args ):
all_ips = list_all_ips()
# filter ip by the device name
cur_ips = {}
if args.dev:
pattern = re.compile( args.dev )
for ip_dev in all_ips:
if pattern.match( ip_dev ):
cur_ips[ ip_dev ] = all_ips[ip_dev]
else:
cur_ips = all_ips
# filter the ip by the ip type
all_ips = cur_ips
cur_ips = {}
if args.ip_type:
for ip_dev in all_ips:
if args.ip_type in all_ips[ip_dev]:
if ip_dev not in cur_ips:
cur_ips[ip_dev] = {}
cur_ips[ip_dev][args.ip_type] = all_ips[ip_dev][args.ip_type]
else:
cur_ips = all_ips
ip_addrs = extract_ip_addr( cur_ips )
if len( ip_addrs ) > 1 and args.all:
print json.dumps( ip_addrs )
elif len( ip_addrs ) > 0:
print ip_addrs[0]
def is_same_ip( ip1, ip2 ):
try:
return ipaddress.ip_address( unicode( ip1 ) ) == ipaddress.ip_address( unicode( ip2 ) )
except Exception as ex:
print ex
return False
def del_ip( args ):
all_ips = list_all_ips()
for ip in args.ip:
for dev in all_ips:
for ip_type in all_ips[dev]:
for item in all_ips[dev][ip_type]:
fields = item.split( '/' )
if len( fields ) == 2 and is_same_ip( ip, fields[0] ):
command = "ip a del %s dev %s" % ( item, dev )
print command
os.system( command )
def parse_args():
parser = argparse.ArgumentParser( description = "linux ip tools" )
subparsers = parser.add_subparsers( help = "linux ip tools" )
list_ip_parser = subparsers.add_parser( "list", help = "list all the ip address" )
list_ip_parser.set_defaults( func = print_all_ips )
get_ip_by_dev_parser = subparsers.add_parser( "get", help = "get ip addess" )
get_ip_by_dev_parser.add_argument( "--dev", help = "device pattern", required = False )
get_ip_by_dev_parser.add_argument( "--cidr", help = "cidr or ip address", required = False )
get_ip_by_dev_parser.add_argument( "--ip-type", help = "ip type", choices = ['ipv4', 'ipv6'], required = False )
get_ip_by_dev_parser.add_argument( "--all", help = "all the ip address in json array", action = "store_true", required = False )
get_ip_by_dev_parser.set_defaults( func = get_ip_addr )
del_ip_parser = subparsers.add_parser( "del", help = "delete ip address" )
del_ip_parser.add_argument( "ip", nargs = "+", help = "ip address to be deleted" )
del_ip_parser.set_defaults( func = del_ip )
return parser.parse_args()
def main():
args = parse_args()
args.func( args )
if __name__ == "__main__":
main()