进程应用
1.远程执行命令
#!/usr/src/python # -*- coding:utf-8 -*- import paramiko,time,re from multiprocessing import Process import configparser def cmd_remote(hostname,port,username,pwd,cmd): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname,port,username,pwd) stdin, stdout, stderr = ssh.exec_command(cmd) res=stdout.read() print('==================='+hostname+'======================= '+res.decode('gbk')) ssh.close(); if __name__ == '__main__': conf=configparser.ConfigParser() conf.read("dbhostinfo") sec_list=conf.sections() while True: cmd=input('prompt>>').strip() server_group = re.findall(r'-g(.+?)-', cmd) host_list = re.findall(r'-h(.+?)-', cmd) cmd_list = re.findall(r'"(.+?)"', cmd) if not cmd:continue elif cmd=='exit': break elif re.match(r'^batch_run',cmd) and server_group and cmd_list: res = server_group[0].strip() single_group=res.split(',') exe_cmd = cmd_list[0].strip() for item in single_group: opt=conf.options(item) p_list=[] for host in opt: single_host = host_list[0].strip() if host not in single_host:continue host_dict=eval(conf.get(item,host)) hostname=host_dict['ip'] port=int(host_dict['port']) username=host_dict['username'] pwd=host_dict['pwd'] p=Process(target=cmd_remote,args=(hostname,port,username,pwd,exe_cmd)) p_list.append(p) for pro in p_list: pro.start() for pro in p_list: pro.join() else: print('命令不对哦!只能是run和scp') # batch_run -h h1,h2 -g mysql,web -cmd "df -h" # batch_scp -h h1,h2 -g mysql,web -action put -local test.py -remote /tmp/
2.ftp上传功能
#!/usr/src/python # -*- coding:utf-8 -*- import paramiko,time,re from multiprocessing import Process import configparser def cmd_scp(hostname,port,username,pwd,file_name,remote_path): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname,port,username,pwd) sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) sftp = ssh.open_sftp() try: sftp.put(file_name, remote_path) print('OK!') except FileNotFoundError: print('本地目录下没有该文件,请上传已存在的文件') pass # res=stdout.read() # print('==================='+hostname+'======================= '+res.decode('gbk')) ssh.close(); if __name__ == '__main__': conf=configparser.ConfigParser() conf.read("dbhostinfo") sec_list=conf.sections() while True: cmd=input('prompt>>').strip() server_group = re.findall(r'-g(.+?)-', cmd) host_list = re.findall(r'-h(.+?)-', cmd) remote_path = cmd.split(' ')[-1] f_name = re.findall(r'-local(.+?)-remote', cmd) oper = re.findall(r'-action(.+?)-local', cmd) print(host_list) if not cmd:continue elif cmd=='exit': break elif re.match(r'^batch_scp',cmd) and server_group and f_name and re.search(r'/S.*/', remote_path): res = server_group[0].strip() single_group=res.split(',') file_name=f_name[0].strip() print(file_name) for item in single_group: opt=conf.options(item) p_list=[] for host in opt: single_host = host_list[0].strip() if host not in single_host:continue host_dict=eval(conf.get(item,host)) hostname=host_dict['ip'] port=int(host_dict['port']) username=host_dict['username'] pwd=host_dict['pwd'] p=Process(target=cmd_scp,args=(hostname,port,username,pwd,file_name,remote_path+file_name)) p_list.append(p) for pro in p_list: pro.start() for pro in p_list: pro.join() else: print('命令不对哦!只能是run和scp') #example #batch_scp -h h1 -g mysql,web -action put -local exe_com.py -remote /tmp/
3.产生配置文件
#!/usr/src/python # -*- coding:utf-8 -*- # 添加主机组及所属主机 import configparser,json conf=configparser.ConfigParser() conf.read("dbhostinfo") while True: sec=input('section:') if sec=='exit':break list_sec=conf.sections() if sec not in list_sec: conf.add_section(sec) hostname=input('hostname:') ip = input('ip:') port = input('port:') username=input('username:') pwd = input('pwd:') host_dic={'ip':ip,'port':port,'username':username,'pwd':pwd} json_res=json.dumps(host_dic) conf.set(sec,hostname,json_res) conf.write(open('dbhostinfo', 'w'))