Templates
wpa_action-1.0.0.sh.j2
Synopsis: Template wpa_action-1.0.0.sh.
Description of the template.
[templates/wpa_action-1.0.0.sh.j2]
1#!/bin/sh
2# {{ ansible_managed }}
3
4version="1.0.0"
5ifname=$1
6cmd=$2
7# TODO: test prams
8
9{% if wpacli_action_script_log_to_file %}
10logtofile="1"
11{% else %}
12logtofile="0"
13{% endif %}
14logfile="{{ wpacli_action_script_logfile }}"
15
16if [ "$logtofile" = "1" ]; then
17 my_date=`date +"%b %d %T"`
18 printf "$my_date $ifname: $cmd \n" >> $logfile
19fi
20
21# wpa_supplicant reports connection to SSID. Start dhclient and
22# restart routing
23if [ "$cmd" = "CONNECTED" ]; then
24 if [ "$logtofile" = "1" ]; then
25 ssid=`wpa_cli -i$ifname status | grep ^ssid= | cut -f2- -d=`
26 printf "$my_date $ifname: SSID: $ssid \n" >> $logfile
27 fi
28 message=`/etc/rc.d/dhclient forcestart $ifname 2>&1` # NOTE 1
29 if [ "$logtofile" = "1" ]; then
30 printf "$my_date $ifname: dhclient start: $message \n" >> $logfile
31 fi
32 message=`/etc/rc.d/routing restart 2>&1`
33 if [ "$logtofile" = "1" ]; then
34 printf "$my_date $ifname: routing restart: $message \n" >> $logfile
35 fi
36fi
37
38# wpa_supplicant reports disconnection from SSID. Stop dhclient and
39# restart routing
40if [ "$cmd" = "DISCONNECTED" ]; then
41 message=`/etc/rc.d/dhclient forcestop $ifname`
42 if [ "$logtofile" = "1" ]; then
43 printf "$my_date $ifname: dhclient forcestart: $message \n" >> $logfile
44 fi
45 message=`/etc/rc.d/routing restart 2>&1`
46 if [ "$logtofile" = "1" ]; then
47 printf "$my_date $ifname: routing restart: $message \n" >> $logfile
48 fi
49fi
50
51exit 0
52
53# NOTE 1
54# We don't want /etc/network.subr to handle DHCP and instruct
55# ifconfig, in rc.conf, to configure WPA only.
56# ifconfig_wlan1="WPA"
57# When we use "/etc/rc.d/dhclient start wlan1" it complains
58# 'wlan1' is not a DHCP-enabled interface
59# Hence we use /etc/rc.d/dhclient forcestart $ifname in this script
60
61# NOTE 2
62# Example how to activate the script.
63# wpa_cli -B -i wlan1 -a /root/bin/wpa_action.sh
64
65# EOF
wpa_action-1.1.0.sh.j2
Synopsis: Template wpa_action-1.1.0.sh.
Description of the template.
[templates/wpa_action-1.1.0.sh.j2]
1#!/bin/sh
2# {{ ansible_managed }}
3
4version="1.1.0"
5ifname=$1
6cmd=$2
7# TODO: test prams
8
9{% if wpacli_action_script_log_to_file %}
10logtofile="1"
11{% else %}
12logtofile="0"
13{% endif %}
14logfile="{{ wpacli_action_script_logfile }}"
15
16{% if wpacli_action_script_ntp_set %}
17ntp_set="1"
18{% else %}
19ntp_set="0"
20{% endif %}
21ntp_server="{{ wpacli_action_script_ntp_server }}"
22ntpdate_flags="{{ wpacli_action_script_ntpdate_flags }}"
23
24# functions
25log() {
26 if [ "$logtofile" = "1" ]; then
27 my_date=`date +"%b %d %T"`
28 printf "$my_date $ifname: $cmd: $message \n" >> $logfile
29 fi
30}
31
32routing_restart() {
33 cmd="/etc/rc.d/routing restart"
34 message=`$cmd 2>&1`
35 log
36}
37
38dhclient_forcestart() {
39 cmd="/etc/rc.d/dhclient forcestart $ifname" # NOTE 1
40 message=`$cmd 2>&1`
41 log
42}
43
44dhclient_forcestop() {
45 cmd="/etc/rc.d/dhclient forcestop $ifname"
46 message=`$cmd 2>&1`
47 log
48}
49
50ntpd_stop() {
51 cmd="/etc/rc.d/ntpd stop"
52 message=`$cmd 2>&1`
53 log
54}
55
56ntpd_start() {
57 cmd="/etc/rc.d/ntpd start"
58 message=`$cmd 2>&1`
59 log
60}
61
62ntpdate_settimeofday() {
63 cmd="/usr/sbin/ntpdate $ntpdate_flags $ntp_server"
64 message=`$cmd 2>&1`
65 log
66}
67
68log_SSID() {
69 if [ "$logtofile" = "1" ]; then
70 ssid=`wpa_cli -i$ifname status | grep ^ssid= | cut -f2- -d=`
71 my_date=`date +"%b %d %T"`
72 printf "$my_date $ifname: SSID: $ssid \n" >> $logfile
73 fi
74}
75
76# log interface and command
77if [ "$logtofile" = "1" ]; then
78 my_date=`date +"%b %d %T"`
79 printf "$my_date $ifname: $cmd \n" >> $logfile
80fi
81
82# wpa_supplicant reports connection to SSID. Start dhclient and
83# restart routing
84if [ "$cmd" = "CONNECTED" ]; then
85 log_SSID
86 dhclient_forcestart
87 routing_restart
88 if [ "$ntp_set" = "1" ]; then # NOTE 3
89 ntpd_stop
90 ntpdate_settimeofday
91 ntpd_start
92 fi
93fi
94
95# wpa_supplicant reports disconnection from SSID. Stop dhclient and
96# restart routing
97if [ "$cmd" = "DISCONNECTED" ]; then
98 dhclient_forcestop
99 routing_restart
100fi
101
102exit 0
103
104# NOTE 1
105# We don't want /etc/network.subr to handle DHCP. Therefor we instruct
106# ifconfig, in rc.conf, to configure WPA only
107# ifconfig_wlan1="WPA"
108# When we use "/etc/rc.d/dhclient start wlan1" it complains
109# 'wlan1' is not a DHCP-enabled interface
110# Hence we use /etc/rc.d/dhclient forcestart $ifname in this script.
111
112# NOTE 2
113# Example how to activate the script
114# wpa_cli -B -i wlan1 -a /root/bin/wpa_action.sh
115
116# NOTE 3
117# In a wifi-only system, /etc/rc.d/ntpdate will time-out if it
118# executes before /etc/rc.d/wpa_supplicant connects to the network
119# (See rcorder /etc/rc.d/*)
120
121# EOF
wpa_cli.j2
Synopsis: Template wpa_cli.
Description of the template.
1#!/bin/sh
2# {{ ansible_managed }}"
3
4# PROVIDE: wpa_cli
5# REQUIRE: mountcritremote
6# KEYWORD: nojail nostart
7
8. /etc/rc.subr
9. /etc/network.subr
10
11name="wpa_cli"
12desc="Frontend to WPA/802.11i Supplicant for wireless network
13devices. Run in daemon mode executing the action file based on events
14from wpa_supplicant"
15rcvar=
16
17ifn="$2"
18if [ -z "$ifn" ]; then
19 return 1
20fi
21
22load_rc_config $name
23
24command="${wpa_cli_program}"
25pidfile="/var/run/${name}/${ifn}.pid"
26command_args="-B -i $ifn -P $pidfile -p ${wpa_cli_ctrl_interface} -a ${wpa_cli_action_file}"
27required_files="${wpa_cli_action_file}"
28
29run_rc_command "$1"
30
31# EOF