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, 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