can now specify --only-router and --only-group

master
Marek Isalski 4 years ago
parent 851f2357de
commit 5b79ea5157

@ -60,7 +60,7 @@ def make_comment( selector, rval, hostname ):
rvalbits.append( "%s=%s" % ( k, ",".join( "%s:%s" % vi for vi in v.items() ) ) )
else:
rvalbits.append( "%s=%s" % ( k, v ) )
if selector == hostname:
if selector == hostname or selector is None:
return "%s [%s]" % ( hostname, " ".join( rvalbits ) )
else:
return "%s [%s] %s" % ( hostname, " ".join( rvalbits ), selector )
@ -83,12 +83,22 @@ def connection( host, username = None, password = None, port = 8729 ):
kwargs[ 'ssl_wrapper' ] = ssl_ctx.wrap_socket
return librouteros.connect( **kwargs )
def router_in_groups( options, groups ):
if not groups:
return False
for group in groups:
if group in options.get( 'group', [] ):
return True
return False
def connect_routers( config, args ):
routers = config.get( 'router', {} )
rval = {}
with progress.bar.PixelBar( 'Connecting', max = len( routers ) ) as bar:
for ( name, kwargs ) in routers.items():
if ( not args.only ) or ( name in args.only ):
for ( name, options ) in routers.items():
if ( not args.only_router and not args.only_group ) or ( name in args.only_router ) or router_in_groups( options, args.only_group ):
kwargs = {}
kwargs.update( options.get( 'connection', {} ) )
if 'host' not in kwargs:
kwargs[ 'host' ] = name
rval[ name ] = connection( **kwargs )
@ -138,10 +148,10 @@ def monty_fixup( config, args, routers ):
while rev.endswith( "." ):
rev = rev[ :-1 ]
reverse[ item[ 'interface' ] ] = rev
sel = rev
sel = reverse[ item[ 'interface' ] ]
new_comment = make_comment( sel, rval, hst )
if args.dry_run:
print( item[ 'interface' ], item[ 'address' ], '->', new_comment )
print( '/ip address set [find where interface="%s" and network="%s"] comment="%s"' % ( item[ 'interface' ], item[ 'network' ], new_comment ) )
else:
print( name, ":", item[ 'interface' ], new_comment )
ip_address.update( **{ 'comment': new_comment, '.id': item[ '.id' ] } )
@ -164,7 +174,7 @@ def monty_fixup( config, args, routers ):
hst = reverse.get( item[ 'interface' ], "XXX " + item[ 'interface' ] )
new_comment = make_comment( sel, rval, hst )
if args.dry_run:
print( item[ 'interface' ], item[ 'address' ], '->', new_comment )
print( '/ipv6 address set [find where interface="%s" and address="%s" ] comment="%s"' % ( item[ 'interface' ], item[ 'address' ], new_comment ) )
else:
print( name, ":", item[ 'interface' ], new_comment )
ipv6_address.update( **{ 'comment': new_comment, '.id': item[ '.id' ] } )
@ -184,7 +194,8 @@ def main():
)
subparsers = parser.add_subparsers()
parser.add_argument( '--only', action = 'append' )
parser.add_argument( '--only-router', action = 'append', default = [] )
parser.add_argument( '--only-group', action = 'append', default = [] )
parser.add_argument( '-n', '--dry-run', action = 'store_true' )
parser_check = subparsers.add_parser( 'check' )

Loading…
Cancel
Save