<s> from twisted . conch . ssh import transport , userauth , connection , common , keys , channel <EOL> from twisted . internet import defer , protocol , reactor <EOL> from twisted . python import log <EOL> import struct , sys , getpass , os <EOL> """<STR_LIT>""" <EOL> USER = '<STR_LIT:user>' <EOL> HOST = '<STR_LIT:localhost>' <EOL> PORT = <NUM_LIT> <EOL> SERVER_FINGERPRINT = '<STR_LIT>' <EOL> CLIENT_RSA_PUBLIC = '<STR_LIT>' <EOL> CLIENT_RSA_PRIVATE = '<STR_LIT>' <EOL> class SimpleTransport ( transport . SSHClientTransport ) : <EOL> def verifyHostKey ( self , hostKey , fingerprint ) : <EOL> print '<STR_LIT>' % fingerprint <EOL> if SERVER_FINGERPRINT == fingerprint : <EOL> return defer . succeed ( True ) <EOL> else : <EOL> print '<STR_LIT>' % SERVER_FINGERPRINT <EOL> return defer . fail ( Exception ( '<STR_LIT>' ) ) <EOL> def connectionSecure ( self ) : <EOL> self . requestService ( <EOL> SimpleUserAuth ( USER , <EOL> SimpleConnection ( ) ) ) <EOL> class SimpleUserAuth ( userauth . SSHUserAuthClient ) : <EOL> def getPassword ( self ) : <EOL> return defer . succeed ( getpass . getpass ( "<STR_LIT>" % ( USER , HOST ) ) ) <EOL> def getGenericAnswers ( self , name , instruction , questions ) : <EOL> print name <EOL> print instruction <EOL> answers = [ ] <EOL> for prompt , echo in questions : <EOL> if echo : <EOL> answer = raw_input ( prompt ) <EOL> else : <EOL> answer = getpass . getpass ( prompt ) <EOL> answers . append ( answer ) <EOL> return defer . succeed ( answers ) <EOL> def getPublicKey ( self ) : <EOL> if ( <EOL> not CLIENT_RSA_PUBLIC or <EOL> not os . path . exists ( CLIENT_RSA_PUBLIC ) or <EOL> self . lastPublicKey <EOL> ) : <EOL> return <EOL> return keys . Key . fromFile ( filename = CLIENT_RSA_PUBLIC ) <EOL> def getPrivateKey ( self ) : <EOL> """<STR_LIT>""" <EOL> return defer . succeed ( keys . Key . fromFile ( CLIENT_RSA_PRIVATE ) ) <EOL> class SimpleConnection ( connection . SSHConnection ) : <EOL> def serviceStarted ( self ) : <EOL> self . openChannel ( TrueChannel ( <NUM_LIT:2> ** <NUM_LIT:16> , <NUM_LIT:2> ** <NUM_LIT:15> , self ) ) <EOL> self . openChannel ( FalseChannel ( <NUM_LIT:2> ** <NUM_LIT:16> , <NUM_LIT:2> ** <NUM_LIT:15> , self ) ) <EOL> self . openChannel ( CatChannel ( <NUM_LIT:2> ** <NUM_LIT:16> , <NUM_LIT:2> ** <NUM_LIT:15> , self ) ) <EOL> class TrueChannel ( channel . SSHChannel ) : <EOL> name = '<STR_LIT>' <EOL> def openFailed ( self , reason ) : <EOL> print '<STR_LIT>' , reason <EOL> def channelOpen ( self , ignoredData ) : <EOL> self . conn . sendRequest ( self , '<STR_LIT>' , common . NS ( '<STR_LIT:true>' ) ) <EOL> def request_exit_status ( self , data ) : <EOL> status = struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' % status <EOL> self . loseConnection ( ) <EOL> class FalseChannel ( channel . SSHChannel ) : <EOL> name = '<STR_LIT>' <EOL> def openFailed ( self , reason ) : <EOL> print '<STR_LIT>' , reason <EOL> def channelOpen ( self , ignoredData ) : <EOL> self . conn . sendRequest ( self , '<STR_LIT>' , common . NS ( '<STR_LIT:false>' ) ) <EOL> def request_exit_status ( self , data ) : <EOL> status = struct . unpack ( '<STR_LIT>' , data ) [ <NUM_LIT:0> ] <EOL> print '<STR_LIT>' % status <EOL> self . loseConnection ( ) <EOL> class CatChannel ( channel . SSHChannel ) : <EOL> name = '<STR_LIT>' <EOL> def openFailed ( self , reason ) : <EOL> print '<STR_LIT>' , reason <EOL> def channelOpen ( self , ignoredData ) : <EOL> self . data = '<STR_LIT>' <EOL> d = self . conn . sendRequest ( self , '<STR_LIT>' , common . NS ( '<STR_LIT>' ) , wantReply = <NUM_LIT:1> ) <EOL> d . addCallback ( self . _cbRequest ) <EOL> def _cbRequest ( self , ignored ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> self . conn . sendEOF ( self ) <EOL> def dataReceived ( self , data ) : <EOL> self . data += data <EOL> def closed ( self ) : <EOL> print '<STR_LIT>' % repr ( self . data ) <EOL> self . loseConnection ( ) <EOL> reactor . stop ( ) <EOL> log . startLogging ( sys . stdout ) <EOL> protocol . ClientCreator ( reactor , SimpleTransport ) . connectTCP ( HOST , PORT ) <EOL> reactor . run ( ) </s>
