first version of automonty: looks at IPs

master
Marek Isalski 4 years ago
parent 7ca0320691
commit 7fb8ca00dd

@ -7,6 +7,7 @@ verify_ssl = true
[packages] [packages]
librouteros = "==2.*" librouteros = "==2.*"
progress = "*"
[requires] [requires]
python_version = "3.8" python_version = "3.8"

@ -0,0 +1,60 @@
#!/usr/bin/python3
import argparse
import librouteros
import progress.bar
import ssl
import os
def connection( host, username = None, password = None, port = 8729 ):
if ':' in host:
( host, port ) = host.split( ":", 1 )
port = int( port )
kwargs = { 'username': username or os.environ.get( 'AUTOMONTY_USERNAME', None ),
'password': password or os.environ.get( 'AUTOMONTY_PASSWORD', None ),
'host': host,
'port': port,
}
kwargs[ 'ssl' ] = True
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False # XXX figure out how
ssl_option = 'CERT_REQUIRED'
ssl_ctx.verify_mode = getattr( ssl, ssl_option, ssl.CERT_REQUIRED )
ssl_ctx.set_ciphers( 'DHE-RSA-AES256-GCM-SHA384' )
kwargs[ 'ssl_wrapper' ] = ssl_ctx.wrap_socket
return librouteros.connect( **kwargs )
def connect_routers( routers ):
rval = {}
with progress.bar.PixelBar( 'Connecting', max = len( routers ) ) as bar:
for router in routers:
rval[ router ] = connection( router )
bar.next()
return rval
def monty_check( args ):
for addr in args.addr:
for name, api in args.router.items():
for item in api( cmd ="/ip/address/print", detail = True ):
if item[ 'network' ] == addr:
print( name, ":", 'ENABLED' if not item[ 'disabled' ] else 'disabled', item[ 'interface' ], '#', item[ 'comment' ] )
def main():
parser = argparse.ArgumentParser( prog = "automonty",
description = 'AutoMonty (re-)configures routers',
)
subparsers = parser.add_subparsers()
parser.add_argument( '--router', action = 'append' )
parser_check = subparsers.add_parser( 'check' )
parser_check.add_argument( 'addr', action = 'append' )
parser_check.set_defaults( func = monty_check )
args = parser.parse_args()
args.router = connect_routers( args.router )
args.func( args )
if __name__ == '__main__':
main()
Loading…
Cancel
Save