|
|
@ -5,6 +5,7 @@ import librouteros
|
|
|
|
import progress.bar
|
|
|
|
import progress.bar
|
|
|
|
import ssl
|
|
|
|
import ssl
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
def connection( host, username = None, password = None, port = 8729 ):
|
|
|
|
def connection( host, username = None, password = None, port = 8729 ):
|
|
|
|
if ':' in host:
|
|
|
|
if ':' in host:
|
|
|
@ -24,12 +25,20 @@ def connection( host, username = None, password = None, port = 8729 ):
|
|
|
|
kwargs[ 'ssl_wrapper' ] = ssl_ctx.wrap_socket
|
|
|
|
kwargs[ 'ssl_wrapper' ] = ssl_ctx.wrap_socket
|
|
|
|
return librouteros.connect( **kwargs )
|
|
|
|
return librouteros.connect( **kwargs )
|
|
|
|
|
|
|
|
|
|
|
|
def connect_routers( routers ):
|
|
|
|
def connect_routers( config, args ):
|
|
|
|
rval = {}
|
|
|
|
rval = {}
|
|
|
|
with progress.bar.PixelBar( 'Connecting', max = len( routers ) ) as bar:
|
|
|
|
if args:
|
|
|
|
for router in routers:
|
|
|
|
with progress.bar.PixelBar( 'Connecting', max = len( args ) ) as bar:
|
|
|
|
rval[ router ] = connection( router )
|
|
|
|
for router in args:
|
|
|
|
bar.next()
|
|
|
|
rval[ router ] = connection( router )
|
|
|
|
|
|
|
|
bar.next()
|
|
|
|
|
|
|
|
elif config:
|
|
|
|
|
|
|
|
with progress.bar.PixelBar( 'Connecting', max = len( config ) ) as bar:
|
|
|
|
|
|
|
|
for ( name, kwargs ) in config.items():
|
|
|
|
|
|
|
|
if 'host' not in kwargs:
|
|
|
|
|
|
|
|
kwargs[ 'host' ] = name
|
|
|
|
|
|
|
|
rval[ name ] = connection( **kwargs )
|
|
|
|
|
|
|
|
bar.next()
|
|
|
|
return rval
|
|
|
|
return rval
|
|
|
|
|
|
|
|
|
|
|
|
def monty_check( args ):
|
|
|
|
def monty_check( args ):
|
|
|
@ -39,7 +48,15 @@ def monty_check( args ):
|
|
|
|
if item[ 'network' ] == addr:
|
|
|
|
if item[ 'network' ] == addr:
|
|
|
|
print( name, ":", 'ENABLED' if not item[ 'disabled' ] else 'disabled', item[ 'interface' ], '#', item[ 'comment' ] )
|
|
|
|
print( name, ":", 'ENABLED' if not item[ 'disabled' ] else 'disabled', item[ 'interface' ], '#', item[ 'comment' ] )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_configuration():
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
config = open( os.path.expanduser( '~/.automonty.yaml' ), 'r' )
|
|
|
|
|
|
|
|
except IOError:
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return yaml.load( config.read(), yaml.SafeLoader )
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
|
|
|
|
config = load_configuration()
|
|
|
|
parser = argparse.ArgumentParser( prog = "automonty",
|
|
|
|
parser = argparse.ArgumentParser( prog = "automonty",
|
|
|
|
description = 'AutoMonty (re-)configures routers',
|
|
|
|
description = 'AutoMonty (re-)configures routers',
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -52,7 +69,7 @@ def main():
|
|
|
|
parser_check.set_defaults( func = monty_check )
|
|
|
|
parser_check.set_defaults( func = monty_check )
|
|
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
args = parser.parse_args()
|
|
|
|
args.router = connect_routers( args.router )
|
|
|
|
args.router = connect_routers( config = config.get( 'router', {} ), args = args.router )
|
|
|
|
|
|
|
|
|
|
|
|
args.func( args )
|
|
|
|
args.func( args )
|
|
|
|
|
|
|
|
|
|
|
|