<s> """<STR_LIT>""" <EOL> import hashlib <EOL> import os <EOL> import re <EOL> import time <EOL> import pexpect <EOL> import gflags <EOL> import logging <EOL> import base_device <EOL> import pexpect_connection <EOL> import push_exceptions as exceptions <EOL> FLAGS = gflags . FLAGS <EOL> gflags . DEFINE_float ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> gflags . DEFINE_float ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> gflags . DEFINE_float ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> gflags . DEFINE_float ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> gflags . DEFINE_float ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> MD5_RE = re . compile ( r'<STR_LIT>' ) <EOL> MINOR_PAUSE = <NUM_LIT> <EOL> INVALID_1 = "<STR_LIT>" <EOL> INVALID_2 = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> INVALID_3 = '<STR_LIT>' <EOL> INVALID_4 = '<STR_LIT>' <EOL> INVALID_5 = '<STR_LIT>' <EOL> INVALID_6_PREFIX = '<STR_LIT>' <EOL> class DeleteFileError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class IosDevice ( base_device . BaseDevice ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . vendor_name = '<STR_LIT>' <EOL> super ( IosDevice , self ) . __init__ ( ** kwargs ) <EOL> self . _success = r'<STR_LIT>' <EOL> def _Connect ( self , username , password = None , ssh_keys = None , <EOL> enable_password = None , ssl_cert_set = None ) : <EOL> _ = enable_password , ssl_cert_set <EOL> self . _connection = pexpect_connection . ParamikoSshConnection ( <EOL> self . loopback_ipv4 , username , password , self . _success , <EOL> timeout = self . timeout_connect , find_prompt = True , ssh_keys = ssh_keys ) <EOL> try : <EOL> self . _connection . Connect ( ) <EOL> self . _DisablePager ( ) <EOL> self . connected = True <EOL> except pexpect_connection . ConnectionError as e : <EOL> self . connected = False <EOL> raise exceptions . ConnectError ( e ) <EOL> except pexpect_connection . TimeoutError as e : <EOL> self . connected = False <EOL> raise exceptions . ConnectError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( self . host , self . loopback_ipv4 , str ( e ) ) ) <EOL> def _Cmd ( self , command , mode = None ) : <EOL> def SendAndWait ( command ) : <EOL> """<STR_LIT>""" <EOL> self . _connection . child . send ( command + '<STR_LIT:\r>' ) <EOL> self . _connection . child . expect ( '<STR_LIT:\r\n>' , timeout = self . timeout_response ) <EOL> self . _connection . child . expect ( self . _connection . re_prompt , <EOL> timeout = self . timeout_response , <EOL> searchwindowsize = <NUM_LIT> ) <EOL> return self . _connection . child . before . replace ( '<STR_LIT:\r\n>' , os . linesep ) <EOL> _ = mode <EOL> command = command . replace ( '<STR_LIT:?>' , '<STR_LIT>' ) <EOL> result = '<STR_LIT>' <EOL> try : <EOL> result = SendAndWait ( command ) <EOL> except pexpect . TIMEOUT as e : <EOL> self . connected = False <EOL> raise exceptions . CmdError ( '<STR_LIT>' % ( e . __class__ , str ( e ) ) ) <EOL> except pexpect . EOF : <EOL> try : <EOL> self . connected = False <EOL> self . _connection . Connect ( ) <EOL> self . _DisablePager ( ) <EOL> self . connected = True <EOL> result = SendAndWait ( command ) <EOL> except pexpect . EOF : <EOL> raise exceptions . CmdError ( '<STR_LIT>' ) <EOL> except pexpect_connection . ConnectionError as e : <EOL> raise exceptions . CmdError ( '<STR_LIT>' % e ) <EOL> except pexpect_connection . TimeoutError as e : <EOL> raise exceptions . CmdError ( '<STR_LIT>' % e ) <EOL> if result and result [ - <NUM_LIT:1> ] == '<STR_LIT:\r>' : <EOL> result = result [ : - <NUM_LIT:1> ] + '<STR_LIT:\n>' <EOL> if ( result . endswith ( INVALID_1 ) or result . endswith ( INVALID_2 ) or <EOL> result . endswith ( INVALID_3 ) or result . endswith ( INVALID_4 ) or <EOL> result . endswith ( INVALID_5 ) or ( <EOL> result . endswith ( '<STR_LIT:\n>' ) and <EOL> result [ result [ : - <NUM_LIT:1> ] . rfind ( '<STR_LIT:\n>' ) + <NUM_LIT:1> : ] . startswith ( <EOL> INVALID_6_PREFIX ) ) ) : <EOL> raise exceptions . CmdError ( '<STR_LIT>' % result ) <EOL> return result <EOL> def _SetConfig ( self , destination_file , data , canary ) : <EOL> if canary : <EOL> raise exceptions . SetConfigCanaryingError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> self . vendor_name ) <EOL> if destination_file not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise exceptions . SetConfigError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % self . vendor_name ) <EOL> result = base_device . SetConfigResult ( ) <EOL> local_digest = hashlib . md5 ( data ) . hexdigest ( ) <EOL> try : <EOL> remote_path = '<STR_LIT>' <EOL> except exceptions . CmdError as e : <EOL> msg = '<STR_LIT>' % e <EOL> logging . error ( msg ) <EOL> raise exceptions . SetConfigError ( msg ) <EOL> remote_tmpfile = '<STR_LIT>' % ( <EOL> remote_path . rstrip ( ) , os . urandom ( <NUM_LIT:8> ) . encode ( '<STR_LIT>' ) ) <EOL> scp = pexpect_connection . ScpPutConnection ( <EOL> self . loopback_ipv4 , <EOL> username = self . _username , <EOL> password = self . _password ) <EOL> try : <EOL> scp . Copy ( data , remote_tmpfile ) <EOL> except pexpect_connection . Error as e : <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' % str ( e ) ) <EOL> try : <EOL> try : <EOL> verify_output = self . _Cmd ( '<STR_LIT>' % remote_tmpfile ) <EOL> match = MD5_RE . search ( verify_output ) <EOL> if match is not None : <EOL> remote_digest = match . group ( <NUM_LIT:1> ) <EOL> else : <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % verify_output ) <EOL> except exceptions . CmdError as e : <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if local_digest != remote_digest : <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( local_digest , remote_digest ) ) <EOL> try : <EOL> self . _connection . child . send ( <EOL> '<STR_LIT>' % ( remote_tmpfile , destination_file ) ) <EOL> pindex = self . _connection . child . expect ( <EOL> [ r'<STR_LIT>' % destination_file , <EOL> r'<STR_LIT>' , <EOL> r'<STR_LIT>' , <EOL> self . _connection . re_prompt ] , <EOL> timeout = self . timeout_act_user ) <EOL> if pindex == <NUM_LIT:0> : <EOL> self . _connection . child . send ( '<STR_LIT:\r>' ) <EOL> try : <EOL> pindex = self . _connection . child . expect ( <EOL> [ r'<STR_LIT>' , <EOL> self . _connection . re_prompt , <EOL> r'<STR_LIT>' <EOL> '<STR_LIT>' ] , <EOL> timeout = self . timeout_act_user ) <EOL> if pindex == <NUM_LIT:0> : <EOL> bad_lines = re . findall ( <EOL> r'<STR_LIT>' , <EOL> self . _connection . child . match . string , <EOL> re . MULTILINE ) <EOL> raise exceptions . SetConfigSyntaxError ( <EOL> '<STR_LIT>' % <EOL> '<STR_LIT:\n>' . join ( bad_lines ) ) <EOL> if pindex == <NUM_LIT:2> : <EOL> self . _connection . child . send ( '<STR_LIT:n>' ) <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' <EOL> % destination_file ) <EOL> except ( pexpect . EOF , pexpect . TIMEOUT ) as e : <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( self . _connection . child . before , self . _connection . child . after ) ) <EOL> elif pindex == <NUM_LIT:2> : <EOL> print "<STR_LIT>" <EOL> raise exceptions . SetConfigError ( '<STR_LIT>' <EOL> '<STR_LIT>' % destination_file ) <EOL> except ( pexpect . EOF , pexpect . TIMEOUT ) as e : <EOL> raise exceptions . SetConfigError ( <EOL> '<STR_LIT>' ) <EOL> if destination_file == '<STR_LIT>' : <EOL> logging . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , self . host , self . loopback_ipv4 ) <EOL> try : <EOL> self . _Cmd ( '<STR_LIT>' ) <EOL> except exceptions . CmdError as e : <EOL> raise exceptions . SetConfigError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( self . host , self . loopback_ipv4 , <EOL> str ( e ) ) ) <EOL> finally : <EOL> try : <EOL> self . _DeleteFile ( remote_tmpfile ) <EOL> except DeleteFileError as e : <EOL> result . transcript = '<STR_LIT>' % str ( e ) <EOL> logging . warn ( result . transcript ) <EOL> return result <EOL> def _DeleteFile ( self , file_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _connection . child . send ( '<STR_LIT:\r>' ) <EOL> self . _connection . child . expect ( '<STR_LIT:\r\n>' , timeout = self . timeout_act_user ) <EOL> self . _connection . child . expect ( self . _connection . re_prompt , <EOL> timeout = self . timeout_act_user , <EOL> searchwindowsize = <NUM_LIT> ) <EOL> self . _connection . child . send ( '<STR_LIT>' % file_name ) <EOL> except pexpect . ExceptionPexpect : <EOL> raise DeleteFileError ( '<STR_LIT>' % <EOL> self . _connection . child ) <EOL> try : <EOL> pindex = self . _connection . child . expect ( <EOL> [ r'<STR_LIT>' , <EOL> r'<STR_LIT>' ] , <EOL> timeout = self . timeout_act_user ) <EOL> if pindex == <NUM_LIT:0> : <EOL> self . _connection . child . send ( '<STR_LIT:\r>' ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . _connection . child . expect ( [ r'<STR_LIT>' ] , <EOL> timeout = self . timeout_act_user ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . _connection . child . send ( '<STR_LIT:\r>' ) <EOL> elif pindex == <NUM_LIT:1> : <EOL> raise DeleteFileError ( '<STR_LIT>' % <EOL> self . _connection . child . match ) <EOL> pindex = self . _connection . child . expect ( [ self . _connection . re_prompt , <EOL> r'<STR_LIT>' ] , <EOL> timeout = self . timeout_act_user ) <EOL> if pindex == <NUM_LIT:1> : <EOL> raise DeleteFileError ( '<STR_LIT>' % <EOL> self . _connection . child . match ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> except pexpect . ExceptionPexpect : <EOL> raise DeleteFileError ( '<STR_LIT>' % <EOL> self . _connection . child ) <EOL> def _GetConfig ( self , source ) : <EOL> try : <EOL> if source in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> result = self . _Cmd ( '<STR_LIT>' % source ) <EOL> else : <EOL> raise exceptions . GetConfigError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not result : <EOL> return exceptions . EmptyConfigError ( '<STR_LIT>' % <EOL> self . host ) <EOL> else : <EOL> return result <EOL> except exceptions . Error as e : <EOL> raise exceptions . GetConfigError ( '<STR_LIT>' % <EOL> ( self . host , str ( e ) ) ) <EOL> def _Disconnect ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> try : <EOL> self . _connection . child . send ( '<STR_LIT>' ) <EOL> self . _connection . child . expect ( self . _connection . exit_list , <EOL> timeout = self . timeout_act_user ) <EOL> self . connected = False <EOL> except ( pexpect . EOF , pexpect . TIMEOUT ) as e : <EOL> self . connected = False <EOL> raise exceptions . DisconnectError ( '<STR_LIT>' % ( e . __class__ , str ( e ) ) ) <EOL> def _DisablePager ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _connection . child . send ( '<STR_LIT:\r>' ) <EOL> self . _connection . child . expect ( r'<STR_LIT:\r\n>' , <EOL> timeout = self . timeout_connect ) <EOL> self . _connection . child . expect ( self . _connection . re_prompt , <EOL> timeout = self . timeout_connect , <EOL> searchwindowsize = <NUM_LIT> ) <EOL> self . _connection . child . send ( '<STR_LIT>' ) <EOL> pindex = self . _connection . child . expect ( <EOL> [ self . _connection . re_prompt , r'<STR_LIT>' ] , <EOL> timeout = self . timeout_connect ) <EOL> if pindex == <NUM_LIT:1> : <EOL> self . connected = False <EOL> raise exceptions . ConnectError ( '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:0.5> ) <EOL> except ( pexpect . EOF , pexpect . TIMEOUT ) as e : <EOL> self . connected = False <EOL> raise exceptions . ConnectError ( '<STR_LIT>' % ( e . __class__ , str ( e ) ) ) <EOL> logging . debug ( '<STR_LIT>' ) </s>
