├── CLI.py ├── Comands ├── GUI.py ├── Hosts ├── LICENSE ├── README.md ├── bcolors.py ├── help.py ├── sshing.py ├── validating_files.py └── writelog.py /CLI.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | import os 4 | import help 5 | import sshing 6 | import writelog 7 | import validating_files 8 | import bcolors 9 | 10 | ####################################################################### 11 | # FUNCTION - CREATE FOLDER IF IS DOESNT EXIST 12 | def createFolders(logpath): 13 | if not os.path.exists(logpath):os.makedirs(logpath) 14 | 15 | ####################################################################### 16 | # MAIN 17 | def main(): 18 | local = 'cli' 19 | logpath = './logpath/' 20 | createFolders(logpath) 21 | hostip=validating_files.validate_hostfile(sys.argv[1]) 22 | commands=validating_files.validate_commandfile(sys.argv[2]) 23 | for IP in hostip: 24 | tolog=sshing.sshing(IP,commands,local) 25 | writelog.writelog(tolog[1],tolog[0],logpath) 26 | print bcolors.bcolors.OKGREEN + "scritp ended" + bcolors.bcolors.ENDC 27 | 28 | ####################################################################### 29 | # ACCESS TO MAIN 30 | if __name__ == "__main__": 31 | if len(sys.argv) < 3 or len(sys.argv) > 3 : 32 | help.help() 33 | else: 34 | main() 35 | -------------------------------------------------------------------------------- /Comands: -------------------------------------------------------------------------------- 1 | terminal lenght 0 2 | show running-config 3 | (...) 4 | -------------------------------------------------------------------------------- /GUI.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | import os 4 | import sshing 5 | import writelog 6 | import validating_files 7 | import gooey 8 | from gooey import GooeyParser 9 | 10 | ####################################################################### 11 | # FUNCTION - CREATE FOLDER IF IS DOESNT EXIST 12 | def createFolders(logpath): 13 | if not os.path.exists(logpath):os.makedirs(logpath) 14 | 15 | ####################################################################### 16 | # Main Fuction 17 | @gooey.Gooey(program_name='SSH Cisco Config') 18 | def main(): 19 | local = 'gui' 20 | parser = GooeyParser(description='Script for changing devices configurations') 21 | parser.add_argument('hostfile',help='File with hosts ID\'s', widget='FileChooser') 22 | parser.add_argument('comandfile',help='File with command\'s to execute on hosts', widget='FileChooser') 23 | parser.add_argument('logpath',help='Path where it will be saved the log\'s from SSH sessions') 24 | args = parser.parse_args() 25 | createFolders(args.logpath) 26 | hostid=validating_files.validate_hostfile(args.hostfile) 27 | commands=validating_files.validate_commandfile(args.comandfile) 28 | for IP in hostid: 29 | tolog=sshing.sshing(IP,commands,local) 30 | writelog.writelog(tolog[1],tolog[0],args.logpath) 31 | scriptend = "scritp ended" 32 | print "\n" + scriptend.center(150, ' ') 33 | 34 | 35 | ####################################################################### 36 | # ACCESS TO MAIN 37 | if __name__ == "__main__": 38 | main() 39 | -------------------------------------------------------------------------------- /Hosts: -------------------------------------------------------------------------------- 1 | 192.168.111.0 2 | 192.168.112.0 3 | 192.168.141.0 4 | 5 | 192.168.113.0 6 | 192.168.111.6 7 | 192.168.111.12 8 | 9 | 10 | 11 | 12 | 13 | 14 | 192.168.115.1 15 | 16 | 192.168.111. 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Hugo Marques (hhugomarques@gmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SSH-Cisco-Config 2 | 3 | - Basic Script with GUI or on CLI for SSH to Cisco Routers or Switches and run some commands on it. 4 | 5 | ### Before use: 6 | 7 | - Python Version 2 8 | - Needed an hostfile with the name of the device (EX:192.68.2.250), see example file called Hosts. 9 | - Needed an configurationfile with the commands to send via ssh to device, see example file called Commands. 10 | - If using GUI.py change lines 10 & 11 of sshing.py to correct user and password, on CLI.py it is asked to user, for each equipment. 11 | 12 | ### Modules used: 13 | 14 | - re 15 | - sys 16 | - os 17 | - [gooey] 18 | - [wxpython] for [gooey] 19 | - getpass 20 | - pexpect 21 | - from pexpect used pxssh 22 | 23 | ### Help for wxpython: 24 | 25 | - After Gooey is installed, make sure [wxPython] is installed on your machine as well. Unfortanately, this cannot be done from the CLI and should be manually downloaded from the [wxPython website]. 26 | 27 | ### To use: 28 | 29 | There are two ways to run this script one is form Shell or with a GUI : 30 | 31 | ```sh 32 | $ python CLI.py 33 | or 34 | $ python GUI.py 35 | ``` 36 | 37 | Or simply double click on GUI.py 38 | 39 | ### To do: 40 | - Create hide textbox for password on gooey 41 | - Add textbox for Username and Password on gooey 42 | 43 | 44 | 45 | License 46 | ---- 47 | 48 | MIT 49 | 50 | 51 | **USE IT ON YOUR OWN RISK !** 52 | 53 | 54 | [gooey]: 55 | [wxPython]: 56 | [wxPython website]: 57 | -------------------------------------------------------------------------------- /bcolors.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # ONLY Colors 3 | class bcolors: 4 | OKGREEN = '\033[92m' 5 | FAIL = '\033[91m' 6 | ENDC = '\033[0m' 7 | -------------------------------------------------------------------------------- /help.py: -------------------------------------------------------------------------------- 1 | import bcolors 2 | import os 3 | 4 | ####################################################################### 5 | # HELP MENU 6 | def help(): 7 | os.system("clear") 8 | print "#####################################################################" 9 | print "# #" 10 | print "#" + bcolors.bcolors.FAIL+ " Please use: CLI.py " + bcolors.bcolors.ENDC + " #" 11 | print "# #" 12 | print "# hhugomarques@gmail.com #" 13 | print "#####################################################################" 14 | -------------------------------------------------------------------------------- /sshing.py: -------------------------------------------------------------------------------- 1 | import bcolors 2 | import getpass 3 | import pexpect 4 | from pexpect import pxssh 5 | 6 | ####################################################################### 7 | # SSHING 8 | def sshing(host,commands,local): 9 | if local == 'gui': 10 | user = 'Username' 11 | pswd = 'Password' 12 | elif local == 'cli': 13 | user = raw_input('Username: ') 14 | pswd = getpass.getpass('Password: ') 15 | tout = 360 16 | failed = 0 17 | output_string='' 18 | try: 19 | ssh = pxssh.pxssh() 20 | ssh = pxssh.pxssh(options={ 21 | "StrictHostKeyChecking": "no", 22 | "UserKnownHostsFile": "/dev/null"}) 23 | ssh.login( host, user, pswd, auto_prompt_reset=False) 24 | ssh.sendline('\n') 25 | ssh.expect('#', timeout=tout) 26 | h = ssh.before 27 | hostname = h.lstrip() 28 | output_string+= ssh.before 29 | for command in commands: 30 | ssh.sendline(command) 31 | ssh.expect('#', timeout=tout) 32 | output_string+= '#' + ssh.before 33 | if local == 'gui':print "Device " + hostname + " configured." 34 | elif local == 'cli':print bcolors.bcolors.OKGREEN + "Device " + hostname + " configured." + bcolors.bcolors.ENDC 35 | return (output_string,hostname) 36 | except pxssh.ExceptionPxssh as e: 37 | output_string='' 38 | if local == 'gui':print "pxssh failed to login to : " + host + " ." 39 | elif local == 'cli':print bcolors.bcolors.FAIL + "pxssh failed to login to : " + host + " ." + bcolors.bcolors.ENDC 40 | output_string += str(e) 41 | return (output_string,host) 42 | -------------------------------------------------------------------------------- /validating_files.py: -------------------------------------------------------------------------------- 1 | import re 2 | import sys 3 | import os 4 | 5 | ####################################################################### 6 | # Validation of commandfile 7 | def validate_commandfile(commandfile): 8 | command = [] 9 | try: 10 | for line in open(commandfile, 'r'): 11 | isline = re.search("(.*)\n$",line) 12 | if isline:command.append(isline.group(1)) 13 | else:command.append(line) 14 | 15 | except: 16 | print " Cannot read the provided file " 17 | return command 18 | 19 | ####################################################################### 20 | # Validation of hostfile 21 | def validate_hostfile(hostfile): 22 | host = [] 23 | try: 24 | for line in open(hostfile, 'r'): 25 | hostsearch = re.search("^(\d+\.\d+\.\d+\.\d+).*",line) 26 | if hostsearch:host.append(hostsearch.group(1)) 27 | except: 28 | print " Cannot read the provided file " 29 | return host 30 | -------------------------------------------------------------------------------- /writelog.py: -------------------------------------------------------------------------------- 1 | ####################################################################### 2 | # WRITING LOG to FILE 3 | def writelog(host,write,logpath): 4 | filepath = logpath + host + ".txt" 5 | with open(filepath,"w") as configFile: 6 | for line in write:configFile.write(line) 7 | --------------------------------------------------------------------------------