Total Pageviews

Thursday 19 September 2019

在mac上,创建虚拟网卡(假设虚拟网卡名称为tap0)的一段代码

#!/bin/zsh

[[ "$UID" -ne "0" ]] && echo "You must be root. Goodbye..." && exit 1
echo "starting"
exec 4<>/dev/tap0
ifconfig tap0 10.10.10.1 10.10.10.255
ifconfig tap0 up
ping -c1 10.10.10.1
echo "ending"
export PS1="tap interface>"
dd of=/dev/null <&4 & # continuously reads from buffer and dumps to null
 
把上面这段代码保存为vif.sh ,
chmod 755 vif.sh
sudo ./vif.sh
 
运行ifconfig
显示:
...
tap0: flags=8843 mtu 1500
 ether ae:97:b0:a8:38:d9 
 inet 10.10.10.1 netmask 0xff000000 broadcast 10.10.10.255
 media: autoselect
 status: active
 open (pid 54334) 
 
说明虚拟网卡tap0被创建成功。
 
参考:https://stackoverflow.com/questions/87442/virtual-network-interface-in-mac-os-x 
 
注意:删除虚拟网卡tap0,可用命令sudo ifconfig tap0 down来实现。但是运行:
sudo ifconfig tap0 up并不能创建虚拟网卡tap0。
 
yudeMacBook-Air:~ brite$ sudo ifconfig tap1 up
Password:
ifconfig: interface tap1 does not exist
yudeMacBook-Air:~ brite$ 

此时,就需用上面的代码来创建虚拟网卡。
 
 

No comments:

Post a Comment