Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ## Hadoop env var
- port = 50030
- host = '10.14.3.107'
- import warnings
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore",category=DeprecationWarning)
- import paramiko
- import sys, os, socket, getopt
- def check_sane(port):
- # check to see if the hadoop mapred server is up
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.connect((host,port))
- except socket.error:
- if port==50030:
- print "FAIL Hadoop server not responding -- environment not sane"
- elif port==22:
- print "FAIL sshd not responding -- environment not sane"
- s.close()
- sys.exit(1)
- finally:
- if port==50030:
- print "OK %s Hadoop server responding" % (host)
- elif port==22:
- print "OK %s sshd responding" % (host)
- s.close()
- def usage_quit(basename):
- usage = """
- usage: %s [-f source file] [-d DFS destination]
- -f File to be copied to hadoop DFS
- -d Destination including DFS path
- Example: %s -f myfile -d /myuser/myfile
- Same as: %s myfile myfile (assumes current username)
- """ % (basename, basename, basename)
- print usage
- sys.exit(1)
- def ssh_connection(host,port,user,cmd):
- print 'OK Establishing connection to %s' % (host)
- try:
- p = paramiko.Transport((host,port))
- p.connect(username=user, password=None)
- socket = p.open_session()
- socket.exec_command(cmd)
- response = socket.recv(2048)
- print response
- except Exception, err:
- print "FAIL unable to process %r: %s" % (user,err)
- def ssh_socket(host,port,user):
- print 'OK Establishing connection to %s' % (host)
- try:
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(host,port,user,password=None)
- # return ssh
- except Exception, err:
- print "FAIL unable to process %r: %s" % (user,err)
- sys.exit(1)
- def ssh_send(ssh,file):
- # connect sftp and copy the jar
- if __name__ == '__main__':
- basename = os.path.splitext(os.path.basename(sys.argv[0]))[0]
- if len(sys.argv) < 2:
- usage_quit(basename)
- check_sane(50030)
- check_sane(22)
- try: optlist = getopt.getopt(sys.argv[1:], 'f:d:')
- except getopt.GetoptError: usage_quit(basename)
- cmd = cmd_list(optlist)
- # ssh_connection(host,22,os.environ['USER'],cmd)
- mySSH = ssh_socket(host,22,os.environ['USER'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement