now supports config file

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

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

@ -5,6 +5,7 @@ import librouteros
import progress.bar
import ssl
import os
import yaml
def connection( host, username = None, password = None, port = 8729 ):
if ':' in host:
@ -24,12 +25,20 @@ def connection( host, username = None, password = None, port = 8729 ):
kwargs[ 'ssl_wrapper' ] = ssl_ctx.wrap_socket
return librouteros.connect( **kwargs )
def connect_routers( routers ):
def connect_routers( config, args ):
rval = {}
with progress.bar.PixelBar( 'Connecting', max = len( routers ) ) as bar:
for router in routers:
rval[ router ] = connection( router )
bar.next()
if args:
with progress.bar.PixelBar( 'Connecting', max = len( args ) ) as bar:
for router in args:
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
def monty_check( args ):
@ -39,7 +48,15 @@ def monty_check( args ):
if item[ 'network' ] == addr:
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():
config = load_configuration()
parser = argparse.ArgumentParser( prog = "automonty",
description = 'AutoMonty (re-)configures routers',
)
@ -52,7 +69,7 @@ def main():
parser_check.set_defaults( func = monty_check )
args = parser.parse_args()
args.router = connect_routers( args.router )
args.router = connect_routers( config = config.get( 'router', {} ), args = args.router )
args.func( args )

Loading…
Cancel
Save