• 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#*"}
    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
    

      

  • 相关阅读:
    爬虫之暴力字典生成器
    爬虫之自动生成url
    数字、大小写字母的字符编码
    对avalonjs的研究
    求墙之间有多少水洼
    2.在centos7虚拟机搭建nginx网站
    P1250 种树
    暂时用笔记
    羊村的OI题解
    P1083 借教室
  • 原文地址:https://www.cnblogs.com/shaohef/p/11968624.html
Copyright © 2020-2023  润新知