[UPD] Update to python3

This commit is contained in:
root
2026-03-06 18:17:35 +01:00
parent cb8bac56ea
commit 3c0338cb41
+27 -23
View File
@@ -25,7 +25,7 @@ import signal
from iniparse import RawConfigParser
from optparse import OptionParser
from distutils.spawn import find_executable
#from distutils.spawn import find_executable
usage="""\
%prog -c configfile action
@@ -106,7 +106,8 @@ Sent 2 probes (1 broadcast(s))
"""
ARPING1 = re.compile(r'bytes from (?P<mac>\S+).*time=(?P<rtt>[0-9.]*) (?P<unit>.*)')
ARPING2 = re.compile(r'reply from.*\[(?P<mac>\S+)\]\s+(?P<rtt>[0-9.]*)(?P<unit>.*)')
ARPING_PATH = find_executable('arping')
# ARPING_PATH = find_executable('arping')
ARPING_PATH = "/usr/bin/arping"
if ARPING_PATH == None:
raise Exception('No arping command found')
elif "/usr/bin/arping" in ARPING_PATH:
@@ -115,14 +116,14 @@ Sent 2 probes (1 broadcast(s))
device = device,
target_ip = target_ip,
))
packets = [p.groupdict() for p in ARPING2.finditer(output)]
packets = [p.groupdict() for p in ARPING2.finditer(output.decode('utf-8'))]
elif "/usr/sbin/arping" in ARPING_PATH:
(returncode,output) = run('arping -c{ping_count} -i{device} {target_ip}'.format(
ping_count = ping_count,
device = device,
target_ip = target_ip,
))
packets = [p.groupdict() for p in ARPING1.finditer(output)]
packets = [p.groupdict() for p in ARPING1.finditer(output.decode('utf-8'))]
result = {}
if packets:
result['mac'] = packets[-1]['mac']
@@ -216,7 +217,7 @@ ipv4 2 udp 17 178 src=192.168.149.184 dst=80.13.55.10 sport=1194 dport=1194 src=
"""
conn = output.splitlines()
for c in conn:
if "={src} ".format(src=self.last_ip) in c:
if "={src} ".format(src=self.last_ip) in c.decode('utf-8'):
return True
return False
@@ -247,7 +248,7 @@ ipv4 2 udp 17 178 src=192.168.149.184 dst=80.13.55.10 sport=1194 dport=1194 src=
6: br1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT
"""
LINK = re.compile(r':\s+<(?P<link_states>.+)>.* state (?P<link_status>.+?)\s')
link = LINK.search(output)
link = LINK.search(output.decode('utf-8'))
if link:
self._link_states = link.groupdict()['link_states'].split(',')
self._link_status = link.groupdict()['link_status']
@@ -260,12 +261,12 @@ ipv4 2 udp 17 178 src=192.168.149.184 dst=80.13.55.10 sport=1194 dport=1194 src=
if self.target_ip:
(retcode,route) = run('/sbin/ip route show {target_ip}'.format(target_ip=self.target_ip))
if self.gateway:
if not "{target_ip} via {gateway}".format(target_ip=self.target_ip,gateway=self.gateway) in route:
if not "{target_ip} via {gateway}".format(target_ip=self.target_ip,gateway=self.gateway) in route.decode('utf-8'):
logger.debug(run('/sbin/ip route del {target_ip}'.format(target_ip=self.target_ip),dry_run=self.dry_run)[1])
logger.warning('No route for {target_ip} via {gateway}, adding one'.format(target_ip=self.target_ip,gateway=self.gateway))
logger.debug(run('/sbin/ip route add {target_ip} via {gateway}'.format(target_ip=self.target_ip,gateway=self.gateway),dry_run=self.dry_run)[1])
elif self.device:
if not " {} ".format(self.device) in route:
if not " {} ".format(self.device) in route.decode('utf-8'):
logger.warning('No route for {target_ip} through {device}, adding one'.format(target_ip=self.target_ip,device=self.device))
logger.debug(run('/sbin/ip route add {target_ip} dev {device}'.format(target_ip=self.target_ip,device=self.device),dry_run=self.dry_run)[1])
else:
@@ -305,8 +306,8 @@ available == True if actual rtt and loss are below the max_rtt and max_loss
ping_interval=self.ping_interval,
))
if returncode == 0:
report = REPORT.search(output)
rtt = RTT.search(output)
report = REPORT.search(output.decode('utf-8'))
rtt = RTT.search(output.decode('utf-8'))
if report:
self.last_loss = int(report.groupdict()['loss'])
else:
@@ -326,11 +327,11 @@ available == True if actual rtt and loss are below the max_rtt and max_loss
elif self.last_rtt>self.max_rtt:
self.status='Too long RTT {}ms'.format(self.last_rtt)
else:
self.status = 'ping test failed : {}'.format(output)
self.status = 'ping test failed : {}'.format(output.decode('utf-8'))
else:
self._available = True
else:
self.status = 'Device {} is down or link state is unknown'.format(self.device)
self.status = 'Device {} is down or link state is unknown'.format(self.device.decode('utf-8'))
self._available = False
self.update_leds()
@@ -341,12 +342,12 @@ available == True if actual rtt and loss are below the max_rtt and max_loss
(retcode,output) = run('ip addr show dev {device}'.format(device=self.device))
IPV4ADDR = re.compile(r'\sinet\s+(?P<ipv4>\d+.\d+.\d+.\d+)[/\s]')
MACADDR = re.compile(r'link/(?P<type>\S+)(\s(?P<mac>\S+))?')
ipaddr = IPV4ADDR.search(output)
ipaddr = IPV4ADDR.search(output.decode('utf-8'))
if ipaddr:
self.last_ip = ipaddr.groupdict()['ipv4']
else:
self.last_ip = None
macaddr = MACADDR.search(output)
macaddr = MACADDR.search(output.decode('utf-8'))
if macaddr:
self.device_mac = macaddr.groupdict()['mac']
self.device_type = macaddr.groupdict()['type']
@@ -371,7 +372,7 @@ available == True if actual rtt and loss are below the max_rtt and max_loss
88.163.76.254 scope link src 88.163.76.120
"""
GW = re.compile(r'default via (?P<gateway>\d+.\d+.\d+.\d+)\s+')
gw = GW.search(output)
gw = GW.search(output.decode('utf-8'))
if gw:
logger.debug('Gateway : {}'.format(gw.groupdict()['gateway']))
return gw.groupdict()['gateway']
@@ -559,11 +560,13 @@ def write_pidfile(pidfile,pid=None):
pid = os.getpid()
oldpid = is_pid_running(pidfile)
if oldpid:
if oldpid <> pid:
# if oldpid <> pid:
if oldpid != pid:
raise Exception('There is already a running process {} for the pid file {}'.format(oldpid,pidfile))
with open(pidfile,"wb") as f:
f.write(str(pid))
print(pid)
f.write(bytes(pid))
def remove_pidfile(pidfile):
if os.path.isfile(pidfile):
@@ -573,7 +576,7 @@ if __name__ == '__main__':
(options,args)=parser.parse_args()
if len(args) < 1:
print "ERROR : You must provide one action to perform"
print("ERROR : You must provide one action to perform")
parser.print_usage()
sys.exit(2)
@@ -684,6 +687,7 @@ if __name__ == '__main__':
if not provider.enabled:
logger.warning("Enabling the available provider {}".format(provider.provider_name))
provider.enable()
run('conntrack -F')
if provider.openvpn_master:
restart_openvpn()
@@ -698,7 +702,7 @@ if __name__ == '__main__':
nexthop via 185.16.51.9 realm 3 dev eth1 weight 1
nexthop dev tun2 weight 1
"""
balance = output.splitlines()
balance = output.decode('utf-8').splitlines()
in_balance = False
for l in balance:
if provider.gateway in l.split(' ') or provider.device in l.split(' '):
@@ -738,11 +742,11 @@ if __name__ == '__main__':
else:
selproviders = providers
for provider in selproviders:
print "Checking {}".format(provider.provider_name)
print("Checking {}".format(provider.provider_name))
provider.check_available()
print provider
print(provider)
if provider.used_by_openvpn():
print "This provider is used by Openvpn"
print("This provider is used by Openvpn")
elif action == 'check-json':
result = []
if len(args) >= 2:
@@ -752,4 +756,4 @@ if __name__ == '__main__':
for provider in selproviders:
provider.check_available()
result.append(provider.as_dict())
print jsondumps(result,indent=True)
print(jsondumps(result,indent=True))