Tratto London (Revision): Escalate Privileges

Two years ago I wrote a script that you could use to send commands to telnet/ssh enabled devices called tratto.  Since then I have changed jobs and with new jobs comes new enviornments.  I can no longer send commands like "show run" without sending the enable command (enable is like the 'su' of unix).  I re-visited my code... which is always an entertaining.  Anyway, I added an "escalateprivileges" command and added a string to the Systems object so you can store what the escalation command is for different operating systems.

You can download it here or via github

Connectivity.py

        def escalateprivileges(self, escalated_password=None):
                escalated_password = escalated_password
                if self.connected:
                        self.connection.sendline(self.operatingsystem.ESCALATE_COMMAND)
                        i = self.connection.expect(r"(?i)password[\s:]+")
                        if i==0:
                                self.connection.sendline(escalated_password)
                                i = self.connection.expect(self.operatingsystem.PROMPTLINE)
                                if i==0:
                                        if("denied" in self.connection.before):
                                           print "***Escalation FAILED***"
                                           print self.connection.before
                                        else:
                                           print "***Escalation Successful***"
                else:
                        raise SessionError("***Not Connected***")

Systems.py

class OperatingSystem(object):
        ESCALATE_COMMAND=''
        PAGINATES       =False
        VERSION         =''
        PROMPTLINE      =''

..

class CiscoIOS(OperatingSystem):
        '''cisco ios'''
        PROMPTLINE      = r'[-\w]+[>#]'
        GET_CONFIG      ='show running-config'
        PAGINATES       =True
        VERSION         ='show version'
        DISABLE_PAGINATION = 'terminal length 0'
        ESCALATE_COMMAND='enable'
...

Example usage:

#!/usr/bin/env python

import Connectivity
import Systems

#telnet to a cisco switch

m = Systems.OperatingSystems['IOS']
s = Connectivity.Session("10.10.1.1",23,"telnet",m)
s.login("akonkol", "mypassword")
s.escalateprivileges('myenablepassword')
#s.sendcommand("show ver")
s.sendcommand("show clock")
s.sendcommand("show run")
s.sendcommand("show start")
s.logout()
Tagged as cisco , pexpect python , ssh telnet , tratto
Written by Andrew Konkol on April 16th, 2012

0 Comments

Log in with Twitter, Google, Facebook, LinkedIn to leave a comment.