pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Python pastebin - collaborative debugging tool View Help


Posted by cs3 on Fri 4 Jul 00:37
report spam | download | new post

  1. import sys, os, select, socket
  2. try:
  3.         import paramiko
  4. except Exception, e:
  5.         raise ImportError, "paramiko SSH library import failed !!!"
  6.  
  7. class remoteCmdExecuter:
  8.         def __init__( self ):
  9.                 pass
  10.  
  11.         def execCmd( self, machineDetails ):
  12.                 print "hostname -- " + machineDetails['hostname']
  13.                 print "username -- " + machineDetails['username']
  14.                 print "passwd -- " + machineDetails['passwd']
  15.  
  16.                 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17.                 sock.connect((machineDetails['hostname'], 22))
  18.  
  19.                 pc = paramiko.Transport(sock)
  20.                 pc.start_client()
  21.                 if not pc.is_active():
  22.                         pc.close()
  23.                         raise Exception, 'client negotiation failed'
  24.  
  25.                 pc.auth_password(machineDetails['username'], machineDetails['passwd'])
  26.                 if not pc.is_authenticated():
  27.                         print 'Authentication failed'
  28.                         pc.close()
  29.                         raise Exception, 'invalid password'
  30.  
  31.                 print self.__runCmd(pc, "touch i-was-here.grr")
  32.  
  33.                 pc.close()
  34.  
  35.                 return True
  36.  
  37.         def __runCmd( self, t, cmd ):
  38.                 out = ''
  39.                 print 'Running cmd:', cmd
  40.                 chan = t.open_session()
  41.                 chan.setblocking(0)
  42.  
  43.                 chan.exec_command(cmd)
  44.  
  45.                 if chan.recv_exit_status():
  46.                         print 'cmd not successful !!!'
  47.                         chan.close()
  48.                         return False
  49.  
  50.                 chan.close()
  51.                 return True
  52.  
  53. if __name__ == "__main__":
  54.         aD = remoteCmdExecuter()
  55.         machineDetails = { 'hostname': 'cs3.csshyamsundar.com',
  56.                         'ipaddr': '192.168.0.1', 'username': 'cs3', 'passwd': 'passwd' }
  57.         aD.execCmd( machineDetails )

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me