標籤:localhost network password python import
公司需要新上項目,上架20台機器,要對這些主機進行改名
#!/usr/bin/env pythonfrom multiprocessing import Process, Poolimport paramikoimport sys,oss = paramiko.SSHClient()s.load_system_host_keys()s.set_missing_host_key_policy(paramiko.AutoAddPolicy())def ssh_run(host_info): ip=host_info[0] username=host_info[1] password=host_info[2] hostname=host_info[3] s.connect(ip,22,username,password,timeout=5) stdin,stdout,stderr=s.exec_command("sed -i ‘/HOSTNAME/s/localhost\.localdomain/%s/‘ /etc/sysconfig/network"%hostname) cmd_result = stdout.read(),stderr.read() print ‘\033[32;1m-------------%s-----------\033[0m‘ % ip,‘Success‘ for line in cmd_result: print line,p = Pool(processes=2)result_list = []f = open(‘ip.txt‘,‘r‘)for line in f.readlines(): new_line=line.strip(‘\n‘).split() result_list.append(p.apply_async(ssh_run, [new_line,])) f.close()for res in result_list: res.get()s.close()
cat ip.txt 192.168.10.40 root 123456 web1192.168.10.35 root 123456 web2
本文出自 “伺服器” 部落格,請務必保留此出處http://zhangfang2012.blog.51cto.com/6380212/1641806
python 批量修改主機名稱