Recent Posts
C# | 24 sec ago
C# | 39 sec ago
None | 1 min ago
None | 2 min ago
C | 4 min ago
None | 4 min ago
Bash | 5 min ago
C++ | 6 min ago
Java | 6 min ago
None | 6 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By cs3 on the 3rd of Jul 2008 11:37:27 PM Download | Raw | Embed | Report
  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. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: