check_openwrt_bssid: improve retries

This commit is contained in:
Cyberes 2024-04-01 14:41:36 -06:00
parent 5fab8adf54
commit b66ba4fa37
1 changed files with 19 additions and 5 deletions

View File

@ -21,7 +21,7 @@ def main(args):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for _ in range(args.retries + 1):
for retry_i in range(args.retries):
try:
ssh.connect(args.host, username='root')
except paramiko.ssh_exception.AuthenticationException:
@ -82,10 +82,24 @@ def main(args):
print(json.dumps(cells))
sys.exit(nagios.STATE_UNKNOWN)
# ============================================
# Check if we need to retry
if retry_i + 1 < args.retries:
if args.target_mac not in list(cells.keys()):
time.sleep(10)
continue
if not cells[args.target_mac]['ssid']:
time.sleep(10)
continue
if cells[args.target_mac]['ssid'] != args.expected_ssid:
time.sleep(10)
continue
# ============================================
ap_data = cells[args.target_mac]
computed_quality = int(try_float(ap_data['quality'][0]) / try_float(ap_data['quality'][1]) * 100)
@ -129,7 +143,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--host', required=True, help='The host to SSH into.')
parser.add_argument('--interface', required=True, help='The wireless interface to use (example: `wlan0`).')
parser.add_argument('--retries', default=3, help='If the target AP is not found, do this many retries.')
parser.add_argument('--retries', default=4, help='If the target AP is not found, do this many retries.')
parser.add_argument('--print', action='store_true', help='Print the found APs.')
parser.add_argument('--target-mac', required=True, help='The MAC address of the target AP.')
parser.add_argument('--expected-ssid', required=True, help="The AP's expected SSID.")