Obtaining destination ip address in Powershell (Invoke-WebRequest) - powershell

I'm writing script for automation of TCP connections and I have a question. Is there an option to get destination IP address, when you call (for example):
Invoke-WebRequest google.com
Problem is, when I call this command, destination IP address is always different, so I can't use command like ''test-connection''. I would like to hear further explanation. I know that Google has multiple public addresses, but why are they different when I call Invoke-WebRequest, or Test-Connection?
Example:
1.
Destination IP from wireshark capture
2.
IP address from Test-Connection command:
google.com 172.217.18.78
Greetings

Google has a huge userbase. They want to be reachable via multiple addresses, as that provides robust connections and some load balancing too. The technique is called DNS Round Robin. In case one of the multiple IP addresses doesn't work, most modern browsers will automatically try and use other addresses.
If you would like to test a connection to particular IP, you could do a name lookup and pick one of the results. Like so,
# Get a list of all Google IPs
$googles = [Net.Dns]::GetHostAddresses("www.google.com")
# Use IP address for the 1st entry
test-connection $googles[0].IPAddressToString

Related

Powershell WMI Win32_NetworkAdapterConfiguration EnableDHCP to Remove IP Gateway doesn't work

I'll have some computers with 8 network adapters, but only one is configured with the routed VLAN.
To identify it, I wrote a little powershell script that is a loop that assigns the IP address, test the Gateway connectivity until it founds the correct interface.
But when I try to release the IP address using enableDHCP method it will never clears the IP Address nor the Gateway.
I already tried releaseDhcpLease, for individual or for ALL interfaces, but without result ...
I've tried some tricks published here -> https://social.technet.microsoft.com/Forums/ie/en-US/94f0f04a-1669-4276-b529-e68edffd9aff/how-to-remove-default-gateway?forum=winserverpowershell
But none works..
the only effective method for to release the IP configuration was using "netsh command", for example:
netsh int ipv4 -name='Ethernet 3' addr=172.22.33.123 gateway=all
How can I do the same using Powershell + WMI commands? I may invoke "netsh", but script becomes little more complex than I desire.
Regards

strange behaviour Pinging Public IP

I've tried this on 2 different networks:
Set router to respond to ping on internet port.
Lookup public IP on www.whatsmyip.com
Use PuTTy to ssh into another server and pinging the public IP from there.
In one network 1 this works, in network 2 it won't. So the question is. What can cause this behaviour?
The public IP address (the address that external sites see your requests coming from, i.e. what whatismyip will tell you) may not be one that the router is directly reachable on, for example if Carrier-Grade NAT is deployed.
ISPs may use CGNAT if they don't have enough IPv4 addresses, in order to share a single address between multiple clients.
One way to tell if this is happening would be to compare whatismyip.com to the address the router says it has on its WAN/internet interface. If they're different (and the router is connected straight to the ISP), it's probably CGNAT at play.

Block facebook.com using openwrt router

I am using OpenWRT router. I need to block a URL or multiple URLs (Not IP) for specific time. for example, I want to block facebook.com so that clients of this router cant access the website. firewall rules should have the option to do that but I dont know how to do that.
Here is one way to block by domain name rather than by IP address.
The main reason of why you need such a complicated method is that each domain name (e.g. facebook.com) may be resolved as different IP address at any given time. So, we need to keep a list of resolved IP addresses and add iptables rules based on this list.
First, you should enable logging in dnsmasq config:
uci set dhcp.#dnsmasq[0].logqueries=1
uci commit dhcp
/etc/init.d/dnsmasq restart
This will give you log entries like:
daemon.info dnsmasq[2066]: reply facebook.com is 31.13.72.36
Now, you just have to constantly parse syslog and add corresponding iptables rules like this (note that you most likely need a more versatile script and ipset for better performance):
logread -f | awk '/facebook.com is .*/{print $11}' | while read IP; do iptables -I OUTPUT -d $IP -j DROP; done

How exactly does mDNS resolve addresses?

Once a service is discovered through DNS-SD, how exactly does the address of that host get resolved, and does it take significantly more time/overhead?
Also, if I am using JmDNS or Bonjour there are call-backs for both serviceFound and serviceResolved. If I am just interested in the IP address of the device publishing a certain service, is there a faster/more efficient way of getting the address than going through both serviceFound and serviceResolved?
Thanks
DNS-SD uses Multicast DNS (MDNS) which works by sending DNS packets over UDP to a certain multicast address. All mdns-capable hosts in the network also listen to this address. It uses UDP so, it's quite low overhead. Also, the clients are designed in a way that the amount of chatter on the network is kept to a minimum, by using extensive caching.
Service discovery is a two step process. The first step is finding the names of all hosts providing a certain service (e.g. printing). This will not yet give you the ip address, instead it gives you the mdns name (ending with .local). This is because the ip could possibly change, whereas the name will not.
The second step in service discovery is to resolve the .local name of the host over mdns. You ask via multicast who foo.local is, foo.local will see that packet, and respond via broadcast with its ip address, port number and other information.

Get Azure public IP address from deployed app

I'm implementing the PASV mode in a FTP server, and I send to the client the IP address and port of the data end point. This is stupid because the IP is actually where the client is already connecting, so there ire two options:
How could I get the public IP
address from a given instance? Not
the VIP, but the public one.
How could I get the original target
IP address that the user used from
a Socket object? Considering routers and load balancers in the middle :P
An answer to any of this questions would do, although there is another way that could work... may I get the public IP address doing a DNS look up of myapp.cloudapp.net?
A fourth option would be use the Azure Management API library... but, too much trouble :P.
Cheers.
Not sure if you ever figured this out, but here's my take on it. The individual role instances are all behind the Windows Azure load balancer and have no idea what the original, outward-facing IP address is. Also, there's no Management API call that returns IP address - Get Deployment returns the URL but not the IP address. I think the only option is going to be a dns lookup.
Having said that: I don't think you can host a passive ftp server in your role instance (at least not elegantly). You may open up to 25 input endpoints on your role (up from 5 - see my recent blog post about this update), but there's manual work involved in the configuration. I don't know if your ftp application lets you limit your port range to such a small number of ports. Also:
You'd have to define each port as its own input endpoint (this is the manual labor part I mentioned) - input endpoints don't allow a port range to be specified, unlike the internal endpoints.
You'd have to specify the port number that's used internally, and the port numbers would need to be sequential
One last thing on ftp: you should be able to host an sftp server with no trouble, since all traffic comes through one port.
The hack that I'm contemplating right now is to retrieve http://www.icanhazip.com/. It isn't elegant and is subject to the availability of that service, but it gets the job done. A better solution would be appreciated!