When nslookup is missing but python is not

nslookup replacement

Here’s how to look up an IP address when you don’t have nslookup but you have Python:

python -c "import socket; print(socket.gethostbyname('google-public-dns-a.google.com'))"

Of course, this all goes on one line; I think that when you copy the code above it will have no line breaks.

Here’s how to look up a host name:

python -c "import socket; print(socket.gethostbyaddr('8.8.8.8')[0])"

In this case, gethostbyaddr returns a tuple so [0] is needed to get the first member of the tuple.


netstat replacement

Unfortunately, a replacement for netstat is not as concise in python – 241 lines at
https://github.com/da667/netstat. But if you have wget or curl and an Internet connection you can do something like this:

python -c "$( wget -O - https://raw.githubusercontent.com/da667/netstat/master/netstat.py )"

tinyurl.com to the rescue:

python -c "$( wget -O - https://tinyurl.com/netstat-py )"

updates

2018.05.01 – updated for python3 syntax