Mac wifi 自动控制脚本

#!/bin/bash
#author: 黄高明
#date: 2016-01-22
#qq: 530035210
#blog: https://my.oschina.net/pwd/blog 
#控制mac wifi脚本   
logdir=/data/log/shell          #日志路径
log=$logdir/log.log            #日志文件 
is_font=1                #终端是否打印日志: 1打印 0不打印 
is_log=0                 #是否记录日志: 1记录 0不记录
ssid="Raincy"
ssid_passwd="aa123456"
ip_info="192.168.0.120 255.255.255.0 192.168.0.1"
dnsgroup="25.25.25.1 223.5.5.5"
device="Wi-Fi"  
datef(){
date "+%Y-%m-%d %H:%M:%S"
}
    
print_log(){
if [[ $is_log -eq 1  ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1  ]];then
echo "[ $(datef) ] $1"
fi
}

wifi_action(){
case $1 in 

list)
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport scan

;;
start)
networksetup -setairportpower en0 on

;;
stop)
networksetup -setairportpower en0 off

;;
connect)
if [[ "$ssid" == "" && "$ssid_passwd" == "" ]];then
networksetup -setairportnetwork en0 $ssid $ssid_passwd
else
print_log  "ssid or password 为空."
fi

;;
set-statics-ip)
networksetup -setmanual "$device" $ip_info

;;
set-dhcp-ip)
 ipconfig set en0 DHCP

;;
list-devices)
networksetup -listallnetworkservices
;;
set-dns)
networksetup -setdnsservers "$device"  $dnsgroup
dscacheutil -flushcache 
;;
*)
echo "usage: wifi.sh list| start| stop| connect| set-statics-ip| set-dhcp-ip| list-devices| set-dns"

;;
esac
}
wifi_action "$1"

root权限下:

usage: wifi.sh list:查看Wi-Fi列表| start 启动| stop关闭|
connect连接某个ssid| set-statics-ip设置静态ip| set-dhcp-ip设置动态IP|
list-devices查看网络设备| set-dns设置dns

文章作者 明哥
文章地址 https://www.pvcreate.com/index.php/archives/52/
创建时间 2016-01-22
关注订阅 微信订阅号
开源项目 https://gitee.com/lookingdreamer
工具市场 https://gitee.com/lookingdreamer/SPPPOTools
MAC小技巧