small tools for libvirt. vssh, vmove
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4 KiB

#!/bin/bash
connection=$1
host=${connection##*@}
#user=${connection%%@*}
domain=${host}
vip=0.0.0.0
if ! virsh --connect=qemu:///system list --all |grep " ${domain} "
then
echo "not a existing domain"
exit 1
fi
if ! virsh --connect=qemu:///system list |grep " ${domain} "
then
echo "domain is not started"
echo "virsh --connect=qemu:///system start ${domain}"
exit 1
fi
if ! virsh --connect=qemu:///system guestinfo "${domain}" --interface
then
echo "No guest agent on this guest. let's try to find host in dhcp leases."
if virsh --connect=qemu:///system net-dhcp-leases default |grep "${domain}"
then
virsh --connect=qemu:///system net-dhcp-leases default |grep "${domain}" |cut -c '50-80' |sed -e 's/ //g' |cut -d'/' -f 1
vip=$(virsh --connect=qemu:///system net-dhcp-leases default |grep "${domain}" |cut -c '50-80' |sed -e 's/ //g' |cut -d'/' -f 1)
else
echo "no ip. script error."
exit 1
fi
else
virsh --connect=qemu:///system guestinfo "${domain}" --interface |grep 'if.1.addr.0.addr' |cut -d':' -f 2
vip=$(virsh --connect=qemu:///system guestinfo "${domain}" --interface |grep 'if.1.addr.0.addr' |cut -d':' -f 2)
fi
tee ~/.ssh/config.d/"${domain}" <<EOF
Host ${domain}
Hostname ${vip}
EOF
chmod 640 ~/.ssh/config.d/"${domain}"
# local ansible inventory
if [ -d /datas/git/ansible/inventory/ ]
then
tee /datas/git/ansible/inventory/"${domain}" <<EOF
[${domain}]
${vip}
EOF
fi
ssh "${connection}"