connect via ssh to virtualbox guest vm without knowing ip address

cat ssh-vm

HOSTIP=`ip route get 1 | awk '{match($0, /.+src\s([.0-9]+)/, a);print a[1];exit}'`
HOST_NETS=`ip addr |grep 192.168.3.33 | awk '{match($0, /.+inet\s([/.0-9]+)/, a);print a[1];exit}'`
HOST_NETS=${HOST_NETS:-192.168.3.0/24}
NETS=${NETS:-$HOST_NETS}
# MAC=${MAC:-08:00:27:7c:55:26}
VBoxManage list vms
USR=${USR:-mxh}
VM1=$(VBoxManage list vms | head -n 1 | awk '{ print $1 }')
VM1=${VM1:-ubuntu-18.04}
VM1=${VM1%\"*}
VM1=${VM1#*\"}
VMID1=$(VBoxManage list vms | head -n 1 | awk '{ print $2 }')
VMID1=${VMID1%\}*}
VMID1=${VMID1#*\{}
VMNAME=${VMNAME:-$VM1}
echo "VM name is: $VMNAME, UUID is: $VMID1"
NIC_MAC=`VBoxManage showvminfo $VMNAME --details 2>&1 | grep 'NIC.*MAC' | sed -re 's/.*MAC: (.+), Attachment.*/\1/' -e 's/(\w{2})/\1:/g' -e 's/:$//'`
MAC=${MAC:-$NIC_MAC}
echo "VM nic MAC is: $MAC"
# VBoxManage guestproperty enumerate $VMNAME
# VBoxManage guestproperty get $VMNAME "/VirtualBox/GuestInfo/Net/1/V4/IP" | awk '{ print($2); }')
# ssh -l USERNAME $VM_IP
# VM_IP=$(vboxmanage guestproperty get "VM_NAME" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print($2); }')
# VBoxManage guestproperty get "CHICAGOVPN" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $2 }'

echo "Please to frush arp table in networks($NETS) firstly by run:"
echo "nmap -sP $NETS"
IP=`ip neighbor | grep -i "$MAC" | cut -d" " -f1`
VMIP=${VMIP:-IP}
echo "VM IP is: $IP"
if [ !-n $IP ]; then
  echo "VM IP is NULL, exit"
  exit 1
fi
echo "Log VM: ssh $USR@$IP"
ssh $USR@$IP
exit 0
# fping -f $NETS
# https://stackoverflow.com/questions/13552881/can-i-determine-the-current-ip-from-a-known-mac-address
IP=`nmap -sP $NETS >/dev/null && arp -an | grep $MAC | awk '{print $2}' | sed 's/[()]//g'`
echo $IP

# https://serverfault.com/questions/540107/connect-via-ssh-to-virtualbox-guest-vm-without-knowing-ip-address
# linux vboxmanage show  nic  mac address

  

  

NETS=192.168.3.0/24
# MAC=${MAC:-08:00:27:7c:55:26}
VBoxManage list vms
USR=${USR:-mxh}
VM1=$(VBoxManage list vms | head -n 1 | awk '{ print $1 }')
VM1=${VM1:-ubuntu-18.04}
VM1=${VM1%\"*}
VM1=${VM1#*\"}
VMNAME=${VMNAME:-$VM1}
echo "VM name is: $VMNAME"
NIC_MAC=`VBoxManage showvminfo $VMNAME --details 2>&1 | grep 'NIC.*MAC' | sed -re 's/.*MAC: (.+), Attachment.*/\1/' -e 's/(\w{2})/\1:/g' -e 's/:$//'`
MAC=${MAC:-$NIC_MAC}
echo "VM nic MAC is: $MAC"
# VBoxManage guestproperty enumerate $VMNAME
# VBoxManage guestproperty get $VMNAME "/VirtualBox/GuestInfo/Net/1/V4/IP" | awk '{ print($2); }')
# ssh -l USERNAME $VM_IP
# VM_IP=$(vboxmanage guestproperty get "VM_NAME" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print($2); }')
# VBoxManage guestproperty get "CHICAGOVPN" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $2 }'

echo "Please to frush arp table firstly by run:"
echo "nmap -sP $NETS"
IP=`ip neighbor | grep -i "$MAC" | cut -d" " -f1`
VMIP=${VMIP:-IP}
echo "VM IP is: $IP"
if [ -n $IP ]; then
  echo "VM IP is NULL, exit"
  exit 1
fi
echo "Log VM: ssh $USR@$IP"
ssh $USR@$IP
exit 0
# fping -f $NETS
# https://stackoverflow.com/questions/13552881/can-i-determine-the-current-ip-from-a-known-mac-address
IP=`nmap -sP $NETS >/dev/null && arp -an | grep $MAC | awk '{print $2}' | sed 's/[()]//g'`
echo $IP

# https://serverfault.com/questions/540107/connect-via-ssh-to-virtualbox-guest-vm-without-knowing-ip-address
# linux vboxmanage show  nic  mac address

  

 

posted @ 2019-12-02 05:01  lvmxh  阅读(394)  评论(0编辑  收藏  举报