def do_nmap(self, p_kvm_list): for item in p_kvm_list: ip_items = HostLoginifo.objects.filter(ip=item) docker_dct = {} nr = prpcrypt() for ip_item in ip_items: doobj = J_ssh_do([ip_item.ip, ip_item.ssh_port, ip_item.ssh_user]) if ip_item.ssh_type == 0: ssh_passwd = nr.decrypt(ip_item.ssh_passwd) login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ssh_passwd) res = doobj.pass_do(login_info, self.p_kvm_cmds) if ip_item.ssh_type == 1: login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ip_item.ssh_rsa) res = doobj.rsa_do(login_info, self.p_kvm_cmds) if ip_item.ssh_type == 2: login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ip_item.ssh_rsa) res = doobj.dsa_do(login_info, self.p_kvm_cmds) if ip_item.ssh_type == 3: login_info = ( ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ip_item.ssh_rsa, ip_item.rsa_pass) res = doobj.imoocc_rsa_do(login_info, self.p_kvm_cmds) kvm_mac_list = res[self.p_kvm_cmds[0]].split('\n') for kvm_item in kvm_mac_list: if kvm_item: kvm_mac_str = mac_trans(kvm_item)[-10:] py_id = PhysicalServerInfo.objects.get(conn_phy__sn_key=self.ip_key_dic[item]).id v_obj = VirtualServerInfo.objects.filter(mac__contains=kvm_mac_str) if v_obj: v_obj.update(vir_phy=py_id, server_type="KVM") else: p_ob = PhysicalServerInfo.objects.filter(mac__contains=kvm_mac_str) if p_ob and len(p_ob) < 2: p_ob.update(vir_phy=py_id, server_type="KVM")
def try_login(self, sship_list, keyfile, cmdlist): for ip, port in sship_list.items(): print "try key login ...... ", ip, port logger.info("Try ssh idrsa key : %s,%s" % (ip, port)) keyfile = "/home/raffaele/.ssh/id_rsa" if ip not in self.can_login_list.keys(): logger.info("Try ssh id_rsa key: %s, %s, %s" % (ip, port, keyfile)) print "Try ssh id_rsa key: %s, %s, %s" % (ip, port, keyfile) login_info = (ip, int(port), 'raffaele', keyfile) doobj = J_ssh_do() res = doobj.rsa_do(login_info, cmdlist) if res["status"] == "success": sys_hostname = res["hostname"].replace('\n', '') system_info = getsysversion(res[ "cat /etc/issue |grep [0-9] || cat /etc/redhat-release |grep [0-9]"] ) sys_mac = mac_trans(res[ "cat /sys/class/net/[^vftlsdb]*/address || esxcfg-vmknic -l|awk '{print $8}'|grep ':'"] ) sys_sn = sn_trans( res["sudo dmidecode -s system-serial-number"].replace( '\n', '')) machine_type = machine_type_trans( res["sudo dmidecode -s system-manufacturer"] + res["sudo dmidecode -s system-product-name"]) self.can_login_list[ip] = (sys_hostname, port, "raffaele", keyfile, system_info, sys_mac, sys_sn, machine_type) else: if ip not in self.can_not_login_list.keys( ) and ip not in self.can_login_list.keys(): self.can_not_login_list[ip] = (port, keyfile) return self.can_login_list, self.can_not_login_list
def get_host_type(self, pv_dict): h_type = { 'qemu-system-x86_64': '0', 'docker-containerd': '1', 'vmx': '2', } pv = J_ssh_do() for sn_key, ip in pv_dict.items(): for item_name in h_type: if item_name == 'qemu-system-x86_64': v_cmd = ["ps -ef|grep %s' '|grep -v grep" % item_name] res = pv.run(ip, v_cmd) if res['status'] == "success" and res[v_cmd[0]]: HostLoginifo.objects.filter(ip=ip).update(host_type=h_type[item_name]) self.p_kvm_lst.append(ip) elif item_name == 'docker-containerd': v_cmd = ["ps -ef|grep %s' '|grep -v grep" % item_name] res = pv.run(ip, v_cmd) if res['status'] == "success" and res[v_cmd[0]]: HostLoginifo.objects.filter(ip=ip).update(host_type=h_type[item_name]) self.p_docker_lst.append(ip) elif item_name == 'vmx': d_cmd = ["ps|grep %s|grep -v grep" % item_name] res = pv.run(ip, d_cmd) if res['status'] == "success" and res[d_cmd[0]]: HostLoginifo.objects.filter(ip=ip).update(host_type=h_type[item_name]) # Esx宿主机无法通过dmicode命令获取 PhysicalServerInfo.objects.filter(server_ip__contains=ip).update(machine_brand=u"ESXI宿主机(未知型号)") self.p_vmx_lst.append(ip) self.p_relate_dic['qemu-system-x86_64'] = self.p_kvm_lst self.p_relate_dic['docker-containerd'] = self.p_docker_lst self.p_relate_dic['vmx'] = self.p_vmx_lst return self.p_relate_dic
def try_login(self, sship_list, password_list, syscmd_list): ''' 尝试ssh用户密码登录,获取机器基本信息 :param sship_list: :param password_list: :param syscmd_list: :return: ''' password_list = password_list syscmd_list = syscmd_list #判断sship_list是否为字典(dict) if isinstance(sship_list, dict): ssh_tuple_list = [(ip, port) for ip, port in sship_list.items()] #判断sship_list是否为列表(list) elif isinstance(sship_list, list): ssh_tuple_list = sship_list for ip, port in ssh_tuple_list: system_info = "" for password in password_list: if ip not in self.can_login_lst.keys(): login_info = (ip, int(port), 'root', password) doobj = J_ssh_do(login_info) res = doobj.pass_do(login_info, syscmd_list) if res["status"] == "success": if ip in self.not_login_lst: self.not_login_lst.pop(ip) sys_hostname = res["hostname"] sys_mac = mac_trans(res[ "cat /sys/class/net/[^vtlsbd]*/address||esxcfg-vmknic -l|awk '{print $8}'|grep ':'"] ) sys_sn = sn_trans( res["dmidecode -s system-serial-number"]) system_info = getsysversion([ res["cat /etc/issue"], res["cat /etc/redhat-release"] ]) #dmidecode获取硬件信息,由于都是搭载于VMware上,所以查到的都是VM machine_type = machine_type_trans( res["dmidecode -s system-manufacturer"] + res["dmidecode -s system-product-name"]) print("ssh login and exec command:", res) logger.info("ssh login and exec command:", res) self.can_login_lst[ip] = (port, password, 'root', system_info, sys_hostname, sys_mac, sys_sn, machine_type) elif res["status"] == "failed" and re.search( r"reading SSH protocol banner", res["res"]): # print "res res..........................",res['res'] print( "IP:%s Connection closed by remote host,Sleep 60 (s).................. " % ip, res) time.sleep(60) else: if ip not in self.not_login_lst.keys( ) and ip not in self.can_login_lst.keys(): self.not_login_lst[ip] = port # print ip,port,password,traceback.print_exc() return self.can_login_lst, self.not_login_lst
def do_nmap(self, host_list): ip_items = HostLoginifo.objects.filter(ip__in=host_list) ns = prpcrypt() for ip_item in ip_items: docker_dct = {} tmp_sship_list = [] doobj = J_ssh_do([ip_item.ip, ip_item.ssh_port, ip_item.ssh_user]) if ip_item.ssh_type == 0: ssh_passwd = ns.decrypt(ip_item.ssh_passwd) login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ssh_passwd) res = doobj.pass_do(login_info, self.docker_cmd_list) if ip_item.ssh_type == 1: login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ip_item.ssh_rsa) res = doobj.rsa_do(login_info, self.docker_cmd_list) if ip_item.ssh_type == 2: login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ip_item.ssh_rsa) res = doobj.dsa_do(login_info, self.docker_cmd_list) if ip_item.ssh_type == 3: login_info = (ip_item.ip, int(ip_item.ssh_port), ip_item.ssh_user, ip_item.ssh_rsa, ip_item.rsa_pass) res = doobj.imoocc_rsa_do(login_info, self.docker_cmd_list) # port_list = res["docker ps |awk -F '->' '{print $1}'|grep -v 'CONTAINER'|awk -F ':' '{print $NF}'|grep '^[0-9]'"].split("\n") port_list = res[ "docker ps |awk -F '->' '{print $1}'|grep -v 'CONTAINER'|awk 'BEGIN{FS~/s+/;}{print $NF\" \"$1\" \"$2;}'|sed s/0.0.0.0://"].split( "\n") for d_item in port_list: if d_item: print("...............d_item", d_item) d_port, d_id, d_dn = re.split('\s+', d_item)[:3] d_cid = d_id + d_dn docker_dct[d_port] = sn_trans(d_cid) sship_list = [(ip_item.ip, port) for port in docker_dct.keys() if port] nr = prpcrypt() canlogin_list, notlogin_list = self.try_docker_login(sship_list, self.password_list, self.docker_cmd) for ip, port in sship_list: # sship_obj = {} # sship_obj[ip] = port if canlogin_list.has_key(port): id = ConnectionInfo.objects.all().count() + 1 item_val = canlogin_list[port] ssh_passwd = nr.encrypt(item_val[1]) ConnectionInfo.objects.create(id=id, ssh_username=item_val[2], ssh_userpasswd=ssh_passwd, ssh_hostip=ip, ssh_host_port=port, ssh_status=1, ssh_type=4, sn_key=docker_dct[port]) py_id = PhysicalServerInfo.objects.get(conn_phy__sn_key=self.ip_key_dic[ip]).id VirtualServerInfo.objects.create(server_ip=ip, server_type="Docker Contianer", system_ver=item_val[3], sys_hostname=item_val[4], mac=item_val[5], sn=docker_dct[port], vir_phy_id=py_id, conn_vir_id=id) if notlogin_list.has_key(port): item_val = notlogin_list[ip] ConnectionInfo.objects.create(id=id, ssh_username=item_val[2], ssh_hostip=ip, ssh_host_port=item_val[0], ssh_status=0, ssh_type=5, sn_key=docker_dct[port]) py_id = PhysicalServerInfo.objects.get(conn_phy__sn_key=self.ip_key_dic[ip]).id VirtualServerInfo.objects.create(server_ip=ip, server_type="Docker Contianer", vir_phy_id=py_id, conn_vir_id=id)
def try_login(self,sship_list,password_list,syscmd_list): ''' 尝试ssh用户密码登录,获取机器基本信息 :param sship_list: :param password_list: :param syscmd_list: :return: ''' password_list = password_list syscmd_list = syscmd_list if isinstance(sship_list, dict): ssh_tuple_list = [] for ip,port in sship_list.items(): if isinstance(port, str): ssh_tuple_list.append((ip,port)) ssh_tuple_list.append((ip,port[0])) elif isinstance(sship_list,list): ssh_tuple_list = sship_list for ip,port in ssh_tuple_list: #system_info = "" for password in password_list: if ip not in self.can_login_lst.keys(): login_info = (ip,int(port),'root', password) doobj = J_ssh_do(login_info) res = doobj.pass_do(login_info,syscmd_list) if res["status"] == "success": if self.not_login_lst.has_key(ip): self.not_login_lst.pop(ip) sys_hostname = ht_trans(res["hostname"]) #sys_mac = mac_trans(res["cat /sys/class/net/[^vtlsb]*/address||esxcfg-vmknic -l|awk '{print $8}'|grep ':'"]) sys_mac = mac_trans(res["cat /sys/class/net/[e]*/address"]) sys_sn = sn_trans(res["dmidecode -s system-serial-number"]) system_info = getsysversion([res["cat /etc/issue"],res["cat /etc/redhat-release"]]) machine_type = machine_type_trans(res["dmidecode -s system-manufacturer"] + res["dmidecode -s system-product-name"]) print "ssh login and exec command:%s"%res logger.info("ssh login and exec command:%s",res) self.can_login_lst[ip] = (port,password,'root',system_info,sys_hostname,sys_mac,sys_sn,machine_type) elif res["status"] == "failed" and re.search(r"reading SSH protocol banner",res["res"]): # print "res res..........................",res['res'] #print "IP:%s Connection closed by remote host,Sleep 60 (s).................. "%ip,res print "%s:%s:%s paramiko banner_timeout too short or server's sshd not response (%s).................. "%ip,port,password,res #time.sleep(60) else: if ip not in self.not_login_lst.keys() and ip not in self.can_login_lst.keys(): self.not_login_lst[ip] = port # print ip,port,password,traceback.print_exc() print 'self.can_login_lst:', self.can_login_lst print 'self.not_login_lst:', self.not_login_lst return self.can_login_lst,self.not_login_lst
def try_login(self, s_net, s_port, s_user, s_key, s_cmds): ''' 尝试ssh用户密码登录,获取机器基本信息 :param sship_list: :param password_list: :param syscmd_list: :return: ''' login_info = [s_net, int(s_port), s_user, s_key] doobj = J_ssh_do(login_info) res = doobj.pass_do(login_info, s_cmds) if res["status"] == "success": print('11111111111') if s_net in self.not_login_lst: self.not_login_lst.pop(s_net) sys_hostname = res["hostname"] sys_mac = mac_trans(res[ "cat /sys/class/net/[^vtlsb]*/address||esxcfg-vmknic -l|awk '{print $8}'|grep ':'"] ) sys_sn = sn_trans(res["dmidecode -s system-serial-number"]) system_info = getsysversion( [res["cat /etc/issue"], res["cat /etc/redhat-release"]]) self.can_login_lst[s_net] = (s_port, s_key, s_user, system_info, sys_hostname, sys_mac, sys_sn) elif res["status"] == "failed" and re.search( r"reading SSH protocol banner", res["res"]): print("2222222222222") print( "IP:%s Connection closed by remote host,Sleep 0.1 (s).................. " % s_net, res) time.sleep(0.1) else: if s_net not in self.not_login_lst.keys( ) and s_net not in self.can_login_lst.keys(): print("33333333333") self.not_login_lst[s_net] = s_port return self.can_login_lst, self.not_login_lst
def try_key_login(self, sship_list, allkeyfile, syscmd_list): ''' 尝试ssh秘钥登录,获取机器基本信息 :param sship_list: :param allkeyfile: :param syscmd_list: :return: ''' # import traceback for ip, port in sship_list.items(): print("try key login....", ip, port) logger.info("Try ssh key login : %s,%s" % (ip, port)) keyfile = allkeyfile[0] if ip not in self.can_key_login_lst.keys(): logger.info("Try ssh idrsa key : %s,%s,%s" % (ip, port, keyfile)) print('try idrsakey....', ip, port, keyfile) login_info = (ip, int(port), 'root', keyfile) doobj = J_ssh_do(login_info) res = doobj.rsa_do(login_info, syscmd_list) if res["status"] == "success": sys_hostname = res["hostname"] system_info = getsysversion([ res["cat /etc/issue"], res["cat /etc/redhat-release"] ]) sys_mac = mac_trans(res[ "cat /sys/class/net/[^vtlsbd]*/address||esxcfg-vmknic -l|awk '{print $8}'|grep ':'"] ) sys_sn = sn_trans(res["dmidecode -s system-serial-number"]) machine_type = machine_type_trans( res["dmidecode -s system-manufacturer"] + res["dmidecode -s system-product-name"]) self.can_key_login_lst[ip] = (port, keyfile, "root", "", 1, system_info, sys_hostname, sys_mac, sys_sn, machine_type) if res["status"] == "failed": keyfile = allkeyfile[1] logger.info("try iddsa login...%s,%s,%s" % (ip, port, keyfile)) print("try iddsa login...", ip, port, keyfile) login_info = (ip, port, 'root', keyfile) doobj = J_ssh_do(login_info) res = doobj.dsa_do(login_info, syscmd_list) if res["status"] == "success": sys_hostname = res["hostname"] system_info = getsysversion([ res["cat /etc/issue"], res["cat /etc/redhat-release"] ]) sys_mac = mac_trans(res[ "cat /sys/class/net/[^vtlsbd]*/address||esxcfg-vmknic -l|awk '{print $8}'|grep ':'"] ) sys_sn = sn_trans( res["dmidecode -s system-serial-number"]) machine_type = machine_type_trans( res["dmidecode -s system-manufacturer"] + res["dmidecode -s system-product-name"]) if ip in self.key_not_login_lst: self.key_not_login_lst.pop(ip) self.can_key_login_lst[ip] = (port, keyfile, "root", "", 2, system_info, sys_hostname, sys_mac, sys_sn, machine_type) else: keyfile = allkeyfile[2] logger.info("try Non-root idrsa login:%s,%s" % (ip, port)) print("try Non-root idrsa login...", ip, port) password = '******' login_info = (ip, port, 'root', keyfile, password) doobj = J_ssh_do(login_info) res = doobj.imoocc_rsa_do(login_info, syscmd_list) if res["status"] == "success": sys_hostname = res["hostname"] sys_mac = mac_trans(res[ "cat /sys/class/net/[^vtlsbd]*/address||esxcfg-vmknic -l|awk '{print $8}'|grep ':'"] ) system_info = getsysversion([ res["cat /etc/issue"], res["cat /etc/redhat-release"] ]) sys_sn = sn_trans( res["dmidecode -s system-serial-number"]) machine_type = machine_type_trans( res["dmidecode -s system-manufacturer"] + res["dmidecode -s system-product-name"]) if ip in self.key_not_login_lst: self.key_not_login_lst.pop(ip) self.can_key_login_lst[ip] = (port, keyfile, "root", "", 3, system_info, sys_hostname, sys_mac, sys_sn, machine_type) else: if ip not in self.key_not_login_lst.keys( ) and ip not in self.can_key_login_lst.keys(): self.key_not_login_lst[ip] = (port, keyfile) return self.can_key_login_lst, self.key_not_login_lst