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() ) ) ) rvalbits.append( "%s=%s" % ( k, ",".join( "%s:%s" % vi for vi in v.items() ) ) )
else: else:
rvalbits.append( "%s=%s" % ( k, v ) ) rvalbits.append( "%s=%s" % ( k, v ) )
if selector == hostname: if selector == hostname or selector is None:
return "%s [%s]" % ( hostname, " ".join( rvalbits ) ) return "%s [%s]" % ( hostname, " ".join( rvalbits ) )
else: else:
return "%s [%s] %s" % ( hostname, " ".join( rvalbits ), selector ) 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 kwargs[ 'ssl_wrapper' ] = ssl_ctx.wrap_socket
return librouteros.connect( **kwargs ) 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 ): def connect_routers( config, args ):
routers = config.get( 'router', {} ) routers = config.get( 'router', {} )
rval = {} rval = {}
with progress.bar.PixelBar( 'Connecting', max = len( routers ) ) as bar: with progress.bar.PixelBar( 'Connecting', max = len( routers ) ) as bar:
for ( name, kwargs ) in routers.items(): for ( name, options ) in routers.items():
if ( not args.only ) or ( name in args.only ): 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: if 'host' not in kwargs:
kwargs[ 'host' ] = name kwargs[ 'host' ] = name
rval[ name ] = connection( **kwargs ) rval[ name ] = connection( **kwargs )
@ -138,10 +148,10 @@ def monty_fixup( config, args, routers ):
while rev.endswith( "." ): while rev.endswith( "." ):
rev = rev[ :-1 ] rev = rev[ :-1 ]
reverse[ item[ 'interface' ] ] = rev reverse[ item[ 'interface' ] ] = rev
sel = rev sel = reverse[ item[ 'interface' ] ]
new_comment = make_comment( sel, rval, hst ) new_comment = make_comment( sel, rval, hst )
if args.dry_run: 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: else:
print( name, ":", item[ 'interface' ], new_comment ) print( name, ":", item[ 'interface' ], new_comment )
ip_address.update( **{ 'comment': new_comment, '.id': item[ '.id' ] } ) 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' ] ) hst = reverse.get( item[ 'interface' ], "XXX " + item[ 'interface' ] )
new_comment = make_comment( sel, rval, hst ) new_comment = make_comment( sel, rval, hst )
if args.dry_run: 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: else:
print( name, ":", item[ 'interface' ], new_comment ) print( name, ":", item[ 'interface' ], new_comment )
ipv6_address.update( **{ 'comment': new_comment, '.id': item[ '.id' ] } ) ipv6_address.update( **{ 'comment': new_comment, '.id': item[ '.id' ] } )
@ -184,7 +194,8 @@ def main():
) )
subparsers = parser.add_subparsers() 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.add_argument( '-n', '--dry-run', action = 'store_true' )
parser_check = subparsers.add_parser( 'check' ) parser_check = subparsers.add_parser( 'check' )

Loading…
Cancel
Save