Get IPs of vCenter networks using pyVmomi - python

I use pyVmomi to create VM on our vCenter. We have a few networks, like 'PRD-DB'. I can change the network interface of a VM to 'PRD-DB' using pyVmomi.
I know that this network address is 10.125.10.0/24. But I can't find a way of getting this network IP address using pyVmomi. What links IPs to networks ?
[EDIT] To be more precise : How can I retrieve the list of available VLANs, that I can assign to the network card of a VM ? And can I retrieve the network addresses corresponding to these VLANs ?

Are you trying to just get the ip address for each vm?
If so you can use CreateContainerView on the vim.VirtualMachine to get the IP addresses of each VM from your vCentre via the guest object. Although you will need to have VMWare Tools installed to gather most of the information within the guest object.
serviceInstance = SmartConnect(host=host,user=user,pwd=password,port=443)
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
vm_view = content.viewManager.CreateContainerView(content.rootFolder,[vim.VirtualMachine],True)
# Loop through the vms and print the ipAddress
for vm in vm_view.view:
print(vm.guest.ipAddress)
If you are trying to do a more advanced approach I would recommend following the getvnicinfo.py example provided on the vmware github page. This actually goes through a more detailed view getting the information for each VM. Although this doesn't seem to provide the IP Address.
To view the available vlans for a host follow the below example.
hosts = content.viewManager.CreateContainerView(content.rootFolder,[vim.HostSystem],True)
for host in hosts.view:
for network in host.network:
switch = network.config.distributedVirtualSwitch
for portgroup in switch.portgroup:
print(portgroup.name) # This will print the vlan names

Related

Outbound IP for Python Azure Function - requests IP not match list of function outbound IP

I ve created Python function on Azure that call external API service which allows to access only for whitelisted IPs.
Based on Microsoft documentation (https://learn.microsoft.com/en-us/azure/azure-functions/ip-addresses) I found all OutboundIPAddresses and PossibleOutboundAddresses and whitelisted all of them. Despite of IPs have been whitelisted I keep receiving 403 Forrbiden error from the service.
I also verified IP address of the request by adding following code to the function:
ip = requests.request('GET','https://api.ipify.org').text
logging.info('Request send from IP: {}'.format(ip))
It seems that actual outbound IP address is diffrend then specified on the OutboundIPAddress and PossibleOutboundAddresses lists.
I would appreciate your help.
Per our documentation
When a function app that runs on the Consumption plan or the Premium
plan is scaled, a new range of outbound IP addresses may be assigned.
When running on either of these plans, you may need to add the entire
data center to an allowlist.
If you need to add the outbound IP addresses used by your function apps to an allowlist, another option is to add the function apps' data center (Azure region) to an allowlist. You can download a JSON file that lists IP addresses for all Azure data centers. Then find the JSON fragment that applies to the region that your function app runs in.

How to get an public address for Azure SDK Python, before VM creation

I'm aware that the public ip is only generated after you set the vm. But is there a way to force the public ip to be dinamically created BEFORE the vm creation.
Mainly, I need to use the ip to generate the machine's name.
Or, is there a way to update the machine's name, before creation? I can find any information on this.
You can create the public IP before you create the VM. Then when creating the VM, associate the public IP to the VM's NIC.
Add Andrés Nava - .NET's answer. You should create a static Public IP before you create VM. Please refer to this SDK article.
Gets or sets PrivateIP allocation method (Static/Dynamic)
When you create a static Public IP, it is associated a Public IP address and the IP address does not change. But if you create a Dynamic Public IP, it is not associated a IP address when you create the resource and the IP address will change when you stop VM.

Get Hostname from IP Address

My problem is to match IP address to its hostname.
If the IP address is a public IP then a simple socket.gethostbyaddr(ip) solves it but
my problem is with private IP i.e. IP of a network.
How can I get host names from a network IP address? Is there a way to find out hostname or not?
I'm trying to do this on Linux.
You've already got the Python code required
socket.gethostbyaddr(ip)
What you need is on the infrastructure side of things. To get an internal hostname, you need to query the internal DNS server for the network in question. Larger networks almost always have internal DNS services but some smaller network don't since they rely on other means (direct IP, NETBIOS, Bonjour, etc.) to find various resources.
TL:DR : You need to query the internal DNS services for the network in question

How to get the visitor's IP address from the DNS request?

I am running my own DNS name server which I have developed in python. I have a registered domain name say "abc.in". The name servers of the domain are set to my computer's IP address (given by ISP). Now whenever anyone access the domain name I am getting the visitor's ISP's IP address. I want to get the visitor's IP address. Actually I want to make a small CDN like project, also I am not able to find any good source of information. Is it possible to get the visitor's IP address, I am developing it in Python ?
I had read in CloudFlare's blog that they get the visitor's geographic information (IP address) from the initial DNS lookups.
You can use socket function in python. Like following.
>>> socket.getaddrinfo
What you're trying is simply not possible. You'll always get the connection from the resolver, not the final client.

Http connect request from multiple IP address to destination in python

conn=httlib.HTTPConnection(self.proxy)
Self.proxy has destination ip and port.
I want to do multiple connection from multiple IP addresses to destination
How to specify the source IP while connect request.Please help me out.
Thanks in Advance.
I assume that you have multiple network connections on the same computer (i.e. a wired and wireless connection) and you want to make sure that your connect goes over a specific interface.
In general you cannot do this. How your traffic is sent to a specific ip address, and therefore what source ip address it shows, is determined by your operating system's routing tables. As you haven't specified what operating system this I can't go into more detail.
You may be able to do this using some of the more advanced routing configuration, but that's an operating system level problem and can't be done through Python.
I got the solution but not 100%
Requirement: Has to send request from 10 Ip address to one destination.
Achieved the same through the following API
class httplib.HTTPConnection(host[, port[, strict[, timeout[, source_address]]]])
here, we can mention the last parameter source IP
Like, httlib.HTTPConnection(dest_ip, dest_port, src_ip)
For Example:httlib.HTTPConnection("198.168.1.5",8080,"198.168.1.1")
created the connection under for loop for 10 unique src ip addresses.
Output: Connected to destination with 10 different port number with same IP address. I don't know why it happens like this.
Problem solved. Thanks for all.

Categories

Resources