connect via ssh to virtualbox guest vm without knowing ip address

cat ssh-vm

HOSTIP=`ip route get 1 | awk "{match($0, /.+srcs([.0-9]+)/, a);print a[1];exit}"`
HOST_NETS=`ip addr |grep 192.168.3.33 | awk "{match($0, /.+inets([/.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#*"}
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 in networks($NETS) firstly by run:"
echo "nmap -sP $NETS"
IP=`ip neighbor | grep "$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 "$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