def upload_file(file_path_list, remote_user, remote_host, remote_path, password, remote_port=22): ''' Copy a file to remote machine. return 0 if success ''' logger = Logging.getLogger(log_file='run_task.log') try: file_path = ' ' # multi files for path_item in file_path_list: file_path += path_item file_path += ' ' if not file_path or not remote_user or not remote_host or not remote_path: logger.error('Empty parameter given') raise Excpetion, 'Empty parameter given' cmd_text = 'scp -P %d %s %s@%s:%s' % \ (remote_port , file_path, remote_user, remote_host, remote_path) print cmd_text logger.info(cmd_text) global timeout_ cmd = pexpect.spawn(cmd_text, timeout=timeout_) expect_res = cmd.expect( ['(yes/no)', 'password', pexpect.EOF, pexpect.TIMEOUT]) if expect_res == 0: logger.info('pexpect expects (yes/no)') cmd.sendline('yes') cmd.expect('password') cmd.sendline(password) elif expect_res == 1: logger.info('pexpect expects (password)') cmd.sendline(password) elif expect_res == 2: logger.info('pexpect expects (EOF)') else: logger.info('pexpect expects (TIMEOUT)') raise Exception, 'TIMEOUT' expect_res = cmd.expect(['Permission denied', pexpect.EOF]) if expect_res == 0: logger.info('Permission denied') cmd.close() cmd.kill(0) else: cmd.close() return cmd.exitstatus except Exception, e: cmd.close() logger.error(e) return 1
def upload_file(file_path_list , remote_user , remote_host ,remote_path , password , remote_port = 22) : ''' Copy a file to remote machine. return 0 if success ''' logger = Logging.getLogger(log_file='upload_remotefile.log') try : file_path = ' '# multi files for path_item in file_path_list : file_path += path_item file_path += ' ' if not file_path or not remote_user or not remote_host or not remote_path : logger.error('Empty parameter given') raise Excpetion , 'Empty parameter given' cmd_text = 'scp -P %d %s %s@%s:%s' % \ (remote_port , file_path, remote_user, remote_host, remote_path) print cmd_text logger.info(cmd_text) global timeout_ cmd = pexpect.spawn(cmd_text,timeout=timeout_) expect_res = cmd.expect(['(yes/no)','password',pexpect.EOF,pexpect.TIMEOUT]) if expect_res == 0 : logger.info('pexpect expects (yes/no)') cmd.sendline('yes') cmd.expect('password') cmd.sendline(password) elif expect_res == 1 : logger.info('pexpect expects (password)') cmd.sendline(password) elif expect_res == 2 : logger.info('pexpect expects (EOF)') else: logger.info('pexpect expects (TIMEOUT)') raise Exception , 'TIMEOUT' expect_res = cmd.expect(['Permission denied',pexpect.EOF]) if expect_res == 0 : logger.info('Permission denied') cmd.close() cmd.kill(0) else : cmd.close() return cmd.exitstatus except Exception ,e : cmd.close() logger.error(e) return 1
def stop_remotecommand(key_code_list, server_array) : logger = Logging.getLogger(log_file='upload_remotefile.log') log_code = key_code_list[0][:len(key_code_list[0])/2] log_name = log_code + '.log' result = Queue.Queue() def do_stop_query(key_code_list, machine) : try: s = pxssh.pxssh() is_login = s.login(server = machine['remote_host'], username = machine['remote_user'], password = machine['password'] , login_timeout = 100) if is_login == False : raise Exception , 'Permission denied' else: print 'login success' cnt = 0 while(True) : s.sendline('ps -aux | grep --color=never ' + log_code) s.prompt() words = s.before.split() index = 0 PID = -1 FIND = False if machine['remote_path'].endswith('/') == False : machine['remote_path'] += '/' for item in words : if item.find(machine['remote_path'] + log_code) >= 0 : PID = int(words[index-10]) for code in key_code_list : if item.find(code) >= 0 : FIND = True break break index+=1 logger.info("Find %s, key_code_list=%s" % (str(FIND), key_code_list)) print "Find %s, key_code_list=%s" % (str(FIND), key_code_list) if FIND == False or cnt > 10: cmd_text = '' log_name_ = machine['remote_path'] + log_name if cnt > 10 : cmd_text = 'echo "failed to stop: ' + log_code + '" >> ' + log_name_ + ' &' else : cmd_text = 'echo "manumally stop: ' + log_code + '" >> ' + log_name_ + ' &' print cmd_text s.sendline(cmd_text) s.prompt() break #double check else: print "kill: PID %s, key_code_list=%s" % (PID, key_code_list) logger.info("kill: PID %s, key_code_list=%s" % (PID, key_code_list)) cnt += 1 if cnt > 0 : kill_res = s.sendline('kill -9 '+str(PID)) else: kill_res = s.sendline('kill '+str(PID)) s.prompt() s.logout() result.put([s.exitstatus,key_code_list,log_code,machine])#success except Exception, e: print e result.put([-1,[],'',machine])#fail