diff options
author | Andrew Cady <d@jerkface.net> | 2018-06-12 16:56:06 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2018-06-12 17:07:12 -0400 |
commit | eb3f50f27412c06f5efd2dc4788ed38b4809dda4 (patch) | |
tree | c3aa54c7f2422629912ef0f44d110af0ba4ae282 | |
parent | a0d05150e5279ddb39fd981553f244706588f922 (diff) |
wifi restarting scripts
-rwxr-xr-x | dot/local/bin/hard-restart-wifi | 23 | ||||
-rwxr-xr-x | dot/local/bin/restart-linux-device | 53 |
2 files changed, 76 insertions, 0 deletions
diff --git a/dot/local/bin/hard-restart-wifi b/dot/local/bin/hard-restart-wifi new file mode 100755 index 0000000..78f7144 --- /dev/null +++ b/dot/local/bin/hard-restart-wifi | |||
@@ -0,0 +1,23 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | default_dev='wlan0=funston' | ||
4 | |||
5 | die() { printf 'Error: %s\n' "$*" >&2; exit 1; } | ||
6 | |||
7 | with_pwd_path() | ||
8 | { | ||
9 | local PATH="$PATH":. | ||
10 | "$@" | ||
11 | } | ||
12 | |||
13 | [ $(id -u) = 0 ] || die "you are not root." | ||
14 | |||
15 | restart=$(with_pwd_path which restart-linux-device) || die "command 'restart-linux-device' is not available." | ||
16 | |||
17 | dev=${1:-$default_dev} | ||
18 | rawdev=${dev%=*} | ||
19 | |||
20 | [ "$VERBOSE" ] && set -x | ||
21 | $restart $rawdev | ||
22 | ifdown --force $rawdev # It's not clear to me why this is necessary, but it is. | ||
23 | ifup $dev | ||
diff --git a/dot/local/bin/restart-linux-device b/dot/local/bin/restart-linux-device new file mode 100755 index 0000000..e1cd41f --- /dev/null +++ b/dot/local/bin/restart-linux-device | |||
@@ -0,0 +1,53 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | # Thanks, ericwoud | ||
4 | # https://ubuntuforums.org/showthread.php?t=2339439&page=2&p=13585921#post13585921 | ||
5 | |||
6 | warn() { printf 'Error: %s\n' "$*" >&2; } | ||
7 | |||
8 | with_rescan_remove() | ||
9 | { | ||
10 | local devname="$1" dev f hw_path rescan remove | ||
11 | shift | ||
12 | |||
13 | case "$devname" in | ||
14 | */*) | ||
15 | dev=/sys/class/$devname ;; | ||
16 | *) | ||
17 | for f in /sys/class/*/"$devname"; do | ||
18 | dev=$f | ||
19 | break | ||
20 | done | ||
21 | ;; | ||
22 | esac | ||
23 | [ "$dev" -a -d "$dev" ] || { warn "device not found: $devname"; return 1; } | ||
24 | |||
25 | hw_path=$(dirname $(dirname $(realpath "$(dirname "$dev")/$(readlink "$dev")"))) | ||
26 | |||
27 | rescan=$(dirname "$hw_path")/rescan | ||
28 | remove="$hw_path"/remove | ||
29 | |||
30 | if [ ! -e "$rescan" -a -e "$hw_path"/../pci_bus ]; then | ||
31 | local p=$(dirname "$hw_path")/ | ||
32 | rescan=$p/pci_bus/${p#*/pci}/rescan | ||
33 | fi | ||
34 | |||
35 | [ -e "$rescan" ] || { warn "sysfs special file not found: $rescan"; return 1; } | ||
36 | [ -e "$remove" ] || { warn "sysfs special file not found: $remove"; return 1; } | ||
37 | |||
38 | "$@" "$rescan" "$remove" | ||
39 | } | ||
40 | |||
41 | restart() | ||
42 | { | ||
43 | local rescan="$1" remove="$2" | ||
44 | printf 1 > "$remove" || { warn "kernel remove failed"; return 2; } | ||
45 | printf 1 > "$rescan" || { warn "kernel rescan failed"; return 3; } | ||
46 | |||
47 | for ((i=0; i<100; ++i)); do | ||
48 | [ -e "$remove" ] && break | ||
49 | sleep 0.05 | ||
50 | done | ||
51 | } | ||
52 | |||
53 | with_rescan_remove "$1" restart | ||