Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Created March 17, 2016 17:56
Show Gist options
  • Select an option

  • Save ehzawad/0d6ca719a98e1b4c4c87 to your computer and use it in GitHub Desktop.

Select an option

Save ehzawad/0d6ca719a98e1b4c4c87 to your computer and use it in GitHub Desktop.
#!/bin/bash
# determine IP address
vmm=
if [ "$vmm" == "xen" ]; then
# this is EC2, fetch public IP from AWS
declare inet=$(wget --output-document=- --quiet --timeout=5 --tries=10 http://169.254.169.254/latest/meta-data/public-ipv4)
declare regex='([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'
else
declare inet=$(ip -f inet addr show eth1 2> /dev/null | grep inet)
declare regex='inet ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'
fi
if [[ "$inet" == "" ]]; then
declare addr="missing network adapter"
elif [[ "$inet" =~ $regex ]]; then
declare addr=${BASH_REMATCH[1]}
else
declare addr="no IP address"
fi
# display IP address
if [ "$1" == "-xml" ]; then
echo "<tool>IP address</tool>"
echo "<txt>$addr</txt>"
else
echo $addr
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment