Skip to content

Instantly share code, notes, and snippets.

@brycepg brycepg/
Last active Aug 11, 2017

What would you like to do?
Get IP Address from hostname bash function
# Get IP address from hostname using Python
# Args:
# $1 - hostname
# Returns:
# The ip address
ipfromhostname() {
local hostname="$1"
if [ -z "$hostname" ]; then
>&2 echo 'Must supply hostname as an argument'
return 1
local python_exists=$(type python 2>/dev/null)
if [ -z "$python_exists" ]; then
>&2 echo Requires python
return 1
python -c "import socket; print(socket.gethostbyname(\"$hostname\"))" 2>/dev/null
local ret="$?"
if [[ "$ret" != 0 ]]; then
>&2 echo Hostname not found
return 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.