Skip to content

Instantly share code, notes, and snippets.

@ksn135
Created April 11, 2017 22:31
Show Gist options
  • Save ksn135/8da9c8e44d038df5c4bfef99471777e7 to your computer and use it in GitHub Desktop.
Save ksn135/8da9c8e44d038df5c4bfef99471777e7 to your computer and use it in GitHub Desktop.
UNIFI Access Point shell interface, especially for glavkino.ru
#!/bin/bash
# UNIFI Access Point shell interface, v0.1
# (c) April 2017 by Serg N. Kalachev <[email protected]>
username=it
password=Wifi555out
baseurl=https://172.16.144.35:8443
site=default
cookie=/tmp/unifi_cookie
response=/tmp/unifi_response
curl_cmd="curl -s -o ${response} -c ${cookie} -b ${cookie} --insecure"
unifi() {
uri=$1
shift
[ "${uri:0:1}" != "/" ] && uri="/$uri"
json="$1"
shift
[ "$json" = "" ] && json="{}"
${curl_cmd} --data "$json" $baseurl/api$uri $@
ok=$(cat $response | jq '.meta.rc')
[ "$ok" != '"ok"' ] && echo "$uri$(cat $response | jq '.meta.msg')" && return -1
}
unifi login "{'username':'$username', 'password':'$password'}" -j
unifi s/$site/list/wlanconf
case "$1" in
new) if [ $# -ne 3 ] ; then
echo "USAGE: $0 new <SSID> <PASWD>"
return
fi
unifi s/$site/list/usergroup
ugid=$(cat $response | jq '.data[0]._id')
unifi s/$site/list/wlangroup
wgid=$(cat $response | jq '.data[0]._id')
unifi s/$site/add/wlanconf "{'name':'$2', 'x_passphrase':'$3', 'usergroup_id': $ugid, 'wlangroup_id': $wgid, 'enabled': true, 'hide_ssid': false, 'is_guest': false,
'security': 'wpapsk', 'wep_index': 1, 'wpa_mode':'wpa2', 'wpa_enc':'ccmp', 'vlan_enabled': false, 'vlan': '109', 'uapsd_enabled': false, 'schedule_enabled': false,
'mac_filter_enabled': false, 'mac_filter_policy': 'deny', 'mac_filter_list': [], 'schedule': [], 'dtim_mode': 'default', 'dtim_ng': 1, 'dtim_na':1}"
;;
del) if [ $# -ne 2 ] ; then
echo "USAGE: $0 del <SSID>"
return
fi
ssid=$(cat $response | jq '.data | .[] | select(.name == "'$2'") | ._id')
ssid=$(eval echo $ssid)
unifi s/$site/del/wlanconf/$ssid "" -X DELETE
;;
show) if [ $# -ne 2 ] ; then
echo "USAGE: $0 show <SSID>"
return
fi
cat $response | jq '.data | .[] | select(.name == "'$2'")'
;;
list) cat $response | jq '.data | .[] | { SSID: .name , password: .x_passphrase }'
;;
*) echo "USAGE: $0 <action:list|show|new|del> [<SSID> [<PASWD>]]"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment