Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import print_function
- from getpass import *
- import argparse, sys
- from pprint import pprint as pp
- # Import Junos pyez libraries
- try:
- from jnpr.junos import Device
- from jnpr.junos.utils.config import Config
- from jnpr.junos.exception import *
- except:
- print('ERROR: junos-eznc module not found!')
- exit()
- #Default settings
- defaults = {
- 'port': '830',
- 'config_file': '',
- 'user': 'ansible',
- 'password': 'Ansible',
- 'ssh_key': ''
- }
- def parse_arguments(arguments):
- parser = argparse.ArgumentParser(description="Configure switch port in specific ")
- parser.add_argument("-d", "--device", help="Device to apply configuration to")
- parser.add_argument("-i", "--interface", help="Interface / switcport")
- parser.add_argument("-v", "--vlans", help="List of vlans")
- parser.add_argument("-m", "--mode", help="Inteface mode: access|trunk")
- parser.add_argument("-u", "--user", help="Username to login to device")
- parser.add_argument("-p", "--password", action='store_true', help="Prompt for password to login to device")
- parser.add_argument("-c", "--confirm", action='store_true', help="Auto confirm configuration changes (No diff review)")
- parser.add_argument("-P", "--port", help="Netconf port to connect on")
- args = parser.parse_args()
- return args
- # Close NETCONF connection to device
- def disconnect(dev):
- print("Closing session with %s" % dev)
- dev.close()
- def main():
- #Process command line arguments
- args = parse_arguments(sys.argv)
- template_variables = {}
- template_variables['interface'] = args.interface
- template_variables['mode'] = args.mode
- template_variables['vlans'] = args.vlans
- #Set Netconf port to connect on
- if args.port:
- port = args.port
- elif defaults['port'] != '':
- port = defaults['port']
- else:
- port = '830'
- #Set user to connect as
- if args.user is True:
- user = args.user
- elif defaults['user'] != '':
- user = defaults['user']
- else:
- user = getuser()
- #Set password for connecting
- if args.password is True:
- password = getpass('Password for %s: ' % user)
- elif defaults['password'] != '':
- password = defaults['password']
- else:
- password = ''
- #Set device to configure
- device = args.device
- print('*** Processing %s ***' % device)
- # Create device instance and connect to device prepped for config
- #dev = Device(device, user=user, password=password, port=port, ssh_private_key_file=defaults['ssh_key'])
- dev = Device(device, user='ansible', password='Ansible', port=port, ssh_private_key_file=defaults['ssh_key'])
- #Make connection to device
- try:
- dev.open()
- except ConnectError:
- print("ERROR: Unable to connect")
- #Bind config utility instance to device
- dev.bind(cfg=Config)
- #Lock device's config
- print("Locking configuration")
- try:
- dev.cfg.lock()
- except LockError:
- print('ERROR: Failed to lock configuration')
- disconnect(dev)
- exit()
- if args.mode and args.vlans:
- template = 'set interfaces %s unit 0 family ethernet-switching port-mode %s vlan members %s' % args.interface, args.mode, args.vlans
- print(template)
- #dev.cfg.load(path='interface.txt', merge=True)
- dev.cfg.load(template_path='junos-templates/interface.j2', format='text', template_vars=template_variables)
- dev.cfg.pdiff()
- dev.cfg.commit()
- disconnect(dev)
- if __name__ == "__main__":
- main()
- ~
Advertisement
Add Comment
Please, Sign In to add comment