44import subprocess
55import yaml
66import os
7+ import re
78
89
910common_arguments = argparse .ArgumentParser (add_help = False )
@@ -22,21 +23,41 @@ def has_link(logger):
2223 link = False
2324 reachable_devs = set ()
2425
25- output = subprocess .check_output (["/sbin/ip" , "neigh " , "show" ]).decode ("utf-8" )
26+ output = subprocess .check_output (["/sbin/ip" , "address " , "show" ]).decode ("utf-8" )
2627
2728 lines = output .split ("\n " )
28- logger .debug ("/sbin/ip neigh show" )
29+ logger .debug ("/sbin/ip address show" )
30+ dev_match = re .compile (r"^\d+:" )
31+ skip_to_next_dev = True
2932 for line in lines :
3033 logger .debug (line )
3134 split_line = line .split ()
35+ if len (split_line ) < 2 :
36+ continue
37+
38+ logger .debug (split_line )
39+ if dev_match .match (split_line [0 ]):
40+ dev = split_line [1 ][0 :- 1 ] # strip off :
41+ if dev == "lo" or "no-carrier" in split_line [2 ].lower ():
42+ skip_to_next_dev = True ;
43+ else :
44+ skip_to_next_dev = False ;
45+ continue
3246
33- if not len ( split_line ) == 6 :
47+ if skip_to_next_dev :
3448 continue
35- ip , dev_str , dev , addr_str , addr , state = split_line
3649
37- if not state .lower () in ["incomplete" , "failed" , "none" ]:
38- link = True
39- reachable_devs .add (dev )
50+ if split_line [0 ].startswith ("inet" ):
51+ if "scope" in split_line and "global" in split_line :
52+ address = split_line [1 ]
53+ if "/" in address :
54+ address = address .split ("/" )[0 ]
55+ if address .startswith ("127." ) or address .startswith ("::1" ):
56+ continue
57+ # There's a global address - consider link up
58+ link = True
59+ logger .debug ("Device {} up" .format (dev ))
60+ reachable_devs .add (dev )
4061
4162 return link , reachable_devs
4263
0 commit comments