Skip to content

Instantly share code, notes, and snippets.

@cwolferh
Forked from tomassedovic/virt
Created April 18, 2013 20:12
Show Gist options
  • Save cwolferh/5415859 to your computer and use it in GitHub Desktop.
Save cwolferh/5415859 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ $# == 0 ]]; then
echo Usage:
echo "virt <command> <vm> args"
echo Command is: ip, ssh, scp, sshfs
exit 1
fi
cmd="$1"; shift
destination="$1"
user=$(echo $destination | awk '{split($0,array,"@")} END {print array[1]}')
domain=$(echo $destination | awk '{split($0,array,"@")} END {print array[2]}')
if [ -z "$domain" ]; then
domain=$user
user=`whoami`
fi
ip=$(virt-ip $domain) || exit 1
shift
host="$user@$ip"
case "$cmd" in
ip)
echo $ip
;;
ssh)
virt-ssh "$host" $@
;;
scp)
virt-scp "$host" $@
;;
sshfs)
virt-sshfs "$host" $@
;;
esac
#!/bin/bash
# Inspired by Lars Kellogg-Stedman's script:
# http://blog.oddbit.com/post/getting-the-ip-address-of-a-libvirt-domain
function virsh_cmd {
virsh -c qemu:///system $@
}
which xmllint &>/dev/null || (echo "xmllint not found, install libxml2 to get it." 1>&2 && exit 1)
if [[ $# == 0 ]]; then
echo Enter a virtual machine name: 1>&2
echo 1>&2
virsh_cmd list 1>&2
exit 1
fi
# Quit if we got an unknown domain name. `virsh` will print the error messages.
domain_xml=$(virsh_cmd dumpxml $1) || exit 1
# Get the MAC address of the first interface.
mac=$(echo "$domain_xml" |
xmllint --xpath //interface'[1]/mac/@address' - |
sed 's/.*="\([^"]*\)"/\1/'
)
# Get the ip address assigned to this MAC from dnsmasq
ip=$(awk -vmac=$mac '$2 == mac {print $3}' /var/lib/libvirt/dnsmasq/default.leases)
echo $ip
#!/bin/bash
scp -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" $@
#!/bin/bash
ssh -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" $@
#!/bin/bash
sshfs -o "StrictHostKeyChecking=no" -o "UserKnownHostsFile=/dev/null" $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment