Just a feeler question/skills check. For those more exposed to networking than systems, at what capacity do you do any python/perl/bash scripting? What sort of tasks do you script for?
astorrsDrops by now and againMemberPosts: 3,139■■■■■■□□□□
- When it's a task that's going to be repeated again and again (especially if performed by different people) to ensure it's always done the same way
- When the time to script it is <= the time to do it manually
Our network engineers do little to no scripting and are very Windows friendly. We on the systems side end up scripting everything and have a very good grasp of the network.
The smartest network engineers I know can script just as well as I can as a Linux Admin (Python / Perl, Bash, etc.). Those folks are worth their weight in gold and are paid accordingly.
ETA: scripting is called for when repetitive tasks are found. That can happen as little as once a week or as often as daily. I think the answer is largely dependant on the engineer's scripting ability and the demand within the network. Or how lazy the person is!
Work in progress: picking up Postgres, elastisearch, redis, Cloudera, & AWS. Next up: eventually the RHCEand to start blogging again. Control Protocol; my blog of exam notes and IT randomness
Depends on what I'm doing, however it's usually shooting out show commands to multiple switches in which case it looks something like;
cat <file with switchnames>| while read line; do <ssh script for auto login> -c "show <insert what to show>" >> $line.log; done
or if you configure you'd need to shoot multiple commands in most cases;
while read line; do <ssh script> <<EOF;configure; set <w/e>;EOF;done <file with names
can also be done via
while read line; do <ssh script> -c "configure; set <w/e>;";done <file with names
In this case I'm pretty luck and already have files with name of all the switches, however seeing it's both outofband and inband switches you have to edit them with awk/sed. SSH script is done by expect.
It's early in the morning and written by hand so there might be some errors
As of right now I'm trying to learn python.
Im starting to play around with bash scripts for use with the net-snmp library. I did a lot of vb in college so at least some of the underlying concepts are the same i.e. loops, variables, etc..
Can someone send me a link on the scripting used with Cisco Switches ?? I'm very interested in this. I've had some experience on the server side with powershell,vbs, and batch files. Speeding up the process of finding popped ports or a mac address on switch would be great. Also I often have to change port configs from one access vlan to another and setup voice vlan with priority. Thanks!!
Just to chime in, Cmdlets are no bore if you're working primarily in a Windows-centric role. Cmdlets have saved me a lot of time compared to writing equivalent commands in batch or vbscript or using GUI tools. It's literally five times as much work at a minimum to do a shell or WMI call in vbscript than to use a cmdlet that's designed to do what you need the call for in vbscript, and cmdlets cover about 75% of what you would do (maybe 80% in 3.0). Easier, simpler syntax and clearer debugging means almost everything is faster to write. Of the major changes in PowerShell, aliasing to Linux commands is probably the least interesting or important for anyone not from a Linux background. Even for people from a Linux background, it's not like BASH really translates to PowerShell. A few simple commands are aliased, but the platforms are still very different.
Anyway, as far as scripted network device configuration goes, I would probably use PowerShell if using Windows and Python if not, or just Python if I didn't know PowerShell. Bash would be a third choice.
Astorr's answer is the best pretty much regardless of the context of the question. Whether it's systems or networking, those are about the only criteria to consider.
I do script on the daily basic using python. I used it for creating reports. Let say that we want to know if we have the standard IOS on all of our switches (more than 1k switches). I do a python script that will go and ssh into each device and will grab the IOS version and save all that info in an spreadsheet. Also, if we have a project, I create a pre and post script to audit everything that was touch. Is pretty cool especially when you work on a big company, you see different department fighting for your time.
I would post later on. I use couple of python module. I started learning python like just 6 month ago. I'm now working on making my scripts look better because my scripts work, bu they are ugly. I just need more experience.
Rarely. If it's about automating a repetitive task, there's usually someone I can delegate it to, and have it done tomorrow. I am glad I have expert scripting skills, so I know when it can help, for when I need it now, and so I can check those engineers' work!
Here is the code of the main script, but is better if you download the script from github because there is more files that it needs and the format get messup in here:
I also put the code on my blog: ipnet.tumblr.com
#!/usr/bin/python
# Version 1.0
import os,sys,string
sys.path.append('/home/user/python/lib/lib/python')
import pexpect
import xlwt
import credential
os.system('rm Devices_Config_Pull.xls') #This will remove previews created spreadsheet
os.system('rm Devices_Config_Pull.zip') #This will remove previews created zip
list_devices=open('list.txt').readlines() #This will read the list of devices that file is on the same location as the script
email, UID, passwd, enable = credential.cred() #This is a module that I created that will store all of credential
connection = sys.argv[1]
connection = int(connection)
### If the argument is 1 it will telnet into the devices if the argument is 2 it will ssh into the devices
if connection == 1:
tunnel = 'telnet '
conmess = 'telnet'
elif connection == 2:
tunnel = ('ssh -o StrictHostKeyChecking=no -l ' + UID + ' ')
conmess = 'SSH'
######## This for loop block. is the loop for telnet/ssh into the devices
wbk = xlwt.Workbook() #Create an excel workbook
font = xlwt.Font()
font.bold = True
style = xlwt.XFStyle()
style.font = font
for i in list_devices:
rowHostname = 0
rowIp = 1
rowTittle = 2
rowOutput = 4
col1 = 0
IP = ""
IP2 = ""
x = i.strip().split()
if len(x) == 2:
r = 0
else:
r =1
while r<3:
if r == 1:
hostname, IP,IP2 = i.strip().split()
print r
print IP
print IP2
print hostname
sheet = wbk.add_sheet(hostname)
f_write = file (hostname+'_Device_1.txt','wb')
sheet.write(rowHostname,0,hostname+"_Device_1", style)
sheet.write(rowIp,0,"IP: "+IP, style)
sheet.write(rowTittle,0,"Config", style)
elif r == 2:
IP = IP2
col1 = 1
sheet.write(rowHostname,1,hostname+"_Device_2", style)
f_write = file (hostname+'_Device_2.txt','wb')
sheet.write(rowIp,1,"IP: "+IP, style)
sheet.write(rowTittle,1,"Config", style)
elif r == 0:
hostname, IP = i.strip().split()
print r
print IP
print IP2
print hostname
sheet = wbk.add_sheet(hostname)
f_write = file (hostname+'_Device_1.txt','wb')
sheet.write(rowHostname,0,hostname+"_Device_1", style)
sheet.write(rowIp,0,"IP: "+IP, style)
sheet.write(rowTittle,0,"Config", style)
r=3
r+=1
print "connecting to " + IP +" " +hostname
print 'trying to ' + conmess +' ' + IP
child = pexpect.spawn(tunnel + IP)
m = child.expect (['assword:','[Ll]ogin:','[Uu]sername',pexpect.TIMEOUT,pexpect.EOF])
if m==0:
child.sendline(passwd)
elif m==1:
child.sendline(UID)
elif m==2:
child.sendline(UID)
elif m==3:
print "login error"
continue
elif m==4:
print "no login prompt error"
continue
q = child.expect (['>','[Pp]assword',pexpect.TIMEOUT,pexpect.EOF])
if q==0:
child.sendline ('ena')
elif q==1:
child.sendline (passwd)
elif q==2:
print "wrong password or wait too long for prompt"
sheet.write(rowOutput,col1,'Wrong Password')
rowOutput+=1
continue
elif q==3:
sheet.write(rowOutput,col1,'Prompt Timeouts')
rowOutput+=1
continue
q = child.expect (['assword:','>',pexpect.TIMEOUT,pexpect.EOF])
if q==0:
child.sendline (enable)
elif q==1:
child.sendline ('enable')
elif q==2:
sheet.write(rowOutput,col1,'Wrong Password')
rowOutput+=1
continue
elif q==3:
sheet.write(rowOutput,col1,'Prompt Timeout')
rowOutput+=1
continue
q = child.expect (['>','[Pp]assword',pexpect.TIMEOUT,pexpect.EOF, '#'])
if q==0:
child.sendline ('ena')
elif q==1:
child.sendline (enable)
elif q==2:
sheet.write(rowOutput,col1,'Wrong Password')
rowOutput+=1
continue
elif q==3:
sheet.write(rowOutput,col1,'Prompt Timeout')
rowOutput+=1
continue
elif q==4:
child.sendline(' ')
q = child.expect (['>','[Pp]assword',pexpect.TIMEOUT,pexpect.EOF, '#'])
if q==0:
sheet.write(rowOutput,col1,'Wrong Enable Password')
rowOutput+=1
continue
elif q==1:
sheet.write(rowOutput,col1,'Wrong Enable Password')
rowOutput+=1
continue
elif q==2:
sheet.write(rowOutput,col1,'Wrong Password')
rowOutput+=1
continue
elif q==3:
sheet.write(rowOutput,col1,'Prompt Timeout')
rowOutput+=1
continue
elif q==4:
child.sendline(' ')
print 'Im OK'
child.expect('#')
child.sendline ('show run | in hostname') #For grabbing the device histname
child.expect('#')
host=child.before
join =host.split(" ")
host = join[5]
join1=host.splitlines()
host = join1[0]
child.sendline ('term len 0')
print 'pass term'
# This is the end of the process to ssh/telnet into the devices
#Here start to grab the show run information
command = 'sh run'
q = child.expect ([host+'#',pexpect.TIMEOUT,pexpect.EOF])
if q==0:
child.logfile_read = f_write
child.sendline (command)
elif q==1:
print "command not working " + i
continue
elif q==2:
print "command not working" + i
continue
command = 'show clock'
q = child.expect ([host+'#',pexpect.TIMEOUT,pexpect.EOF])
if q==0:
config = child.before
config = config.split("\n")
rowOutput = 4
for i in config:
sheet.write(rowOutput,col1,i)
rowOutput+=1
child.sendline (command)
elif q==1:
print "command not working " + i
continue
elif q==2:
print "command not working" + i
continue
child.expect('#')
f_write.close()
child.sendline('exit')
sheet.col(0).width = 6000
sheet.col(1).width = 6000
wbk.save('Devices_Config_Pull.xls') #Saving the spreadsheet
os.system('zip Devices_Config_Pull Devices_Config_Pull.xls') #Send this command to the OS, in my case I use linux
os.system('uuencode Devices_Config_Pull.zip Devices_Config_Pull.zip | mailx -s "Devices Configs - Spreadsheet" ' +email) #Again using the OS Command to send an email with an attachemt
print ('Sending the zip file to ' +email+'.Please check your email in couple of minutes')
Comments
CUCM SRND 9x/10, UCCX SRND 10x, QOS SRND, SIP Trunking Guide, anything contact center related
- When the time to script it is <= the time to do it manually
profile: linkedin.com/in/astorrs
The smartest network engineers I know can script just as well as I can as a Linux Admin (Python / Perl, Bash, etc.). Those folks are worth their weight in gold and are paid accordingly.
ETA: scripting is called for when repetitive tasks are found. That can happen as little as once a week or as often as daily. I think the answer is largely dependant on the engineer's scripting ability and the demand within the network. Or how lazy the person is!
Next up: eventually the RHCE and to start blogging again.
Control Protocol; my blog of exam notes and IT randomness
Can you post a few examples?
cat <file with switchnames>| while read line; do <ssh script for auto login> -c "show <insert what to show>" >> $line.log; done
or if you configure you'd need to shoot multiple commands in most cases;
while read line; do <ssh script> <<EOF;configure; set <w/e>;EOF;done <file with names
can also be done via
while read line; do <ssh script> -c "configure; set <w/e>;";done <file with names
In this case I'm pretty luck and already have files with name of all the switches, however seeing it's both outofband and inband switches you have to edit them with awk/sed. SSH script is done by expect.
It's early in the morning and written by hand so there might be some errors
As of right now I'm trying to learn python.
http://cli.learncodethehardway.org/book/
I like how Powershell commands are aliased to linux commands (MS did something right!), but those dern cmdlets are a bore :yawn:
Anyway, as far as scripted network device configuration goes, I would probably use PowerShell if using Windows and Python if not, or just Python if I didn't know PowerShell. Bash would be a third choice.
Astorr's answer is the best pretty much regardless of the context of the question. Whether it's systems or networking, those are about the only criteria to consider.
Complete: 55/120 credits SPAN 201, LIT 100, ETHS 200, AP Lang, MATH 120, WRIT 231, ICS 140, MATH 215, ECON 202, ECON 201, ICS 141, MATH 210, LING 111, ICS 240
In progress: CLEP US GOV,
Next up: MATH 211, ECON 352, ICS 340
http://gomezd.com <
My Tshoot test Blog
http://twitter.com/ipnet255
http://gomezd.com <
My Tshoot test Blog
http://twitter.com/ipnet255
Laziness invented scripting.
https://github.com/amb1s1/config_pull
Here is the code of the main script, but is better if you download the script from github because there is more files that it needs and the format get messup in here:
I also put the code on my blog: ipnet.tumblr.com
http://gomezd.com <
My Tshoot test Blog
http://twitter.com/ipnet255
Cisco IOS Scripting with TCL Configuration Guide, Cisco IOS Release 12.4T - Cisco IOS Scripting with Tcl [Support] - Cisco Systems
Amazon.com: TcL Scripting for Cisco IOS (Networking Technology) (9781587059452): Raymond Blair, Arvind Durai, John Lautmann: Books
http://www.cisco.com/en/US/docs/ios-xml/ios/ios_tcl/configuration/15-sy/ios-tcl-15-sy-book.pdf