Last active
January 10, 2017 01:34
-
-
Save raiderrobert/60ffbc1faa88022e487477d0e7f89756 to your computer and use it in GitHub Desktop.
Version checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
import click | |
from netmiko import ConnectHandler | |
def normalize(v): | |
""" | |
Converts strings with dot separation to lists | |
""" | |
return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")] | |
def version_compare(a, b): | |
""" | |
Compares 2 dot separated versions and determines less than, equal to, or greater | |
The return value is negative if a < b, zero if a == b and strictly positive if a > b. | |
""" | |
a = normalize(a) | |
b = normalize(b) | |
return (a > b) - (a < b) | |
def check_fw_version(required_version, **target): | |
fw = ConnectHandler(**target) | |
output = fw.send_command('get system status') | |
for line in output.split('\n'): | |
if line.startswith('Version:'): | |
reg = re.compile('\d\\.\d\\.\d') | |
result = reg.search(line) | |
machine_version = normalize(result.group()) | |
print("Target firmware version is: " + machine_version) | |
same_version = version_compare(required_version, machine_version) | |
if same_version == 0: | |
print('Firmware version is: ' + fwversion) | |
elif same_version == 1: | |
print('Firmware version is: ' + fwversion +'\nThis should be downgraded unless there is a special case.') | |
elif same_version == -1: | |
print('Firmware version is: ' + fwversion +'\nPlease consider upgrading.') | |
@click.command() | |
@click.option('--version', '-v', help='Firmware version to check.', default='5.2.4') #current firmware target, 5.2.4 as of 1/6/17 | |
@click.option('--host', '-h', help='IP address to target', default='192.168.180.1') | |
@click.option('--user', '-u', help='Username', default='admin') | |
@click.option('--password', '-p', help='Password.') | |
@click.option('--device', '-d', help='Device type.', default='fortinet') | |
def main(version, host, user, password, device): | |
target = { | |
'ip': host, | |
'device_type': device, | |
'username': user, | |
'password': password, | |
} | |
check_fw_version(version, **target) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment