Mac wifi 自动控制脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
#author: 黄高明
#date: 2016-01-22
#qq: 530035210
#blog: http://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

坚持原创技术分享,您的支持将鼓励我继续创作!