def check_puppet_version(ip): ''' 检查puppet 版本 ''' p = re.compile("^puppet-0\.25\.5.*") command = "rpm -qa |grep \"^puppet-\"" #command = "sudo sed -i '/10.144.224.86/d' /etc/hosts;sudo sed -i '/10.144.224.82/d' /etc/hosts;sudo service mcollective stop;sudo chkconfig mcollective off" get_hostname_cmd = "hostname -f" install_command = "sudo bash puppet_up.sh;test $? == 0 && rm -f /home/for_monitor/puppet_up.sh" user = "******" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,command) print "result:%s ip:%s" % (result,ip) if isinstance(result,bool): f = open("/tmp/puppet_error.ip",'a+') f.write("%s" % ip) f.close() return False else: pass if p.match(result.strip().strip("\n")): f = open("/tmp/puppet_exec.ip",'a+') f.write("%s : ok" % ip) f.write("\n") f.close() hostname = deal_ssh.remote_ssh_key_exec_simple(ip,user,get_hostname_cmd) clean_ca(hostname) deal_ssh.remote_scp(ip,user) result = deal_ssh.remote_ssh_key_exec_simple(ip,user,install_command)
def do_cmd(ip): user = "******" cmd = "cat /etc/sysconfig/network | grep \"HOSTNAME\"" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,cmd) print result cmd = "cat /etc/hosts |grep \"sogou-in.domain\" | grep -v \"10.12.137.20\"" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,cmd) print result
def set_password_only(host,password): user = "******" _command = 'echo \'%s\' | passwd %s --stdin' % (password,user) print host print _command host = ip user = "******" passwd = "noSafeNoWork@2014" deal_ssh.remote_ssh_key_exec_simple(host,user,_command)
def set_password(ip): ''' 修改密码 ''' import random def id_generator(size=6, chars=string.ascii_uppercase + string.digits+'&%+-()'): return ''.join(random.choice(chars) for _ in range(size)) new_password = id_generator(14) user = "******" host = ip command = '''str=$((echo \"%s\";echo \"%s\")|sudo /sbin/grub-md5-crypt 2>/dev/null | grep -iv \"password\");sudo cat /etc/shadow | awk -v str=$str -F ':' '{OFS=":"}{if($1~/root/){sub(/.*/,str,$2);}print }' > ~/shadow;sudo /bin/cp -f ~/shadow /etc/shadow;sudo chown root:root /etc/shadow;sudo chmod 400 /etc/shadow;rm -f ~/shadow''' % (new_password,new_password) f = open("/tmp/machine.list.pass","a+") f.write(ip) f.write(" ") f.write(new_password) f.write("\n") result = deal_ssh.remote_ssh_key_exec_simple(host,user,command) if result: pass else: print "%s failed" % ip cmd = "ls" root_user = "******" if deal_ssh.remote_ssh_password_simple(host,root_user,new_password,cmd): pass else: print "host: %s, exec failed!" % ip
def del_key(ip): cmd = "sed '/tc_202_117/d' ~/.ssh/authorized_keys" user = "******" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,cmd) if len(result) > 0: print result else: print ip
def change_tmp_power(ip): users = ["lixuebin","lizhi","wangwei","gaozezhou","heguanghao","dongyuntao","liyangshao","yuchang","wujuefei","zhangchengshan","wangshuai","for_dev"] for user in users: cmd = "sudo setfacl -m u:%s:r-x /tmp" % user user = "******" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,cmd) if isinstance(result,bool): print ip return False else: pass
def add_relation(ip): USER = "******" #OP_KEY = '''from=\\"10.147.239.235,10.137.239.235,10.146.239.235,10.136.239.235,10.147.239.1,10.137.239.1,10.146.239.1,10.136.239.1\\",no-agent-forwarding,no-port-forwarding ssh-dss AAAAB3NzaC1kc3MAAACBAPx3767ksyO+E+L6fmIKJ+2Uq6yyyk3F83DQ2J+BLZgkzJG6K9FaoFLJQa+iLu3eL9ik+8/oNYcv96dL4M7tZRrQy0swBzIRlEhVRSMN7Ptiu+2TfNfgujA4PVPIvjPqVcbal1frEIy7VHQHSuVMwMI/6edd6J9FAo9CPHnsIlSdAAAAFQCFzOzFUZyUP9cOD+ubopSb+j3z0wAAAIBYXQOIRHmxk0hlwh13seetRtrkNYp1QGkaSLu8KvSr3cmGAUSndqxPVgvL5xT/C3S+sABB4H5KGpxlqTmqNn2MWM+oX4HBmsKXzslxIp0tlwqUE4DWaNvCEiKBqEBnWM+QTlSZ5C0kTJl+Os4rfEYC46R0bhHhYxc6NnpNyYv6JwAAAIBktn02O6k+Tg6CwN1RcG+RMZcqwZaUJ/kgDha3Ho9CZCcC5mNiC36M1qGW0J47RoEe5vSAsTitBgyr3pPEZp5+pnJjaXVo3uRWVtsAodDoWG0dOqYEIeX03VqrFQrt3SBsezPrpKoxZgItSbR6XnkK42iSrgk/f6Eyd3ckbh5a7Q== root@tc_239_235''' command = "sudo mkdir -p /root/.ssh;sudo cat /root/.ssh/authorized_keys > ~/authorized_keys;sudo sed -i '/tc_239_235/d' ~/authorized_keys;sudo echo \"%s\" >> ~/authorized_keys;sudo /bin/cp ~/authorized_keys /root/.ssh/;sudo chmod 600 /root/.ssh/authorized_keys; rm -f ~/authorized_keys" % OP_KEY result = deal_ssh.remote_ssh_key_exec_simple(ip,USER,command) if result: pass else: print "host:%s add relation failed!" % ip f = open('/tmp/add_relation.result','a+') f.write("host:%s add relation failed!" % ip) f.write("\n") f.close()
def add_hostname_temp(ip): ''' 添加hostname 清理原有hostname ''' inner_domain = "sogou-in.domain" look_hosts_cmd = "cat /etc/hosts" look_network_cmd = "cat /etc/sysconfig/network" short_host_name = "" user = "******" #hostname信息 result = deal_ssh.remote_ssh_key_exec_simple(ip,user,look_network_cmd) buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if line.startswith("HOSTNAME"): short_host_name = line.split("=")[1] hostname = short_host_name + "." + inner_domain p1 = re.compile("(.*)%s()" % short_host_name) result = deal_ssh.remote_ssh_key_exec_simple(ip,user,look_network_cmd) buf = StringIO.StringIO(result) for line in buf.readlines(): line = line.strip() if p1.match(line): delete_old_cmd = "sudo sed -i /%s/d /etc/hosts" % short_host_name deal_ssh.remote_ssh_key_exec_simple(ip,user,delete_old_cmd) add_hostname = "%s %s %s" % (ip,hostname,short_host_name) #add_hostname_temp_cmd = "cp -rp /etc/hosts ~/hosts;echo -e \"%s\" >> hosts;sudo cp -rp hosts /etc/hosts;sudo chown root:root /etc/hosts;hostname -f" % add_hostname #print add_hostname_temp_cmd #result = deal_ssh.remote_ssh_key_exec_simple(ip,user,add_hostname_temp_cmd) add_hostname_cmd = "echo -e \"%s\" >> /etc/hosts;hostname -f" % add_hostname result = deal_ssh.remote_ssh_key_exec_simple(ip,user,add_hostname_cmd) add_hostname_cmd = "sed -i /houston.repos.sogou-inc.com/d /etc/hosts;echo -e \"10.139.45.69 houston.repos.sogou-inc.com\" >> /etc/hosts" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,add_hostname_cmd) print result
def install_soft(hostip): result = False user = "******" scripts_dir = "/opt/opbin/do_remote/scripts" file_name = "%s/%s" % (scripts_dir,"init_env.sh") install_dir = "/usr/local/src/" cmd_local = "rsync -auv %s %s:%s" % (file_name,hostip,install_dir) print cmd_local child1 = subprocess.call(shlex.split(cmd_local), stdout=subprocess.PIPE) cmd_remote = "cd %s;bash %s" % (install_dir,"init_env.sh") if child1 == 0: result = deal_ssh.remote_ssh_key_exec_simple(hostip,user,cmd_remote) if result: print "soft install success!" return True print "install soft sucess!"
def get_vlan_id(ip): user = "******" cmd = "rsync /opt/opbin/do_remote/scripts/update_new_version.sh 10.13.192.140:/usr/local/src/" result = deal_ssh.remote_ssh_key_exec_simple(ip,user,cmd) print result
def del_relation(ip): USER = "******" command = "sudo sed -i '/tc_239_235/d' /root/.ssh/authorized_keys" result = deal_ssh.remote_ssh_key_exec_simple(ip,USER,command) print result