Exemplo n.º 1
0
def cmd_excute():
    try:
        h = db_connector.IP.objects.get(ip=sys.argv[1])
        host = h.ip
        port = int(h.port)
        username = run_user
        auth = db_connector.AuthByIpAndRemoteUser.objects.get(
            ip__ip=host, remoteUser__name=username)
        password = auth.password
        auth_type = auth.authtype
        print password, auth_type
        pkey_file = auth.password
    except ObjectDoesNotExist:
        err_msg = 'No access right to host,please make sure you have your TriAquae user bound to remote user %s and host %s' % (
            run_user, sys.argv[1])
        logger.RecordLog(sys.argv[1], 'CommandExcution', cmd, err_msg, 'Error',
                         track_mark, run_user, multi_run)
        print err_msg
        sys.exit()

    s = paramiko.SSHClient()
    s.load_system_host_keys()
    s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        if auth_type == 'ssh-key':
            #pkey_file = '/home/alex/.ssh/id_rsa'
            key = paramiko.RSAKey.from_private_key_file(pkey_file)
            s.connect(host, port, username, pkey=key, timeout=5)
            stdin, stdout, stderr = s.exec_command(cmd)

            result = stdout.read(), stderr.read()
            logger.RecordLog(host, 'CommandExcution', cmd, result, 'Success',
                             track_mark, run_user, multi_run)
            print Split_line, h.ip, Split_line, '\n',
            for line in result:
                print line,
        elif auth_type == 'ssh':
            s.connect(host, port, username, password, timeout=5)
            stdin, stdout, stderr = s.exec_command(cmd)
            result = stdout.read(), stderr.read()
            logger.RecordLog(host, 'CommandExcution', cmd, result, 'Success',
                             track_mark, run_user, multi_run)
            print Split_line, h.ip, Split_line, '\n', stdout.read()
            if stderr.read(): print 'error happend!', stderr.read()
            for line in result:
                print line,
    except paramiko.AuthenticationException:
        result = "%s ---Authentication failed!\n" % host
        print result
        logger.RecordLog(host, 'CommandExcution', cmd, result, 'Error',
                         track_mark, run_user, multi_run)
    except:
        result = "%s ---timeout or configration error, please manually check the connection!\n" % host
        print result
        logger.RecordLog(host, 'CommandExcution', cmd, result, 'Error',
                         track_mark, run_user, multi_run)

    s.close()
Exemplo n.º 2
0
s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

print h.protocol_type.protocol
try:
    if h.protocol_type.protocol == 'SSH_KEY':
        #pkey_file = '/home/alex/.ssh/id_rsa'
        key = paramiko.RSAKey.from_private_key_file(pkey_file)
        s.connect(host, port, username, pkey=key, timeout=5)
        stdin, stdout, stderr = s.exec_command(cmd)
        #print Split_line,h.ip,Split_line

        result = stdout.read(), stderr.read()
        logger.RecordLog(host, 'CommandExcution', cmd, result, 'Success',
                         track_mark, run_user)
        print Split_line, h.ip, Split_line, '\n',
        for line in result:
            print line,
        #print stderr.read()
    elif h.protocol_type.protocol == 'SSH':
        #try:
        s.connect(host, port, username, password, timeout=5)
        stdin, stdout, stderr = s.exec_command(cmd)
        #print Split_line,h.ip,Split_line
        result = stdout.read(), stderr.read()
        logger.RecordLog(host, 'CommandExcution', cmd, result, 'Success',
                         track_mark, run_user)
        print Split_line, h.ip, Split_line, '\n', stdout.read()
        if stderr.read(): print 'error happend!', stderr.read()
        for line in result:
Exemplo n.º 3
0
home = os.environ['HOME']
try:
    h = db_connector.IP.objects.get(ip=sys.argv[1])
    host = h.ip
    port = int(h.port)
    username = sys.argv[2]
    auth = db_connector.AuthByIpAndRemoteUser.objects.get(
        ip__ip=host, remoteUser__name=username)
    password = auth.password
    auth_type = auth.authtype
    option = sys.argv[3]
    pkey_file = auth.password
except ObjectDoesNotExist:
    err_msg = 'No access right to host,please make sure you have your OPS user bound to remote user %s and host %s' % (
        sys.argv[2], sys.argv[1])
    logger.RecordLog(sys.argv[1], 'SftpConnection', 'N/A', err_msg, 'Error',
                     track_mark, sys.argv[2], multi_run)
    print err_msg
    sys.exit()
try:
    run_user = sys.argv[6]
except IndexError:
    run_user = '******'

s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    t = paramiko.Transport((host, port))
    if auth_type == 'ssh-key':
        key = paramiko.RSAKey.from_private_key_file(pkey_file)
        t.connect(username=username, pkey=key)
Exemplo n.º 4
0
def cmd_excute():
	try:
	    h=db_connector.IP.objects.get(ip = sys.argv[1])
	    host= h.ip 
	    port= int(h.port )
	    username = sys.argv[2] 
	    auth = db_connector.AuthByIpAndRemoteUser.objects.get(ip__ip=host, remoteUser__name= username)
	    password = auth.password
	    auth_type = auth.authtype
	    option = sys.argv[3]
	    pkey_file= auth.password
	except ObjectDoesNotExist:
	    err_msg = 'No access right to host,please make sure you have your OPS user bound to remote user %s and host %s' %(sys.argv[2],sys.argv[1])
	    logger.RecordLog(sys.argv[1],'SftpConnection','N/A',err_msg,'Error',track_mark,sys.argv[2],multi_run)
	    print err_msg
	    sys.exit()
	try:
		run_user = sys.argv[6]
	except IndexError:
		run_user = '******'

	s = paramiko.SSHClient()
	s.load_system_host_keys()
	s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	try:
	    t = paramiko.Transport((host,port))
	    if auth_type == 'ssh-key':
			key = paramiko.RSAKey.from_private_key_file(pkey_file)
			t.connect(username=username,pkey=key)
	    elif auth_type == 'ssh':
			t.connect(username=username,password=password)

	except paramiko.AuthenticationException:
		msg= host,'---Authentication failed!'
		print msg
		logger.RecordLog(host,'SftpConnection','N/A',msg,'Error',track_mark,run_user,multi_run)
		sys.exit()
	except paramiko.SSHException:
		msg= host,'---Unable to connect,no route to host!'
		print msg
		logger.RecordLog(host,'SftpConnection','N/A',msg,'Error',track_mark,run_user,multi_run)
		sys.exit()


	sftp = paramiko.SFTPClient.from_transport(t)
	remote_path = sys.argv[5] 
	sftp_recv = tri_config.Tri_sftp_recv_dir 
	if option == '-s': #send file
		local_file = sys.argv[4]
		filename = local_file.split('/')[-1]
		remote_file ='%s/%s' %(remote_path,filename)
		msg= '------- sending file %s to %s -------' %(local_file,host)
		print msg
		try:
			sftp.mkdir(remote_path)
		except IOError:pass
		sftp.put(local_file,remote_file)
			
		msg = host,"send file %s to remote directory successful: %s, under %s's home direcotry" % (filename,remote_path,username)
		logger.RecordLog(host,'SendFile','N/A',msg,'Success',track_mark,run_user,multi_run)
		sftp.put(local_file,remote_file)
		print 'Done!'
	elif option == '-g': # get file
		remote_file = sys.argv[4]
		filename = remote_file.split('/')[-1]
		local_file = '%s/%s_%s' %(sftp_recv,host,filename)
		print '------- getting file %s from %s -------' %(remote_file,host)
		try:
			sftp.lstat(remote_file)	
			sftp.get(remote_file,local_file)
			print 'put file %s into local path: ' % local_file
			msg = "Result:",'Get file %s from remote ip %s and put it into local path: %s successful' %(remote_file,host,local_file)
			logger.RecordLog(host,'GetFile','N/A',msg,'Success',track_mark,run_user,multi_run)
			print 'Done'	
		except IOError:
			msg= 'Error:','file on remote ip %s not exist or it is a directory!' % host
			print msg
			logger.RecordLog(host,'GetFile','N/A',msg,'Error',track_mark,run_user,multi_run)

		
	s.close()
Exemplo n.º 5
0
cmd = sys.argv[2]
try:
        h=db_connector.IP.objects.get(ip = sys.argv[1])
        host= h.ip 
        port= int(h.port )
        username = run_user 
        auth = db_connector.AuthByIpAndRemoteUser.objects.get(ip__ip=host, remoteUser__name= username)
        password = auth.password
        auth_type = auth.authtype
        print password, auth_type
        #pkey_file = db_connector.RemoteUser.objects.get(user_name='root',description="test").RsaKey_file_path
        pkey_file= auth.password 
except ObjectDoesNotExist:
    err_msg = 'No access right to host,please make sure you have your TriAquae user bound to remote user %s and host %s' %(run_user,sys.argv[1])
    logger.RecordLog(sys.argv[1],'CommandExcution',cmd,err_msg,'Error',track_mark,run_user,multi_run)
    print err_msg
    sys.exit()

s = paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())


try:
	if auth_type == 'ssh-key':
		#pkey_file = '/home/alex/.ssh/id_rsa'
		key = paramiko.RSAKey.from_private_key_file(pkey_file)
		s.connect(host,port,username,pkey=key,timeout=5)
		stdin,stdout,stderr = s.exec_command(cmd)
		#print Split_line,h.ip,Split_line