#!/bin/bash
# sed usage: https://www.tutorialspoint.com/unix/unix-regular-expressions.htm
net_dir='/Library/Preferences/VMware Fusion'
dhcp_dir='/Library/Preferences/VMware Fusion/vmnet8'
echo "back up networking file..."
sudo mv "$net_dir/networking" "$net_dir/networking.bak"
echo "back up dhcpd.conf file..."
sudo mv "$dhcp_dir/dhcpd.conf" "$dhcp_dir/dhcpd.conf.bak"
# recieve subset from use input
echo 'Please input ip subnet address (example: 192.168.88):'
read ip
# sed -i in mac invalid,use >> replace it
# mac sed -i "" 's/pattern/replace/g',"" must exist
# in furture, update it!
echo "update networking files for vmware8..."
sudo cat "$net_dir/networking.bak" | sed 's/\(VNET_8_HOSTONLY_SUBNET\) \([1-9]\{1,\}\.\)\{3\}/\1 '$ip'./g' >> "$net_dir/networking"
echo 'update subnet for hdcpd.conf...'
regex_str="s/\([1-9]\{1,\}\.\)\{3\}/'${ip}.'/g"
sudo cat "$dhcp_dir/dhcpd.conf.bak" | sed 's/\([1-9]\{1,\}\.\)\{3\}/'$ip'./g' >> "$dhcp_dir/dhcpd.conf"
# show info to prompt user for setting or updating static gateway and static dns
# in mac: \033[1;31m 1; must exist
echo -e "\033[1;31m Set IP SubNet: $ip \033[0m"
echo -e "\033[1;31m If you set static ip, please set or update your linux gatway and dns: $ip.2 \033[0m"
echo -e "\033[1;31m Please quit completely your vmware fusion and restart it. \033[0m"
sudo chmod u+x set_subnet.sh
sudo ./set_subnet.sh
根据脚本提示输入你要设置的网段即可。 例如设置成192.168.100 网段。 脚本支持设置的是C 类内网地址网段。不支持B类0