本文整理匯總了Python中router.Router.get_config方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.get_config方法的具體用法?Python Router.get_config怎麽用?Python Router.get_config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類router.Router
的用法示例。
在下文中一共展示了Router.get_config方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: main
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import get_config [as 別名]
def main():
# Parse argment
parser = ArgumentParser(
description='Collect configuration files on mutiple routers')
parser.add_argument('-i', '--inventory',
type=str,
default='/etc/configcollector/inventory.json',
help='router information file\n\
(default=etc/configcollector/inventory.json)')
parser.add_argument('-o', '--output',
type=str,
default='./',
help='output directory\n\
(default=CURRENT_DIRECTORY)')
args = parser.parse_args()
# Read router infomation file
try:
with open(args.inventory, 'r') as file:
router_info_json = file.read()
except (IOError, IndexError):
sys.stderr.write('Cannot open file : ' + args.inventory + '\n')
sys.exit(1)
# Convert json format to dictionary
try:
router_info = json.loads(router_info_json)
except ValueError as error:
sys.stderr.write('JSON format error : \n')
sys.stderr.write(router_info_json)
sys.stderr.write(str(error))
sys.exit(1)
# Login and get config for each routers
for num in range(len(router_info)):
router = Router(router_info[num])
print 'Accessing router: ' + router_info[num]['hostname'] + '...'
try:
router.login()
output = router.get_config()
except:
sys.stderr.write('SSH connection error\n')
sys.stderr.write(str(router_info[num]) + '\n')
router.logout()
sys.exit(1)
else:
router.logout()
# Open output file written config
try:
output_filename =\
args.output + router_info[num]['hostname'] + '.txt'
print 'Writing output file "' + output_filename + '"...'
except AttributeError:
sys.stderr.write('Cannot read : ' + output_filename + '\n')
sys.exit(1)
# Write output file
try:
with open(output_filename, 'w') as file:
file.write(output)
except:
sys.stderr.write('Cannot open "' + output_filename + '"\n')
file.close()
sys.exit(1)
print 'Success : "' + output_filename + '"!'