danhoutz

Pyez Set Interface Vlan code

Mar 20th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4. from getpass import *
  5. import argparse, sys
  6. from pprint import pprint as pp
  7.  
  8. # Import Junos pyez libraries
  9. try:
  10. from jnpr.junos import Device
  11. from jnpr.junos.utils.config import Config
  12. from jnpr.junos.exception import *
  13. except:
  14. print('ERROR: junos-eznc module not found!')
  15. exit()
  16.  
  17. #Default settings
  18. defaults = {
  19. 'port': '830',
  20. 'config_file': '',
  21. 'user': 'ansible',
  22. 'password': 'Ansible',
  23. 'ssh_key': ''
  24. }
  25.  
  26. def parse_arguments(arguments):
  27. parser = argparse.ArgumentParser(description="Configure switch port in specific ")
  28. parser.add_argument("-d", "--device", help="Device to apply configuration to")
  29. parser.add_argument("-i", "--interface", help="Interface / switcport")
  30. parser.add_argument("-v", "--vlans", help="List of vlans")
  31. parser.add_argument("-m", "--mode", help="Inteface mode: access|trunk")
  32. parser.add_argument("-u", "--user", help="Username to login to device")
  33. parser.add_argument("-p", "--password", action='store_true', help="Prompt for password to login to device")
  34. parser.add_argument("-c", "--confirm", action='store_true', help="Auto confirm configuration changes (No diff review)")
  35. parser.add_argument("-P", "--port", help="Netconf port to connect on")
  36. args = parser.parse_args()
  37. return args
  38.  
  39. # Close NETCONF connection to device
  40. def disconnect(dev):
  41. print("Closing session with %s" % dev)
  42. dev.close()
  43.  
  44. def main():
  45. #Process command line arguments
  46. args = parse_arguments(sys.argv)
  47.  
  48. template_variables = {}
  49. template_variables['interface'] = args.interface
  50. template_variables['mode'] = args.mode
  51. template_variables['vlans'] = args.vlans
  52.  
  53. #Set Netconf port to connect on
  54. if args.port:
  55. port = args.port
  56. elif defaults['port'] != '':
  57. port = defaults['port']
  58. else:
  59. port = '830'
  60.  
  61. #Set user to connect as
  62. if args.user is True:
  63. user = args.user
  64. elif defaults['user'] != '':
  65. user = defaults['user']
  66. else:
  67. user = getuser()
  68.  
  69. #Set password for connecting
  70. if args.password is True:
  71. password = getpass('Password for %s: ' % user)
  72. elif defaults['password'] != '':
  73. password = defaults['password']
  74. else:
  75. password = ''
  76.  
  77. #Set device to configure
  78. device = args.device
  79.  
  80. print('*** Processing %s ***' % device)
  81.  
  82. # Create device instance and connect to device prepped for config
  83. #dev = Device(device, user=user, password=password, port=port, ssh_private_key_file=defaults['ssh_key'])
  84. dev = Device(device, user='ansible', password='Ansible', port=port, ssh_private_key_file=defaults['ssh_key'])
  85.  
  86. #Make connection to device
  87. try:
  88. dev.open()
  89. except ConnectError:
  90. print("ERROR: Unable to connect")
  91.  
  92. #Bind config utility instance to device
  93. dev.bind(cfg=Config)
  94.  
  95. #Lock device's config
  96. print("Locking configuration")
  97. try:
  98. dev.cfg.lock()
  99. except LockError:
  100. print('ERROR: Failed to lock configuration')
  101. disconnect(dev)
  102. exit()
  103.  
  104. if args.mode and args.vlans:
  105. template = 'set interfaces %s unit 0 family ethernet-switching port-mode %s vlan members %s' % args.interface, args.mode, args.vlans
  106.  
  107. print(template)
  108.  
  109. #dev.cfg.load(path='interface.txt', merge=True)
  110. dev.cfg.load(template_path='junos-templates/interface.j2', format='text', template_vars=template_variables)
  111.  
  112. dev.cfg.pdiff()
  113.  
  114. dev.cfg.commit()
  115.  
  116. disconnect(dev)
  117.  
  118. if __name__ == "__main__":
  119. main()
  120. ~
Advertisement
Add Comment
Please, Sign In to add comment