Python SSHv2 to Cisco routers for multiple commands

Darrell Newcomb darrell at cenic.org
Wed Mar 11 15:12:42 UTC 2009


It's good to see the list get a little activity.  To share ideas:

The first part of the original post, sending multiple commands, can  
be done easily with clogin (rancid) and a flat file of commands.  As  
well as use the config archive to drive the target device list in a  
few seconds.  For example: clogin -x <file> `grep "pattern in config  
for devices you'd like to change" * | grep -v "devices/things to  
exclude" | grep -v ".new" | awk -F: '{print $1}' | sort | uniq`    
Where the * is any filename/path pattern that sucks up config files  
into grep, this tends to vary most across different environments.


The 2nd part being a copy takes a bit of expect(clogin -s <expect  
file>) to handle the confirmation prompting that IOS sends.   That  
prompt doesn't (at least historically) match what clogin's internal  
expect is looking for after sending the last line so it stops sending  
other lines.


What can be time consuming is doing the front-end logic to handle all  
kinds of edge cases if you're working with larger device sets:
--verification of available space on device
--verification of target path/disk being on the device
--clean-up of manually placed or old files (gets better after more of  
the changes are driven by automation)
--target path/disk variations across device types
--devices/drives that weren't available during the update run

Detecting those ahead of time is IMHO the easiest way to handle most  
cases, but then again this comes from someone who use "old" stuff  
like awk too much.  ;-)


> I'm trying to  be able to issue a string of commands to routers  
> which require a particular sequence;
>
> configure terminal
> hostname blahrouter1
> ip domain-name x.i.z
>
> ip tftp source-interface lo0
> end
>
> copy tftp:/x.y.z.w/my/path/file.txt   flash:

On Mar 11, 2009, at 6:57 AM, Corey Smith wrote:

> Have you looked at pxssh in pexpect?
>
> s = pxssh.pxssh(timeout=5, logfile=logfile)
> s.login(host, username, password, original_prompt=r'(?i)%s#' % host,
> auto_prompt_reset=False)
> s.PROMPT = re.compile(r'(?i)^%s' % host, re.MULTILINE)
> s.sendline('term length 0')
> prompt(s)
> s.sendline('term width 512')
> prompt(s)
> s.sendline('show version')
> prompt(s)
>
> -Corey Smith
>
> Sam Crooks wrote:
>> The SSH aspect for multiple commands is currently tripping me up.
>>
>
> _______________________________________________
> Toolmakers mailing list
> Toolmakers at lists.isc.org
> https://lists.isc.org/mailman/listinfo/toolmakers




More information about the Toolmakers mailing list