Advertisement
Guest User

Untitled

a guest
May 25th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. ## Hadoop env var
  4. port = 50030
  5. host = '10.14.3.107'
  6.  
  7. import warnings
  8. with warnings.catch_warnings():
  9.         warnings.filterwarnings("ignore",category=DeprecationWarning)
  10.         import paramiko
  11.  
  12. import sys, os, socket, getopt
  13.  
  14. def check_sane(port):
  15.     # check to see if the hadoop mapred server is up
  16.     try:
  17.         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  18.         s.connect((host,port))
  19.     except socket.error:
  20.         if port==50030:
  21.             print "FAIL Hadoop server not responding -- environment not sane"
  22.         elif port==22:
  23.             print "FAIL sshd not responding -- environment not sane"
  24.         s.close()
  25.         sys.exit(1)
  26.     finally:
  27.         if port==50030:
  28.             print "OK   %s Hadoop server responding" % (host)
  29.         elif port==22:
  30.             print "OK   %s sshd responding" % (host)
  31.         s.close()
  32.  
  33. def usage_quit(basename):
  34.     usage = """
  35. usage: %s [-f source file] [-d DFS destination]
  36.     -f File to be copied to hadoop DFS
  37.     -d Destination including DFS path
  38. Example: %s -f myfile -d /myuser/myfile
  39. Same as: %s myfile myfile (assumes current username)
  40. """ % (basename, basename, basename)
  41.     print usage
  42.     sys.exit(1)
  43.  
  44. def ssh_connection(host,port,user,cmd):
  45.     print 'OK   Establishing connection to %s' % (host)
  46.     try:
  47.         p = paramiko.Transport((host,port))
  48.         p.connect(username=user, password=None)
  49.         socket = p.open_session()
  50.         socket.exec_command(cmd)
  51.         response = socket.recv(2048)
  52.         print response
  53.     except Exception, err:
  54.         print "FAIL unable to process %r: %s" % (user,err)
  55.  
  56. def ssh_socket(host,port,user):
  57.     print 'OK   Establishing connection to %s' % (host)
  58.     try:
  59.         ssh = paramiko.SSHClient()
  60.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  61.         ssh.connect(host,port,user,password=None)
  62. #       return ssh
  63.     except Exception, err:
  64.         print "FAIL unable to process %r: %s" % (user,err)
  65.         sys.exit(1)
  66.  
  67. def ssh_send(ssh,file):
  68.     # connect sftp and copy the jar
  69.    
  70.  
  71.  
  72. if __name__ == '__main__':
  73.     basename = os.path.splitext(os.path.basename(sys.argv[0]))[0]
  74.     if len(sys.argv) < 2:
  75.         usage_quit(basename)
  76.     check_sane(50030)
  77.     check_sane(22)
  78.     try: optlist = getopt.getopt(sys.argv[1:], 'f:d:')
  79.     except getopt.GetoptError: usage_quit(basename)
  80.     cmd = cmd_list(optlist)
  81. #   ssh_connection(host,22,os.environ['USER'],cmd)
  82.     mySSH = ssh_socket(host,22,os.environ['USER'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement