local host ip get

#include "winsock2.h"
#include "Iphlpapi.h"

#pragma comment(lib,"Iphlpapi.lib")

#pragma comment(lib,"ws2_32.lib")




PMIB_TCPTABLE MyGetTcpTable(BOOL bOrder)
 {
     PMIB_TCPTABLE pTcpTable = NULL;
     DWORD dwActualSize = 0;
 
 
     if(::GetTcpTable(pTcpTable, &dwActualSize, bOrder) == ERROR_INSUFFICIENT_BUFFER)
     {
         pTcpTable = (PMIB_TCPTABLE)::GlobalAlloc(GPTR, dwActualSize);
         if(::GetTcpTable(pTcpTable, &dwActualSize, bOrder) == NO_ERROR)
             return pTcpTable;
         ::GlobalFree(pTcpTable);
     }
     return NULL;
 }
 
 void MyFreeTcpTable(PMIB_TCPTABLE pTcpTable)
 {
     if(pTcpTable != NULL)
         ::GlobalFree(pTcpTable);
 }
 
 char* Tcp()
 {
     PMIB_TCPTABLE pTcpTable = MyGetTcpTable(TRUE);
     char    szBuf[14096]="";
     if(pTcpTable != NULL)
     {
         char    strState[12800];
         struct  in_addr    inadLocal, inadRemote;
         DWORD   dwRemotePort = 0;
         char    szLocalIp[128];
         char    szRemIp[128];
 
         sprintf(szBuf,"TCP TABLE\n");
         for(UINT i = 0; i < pTcpTable->dwNumEntries; ++i)
         {
             // ??
             switch (pTcpTable->table[i].dwState)
             {
             case MIB_TCP_STATE_CLOSED:
                 strcpy(strState, "CLOSED");
                 break;
             case MIB_TCP_STATE_TIME_WAIT:
                 strcpy(strState, "TIME_WAIT");
                 break;
             case MIB_TCP_STATE_LAST_ACK:
                 strcpy(strState, "LAST_ACK");
                 break;
             case MIB_TCP_STATE_CLOSING:
                 strcpy(strState, "CLOSING");
                 break;
             case MIB_TCP_STATE_CLOSE_WAIT:
                 strcpy(strState, "CLOSE_WAIT");
                 break;
             case MIB_TCP_STATE_FIN_WAIT1:
                 strcpy(strState, "FIN_WAIT1");
                 break;
             case MIB_TCP_STATE_ESTAB:
                 strcpy(strState, "ESTAB");
                 break;
             case MIB_TCP_STATE_SYN_RCVD:
                 strcpy(strState, "SYN_RCVD");
                 break;
             case MIB_TCP_STATE_SYN_SENT:
                 strcpy(strState, "SYN_SENT");
                 break;
             case MIB_TCP_STATE_LISTEN:
                 strcpy(strState, "LISTEN");
                 break;
             case MIB_TCP_STATE_DELETE_TCB:
                 strcpy(strState, "DELETE");
                 break;
             default:
                strcpy(strState,"Error: unknown state!\n");
                 break;
             }
             // 本地地址
             inadLocal.s_addr = pTcpTable->table[i].dwLocalAddr;
              
             // ?程端口
             if (strcmp(strState, "LISTEN") != 0)
             {
                 dwRemotePort = pTcpTable->table[i].dwRemotePort;
             }
             else
                 dwRemotePort = 0;
             // ?程IP地址
             inadRemote.s_addr = pTcpTable->table[i].dwRemoteAddr;
 
             if(inadLocal.S_un.S_addr != 0 && inadLocal.S_un.S_addr != 0x100007f)
             {
                 strcpy(szLocalIp, inet_ntoa(inadLocal));
                 strcpy(szRemIp, inet_ntoa(inadRemote));
                 sprintf(szBuf,"%20s %10u %20s %10u %s\n",
                     szLocalIp,  ntohs((unsigned short)(0x0000FFFF & pTcpTable->table[i].dwLocalPort)),
                     szRemIp, ntohs((unsigned short)(0x0000FFFF & dwRemotePort)),
                     strState);
                 return szLocalIp;
             }
         }
         MyFreeTcpTable(pTcpTable);
     }
     return "127.0.0.1";;
 }


#!/usr/local/python # -- coding: utf-8 -- import requests import json import logging # Function to get the vCenter server session def get_vc_session(vcip, username, password): s.post('https://' + vcip + '/rest/com/vmware/cis/session', auth=(username, password)) return s # Function to get all the VMs from vCenter inventory def get_vms(vcip,host=''): vms = s.get('https://' + vcip + '/rest/vcenter/vm'+"?filter.hosts="+str(host)) return vms def get_hosts(vcip,cluster=''): result = s.get('https://' + vcip + '/rest/vcenter/host'+"?filter.clusters="+str(cluster)) return result def get_clusters(vcip,dc=''): clusters = s.get('https://' + vcip + '/rest/vcenter/cluster'"?filter.datacenters="+str(dc)) return clusters def get_clusterinfo(vcip,cluster): clusterinfo = s.get('https://' + vcip + '/rest/vcenter/cluster/'+cluster) return clusterinfo def get_datacenters(vcip): datacenters = s.get('https://' + vcip + '/rest/vcenter/datacenter') return datacenters def get_datacenterinfo(vcip,datacenter): datacenterinfo = s.get('https://' + vcip + '/rest/vcenter/datacenter/'+datacenter) return datacenterinfo # Function to power on particular VM def poweron_vm(vmmoid, vcip): s.post('https://' + vcip + '/rest/vcenter/vm/' + vmmoid + '/power/start') # Function to power off particular VM def poweroff_vm(vmmoid, vcip): s.post('https://' + vcip + '/rest/vcenter/vm/' + vmmoid + '/power/stop') def getfolder(vcip): folderinfo = s.get('https://' + vcip + '/rest/vcenter/folder') print(folderinfo) print(dir(folderinfo)) return json.loads(folderinfo.content).values()[0] def gethostlistinfo(vcip): info=get_datacenters(vcip) for dc in json.loads(info.content).values()[0]: dcname=dc.get("name") dcvalue = dc.get("datacenter") #print(dcname,dcvalue) info=get_clusters(vcip, dc=dcvalue) for cls in json.loads(info.content).values()[0]: clustername=cls.get("name") clustervalue = cls.get("cluster") #print(dcname,clustername,clustervalue) info=get_hosts(vcip,cluster=clustervalue) for h in json.loads(info.content).values()[0]: hostip=h.get("name") hostvalue = h.get("host") constate = h.get("connection_state") hostPowerState = h.get("power_state") #print(vcip,dcname,clustername,hostip,constate,power_state) info=get_vms(vcip,hostvalue) for v in json.loads(info.content).values()[0]: vmname = v.get("name") vmvalue = v.get("vm") vmMemSize = v.get("memory_size_MiB") vmCpuCount = v.get("cpu_count") vmPowerState = v.get("power_state") print(vcip,dcname,clustername,hostip,constate,hostPowerState,vmname,vmMemSize,vmCpuCount,vmPowerState) ###main#### vcip='172...' username="administrator@vsphere.local" password="YVd5******" logging.captureWarnings(True) global s s = requests.Session() s.verify = False s = get_vc_session(vcip, username, password) info=gethostlistinfo(vcip) 解释这段代码并改正格式
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值