now supports config file
This commit is contained in:
parent
7fb8ca00dd
commit
7d273d091b
1
Pipfile
1
Pipfile
@ -8,6 +8,7 @@ verify_ssl = true
|
|||||||
[packages]
|
[packages]
|
||||||
librouteros = "==2.*"
|
librouteros = "==2.*"
|
||||||
progress = "*"
|
progress = "*"
|
||||||
|
pyyaml = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.8"
|
python_version = "3.8"
|
||||||
|
25
automonty
25
automonty
@ -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:
|
||||||
|
for router in args:
|
||||||
rval[ router ] = connection( router )
|
rval[ router ] = connection( router )
|
||||||
bar.next()
|
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 )
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user