Skip to content

Instantly share code, notes, and snippets.

@meffie
Created February 16, 2017 14:48
Show Gist options
  • Save meffie/30165785ce5714c3ad33f7fb29174a04 to your computer and use it in GitHub Desktop.
Save meffie/30165785ce5714c3ad33f7fb29174a04 to your computer and use it in GitHub Desktop.
Get the ip4 addr of a domain with arp. (Helper for old versions of virsh).
#!/bin/sh
#
# Get the ipv4 address of a domain.
#
# Note: This script is obsoleted by modern versions of virsh.
#
name=$1
if [ "x$1" = "x" ]; then
echo "usage: virsh-domipaddr <name>" >&2
exit 1
fi
# Get the first mac address.
mac=`virsh domiflist $name | sed '1,2d; s/ */ /g' | cut -d' ' -f5`
if [ "x$mac" = "x" ]; then
echo "error: failed to get mac for domain '$name'" >&2
exit 1
fi
ip=`/usr/sbin/arp -n | awk -v mac=$mac '$3 == mac {print $1}'`
if [ "x$ip" = "x" ]; then
echo "error: failed to find ip for domain '$name', mac '$mac'" >&2
exit 1
fi
echo "$ip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment