Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pdb # Set breakpoints in script for debugging with this line: pdb.set_trace()
- import inspect
- import subprocess
- import re
- import socket
- import sys
- import time
- import os
- import paramiko as pm
- import lib.functions as fn
- import lib.user_functions as ufn
- import lib.ssh_functions as sfn
- from netmiko import ConnectHandler
- import os.path
- from datetime import datetime
- from getpass import getpass
- username = raw_input('Enter your SSH username: ')
- password = getpass()
- with open('./devices_ip/devices_file') as f:
- devices_list = f.read().splitlines()
- with open('./commands_folder/commands_ios_file') as f:
- commands_list_ios = f.read().splitlines()
- with open('./commands_folder/commands_hp_file') as f:
- commands_list_hp = f.read().splitlines()
- with open('./commands_folder/commands_icx_file') as f:
- commands_list_icx = f.read().splitlines()
- #dictionary of R2 parameters, secret is an optional requirement for ConnectHandler
- #secret is actually for enable secret if local user account has no privilege 15
- for devices in devices_list:
- ip_address_of_device = devices
- Cisco_device = {
- 'device_type': 'cisco_ios',
- 'ip': ip_address_of_device,
- 'username': username,
- 'password': password
- }
- ICX_device = {
- 'device_type': 'ruckus_fastiron',
- 'ip': ip_address_of_device,
- 'username': username,
- 'password': password
- }
- hp_device = {
- 'device_type': 'hp_procurve',
- 'ip': ip_address_of_device,
- 'username': username,
- 'password': password
- }
- #this is to prepare for connection and go directly to privilege exec mode for each models type base on the menu entry
- def r2_connect():
- try:
- r2 = ConnectHandler(**Cisco_device)
- r2.enable()
- return r2
- except IOError:
- print("There is a problem connecting to the switches, please check if your switch is online")
- def r3_connect():
- try:
- r3 = ConnectHandler(**ICX_device)
- r3.enable()
- return r3
- except IOError:
- print("There is a problem connecting to the switches, please check if your switch is online")
- def r5_connect():
- try:
- r5 = ConnectHandler(**hp_device)
- r5.enable()
- return r5
- except IOError:
- print("There is a problem connecting to the switches, please check if your switch is online")
- # Loop menu in case invalid option is selected
- while True:
- # Loop menu in case incorrect option is selected, and user declines confirming menu option
- while True:
- print "\n"
- print "------------------ Nettools v2 ------------------ \n"
- print "1) SSH Switches & Push commands"
- print "2) Setup VLAN (Work in progress)"
- print "9) Exit menu\n"
- menuChoice = raw_input("Choose a menu option: ")
- # If menu choice is anything but 9 (exit menu), confirm with user
- if fn.isEmpty(menuChoice):
- print "\nInvalid menu option selected\n"
- mainMenuChoice = ''
- continue
- if menuChoice != '9':
- menuConfirm = raw_input("\nConfirm menu choice of %s (y/n): " % (menuChoice))
- # Check to see if entered value is 'y' or 'n' only
- if menuConfirm != 'y' and menuConfirm != 'n':
- print "\nPlease enter \"y\" or \"n\" only."
- continue
- elif menuConfirm == 'n':
- continue
- elif menuConfirm == 'y':
- break
- # Mainly used for debugging. This should never trigger
- else:
- fn.debugErrorOut('101')
- # Menu option is 9, so break initial menu selection loop
- else:
- break
- # Execute commands based off menu choice
- if menuChoice == '9':
- print "\nExiting script\n"
- sys.exit()
- if menuChoice == '1':
- while True:
- model = raw_input("\nEnter the models type of your switch: " )
- if model != 'HP' and model != 'Cisco' and model != 'ICX':
- print "\nPlease enter HP , Cisco or ICX only. \n"
- continue
- elif model == 'HP':
- r5 = r5_connect()
- output = r5.send_config_set(commands_list_hp)
- r5.disconnect()
- print output
- print "\n Connection has been established and commands has been pushed successfully for your HP Devices...\n"
- break
- elif model == 'Cisco':
- r2 = r2_connect()
- r2.send_command('terminal length 0')
- output = r2.send_config_set(commands_list_ios)
- r2.disconnect()
- print output
- print "\n Connection has been established and commands has been pushed successfully for your Cisco Devices...\n"
- elif model == 'ICX':
- r3 = r3_connect()
- r3.send_command('terminal length 0')
- output = r3.send_config_set(commands_list_icx)
- r3.disconnect()
- print output
- print "\n Connection has been established and commands has been pushed successfully for your ICX Devices...\n"
- break
- if menuChoice == '2':
- while True:
- model = raw_input("\nEnter the models type of your switch: " )
- if model != 'HP' and model != 'Cisco' and model != 'ICX':
- print "\nPlease enter HP , Cisco or ICX only. \n"
- continue
- elif model == 'Cisco':
- vlan_lookup = raw_input("\n Enter the VLAN to base the config on i.e 1000: " )
- vlan = vlan_lookup
- #vlans = re.findall(r'vlan\S\d+(.*)') #Probably a better way to do this
- #if vlan not in vlans:
- # r2 = r2_connect()
- # r2.send_config_set(
- # 'int vlan {vlan}',
- # )
- vlan_creation = raw_input("\n Enter the VLAN you want to add on the switch i.e 1017: ")
- vlan_name = raw_input("\n Enter the name of the VLAN you want to create, no space i.e MARSHA_Associate: ")
- command = [
- 'do show run vlan ' + vlan_lookup,
- 'vlan ' + vlan_creation,
- 'name ' + vlan_name
- ]
- r2 = r2_connect()
- #vlan_config = r2.send_config_set('show run vlan')
- output = r2.send_config_set(command)
- print output
- r2.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement