diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/net-profiles | 76 | ||||
-rwxr-xr-x | scripts/net-rename | 26 | ||||
-rwxr-xr-x | scripts/netcfg | 101 | ||||
-rwxr-xr-x | scripts/netcfg-menu | 69 |
4 files changed, 272 insertions, 0 deletions
diff --git a/scripts/net-profiles b/scripts/net-profiles new file mode 100755 index 0000000..cbaea30 --- /dev/null +++ b/scripts/net-profiles @@ -0,0 +1,76 @@ +#!/bin/bash + +. /usr/lib/network/globals + +case "$1" in + start) + if ! ck_daemon net-profiles; then + exit_stderr "net-profiles has already been started. Try '/etc/rc.d/net-profiles restart'" + fi + + # Ensure any device renaming has occurred as intended + for daemon in "${DAEMONS[@]}"; do + if [[ "$daemon" = "${daemon#!}" && "$daemon" = "net-rename" ]]; then + if ck_daemon net-rename; then + /etc/rc.d/net-rename start + fi + fi + done + + # $NET env var is passed from the kernel boot line + [[ -z "$NETWORKS_MENU_TIMEOUT" ]] && NETWORKS_MENU_TIMEOUT=5 + + if [[ "$NET" = "menu" ]]; then + if /usr/bin/netcfg-menu "$NETWORKS_MENU_TIMEOUT"; then + mv "$STATE_DIR"/{menu,net-profiles} # JP: user may want to disconnect profile by calling net-profiles stop + add_daemon net-profiles + exit 0 + fi + elif [[ -n "$NET" ]]; then + if /usr/bin/netcfg2 check-iface "$NET"; then + echo "$NET" > "$STATE_DIR/net-profiles" # JP: user may want to disconnect profile by calling net-profiles stop + add_daemon net-profiles + exit 0 + fi + elif [[ $NETWORKS = "menu" ]]; then + if /usr/bin/netcfg-menu "$NETWORKS_MENU_TIMEOUT"; then + mv "$STATE_DIR"/menu "$STATE_DIR"/net-profiles + add_daemon net-profiles + exit 0 + fi + else + # No NET= passed at boot, go to NETWORKS=() + for network in "${NETWORKS[@]}"; do + if [[ "$network" = "${network#!}" ]]; then + if /usr/bin/netcfg2 check-iface "$network"; then + echo "$network" >> "$STATE_DIR/net-profiles" + add_daemon net-profiles + fi + fi + done + fi + if [[ ! -f "$STATE_DIR"/net-profiles ]]; then + exit_err "No profile started." # JP: don't add_daemon unless we were successful (above) + fi + ;; + stop) + if ck_daemon net-profiles; then + exit_stderr "net-profiles not running" + fi + + # shutdown any profiles started by netcfg (or from NET_PROFILES in rc.conf) + # JP: only attempt to disconnect the profiles _this daemon_ was told to control + for profile in $(cat "$STATE_DIR/net-profiles"); do + /usr/bin/netcfg2 down "$profile" + done + rm -f "$STATE_DIR/net-profiles" + rm_daemon net-profiles + ;; + restart) + "$0" stop; sleep 1; "$0" start + ;; + *) + exit_stderr "Usage: $0 {start|stop|restart}" +esac + +# vim: ft=sh ts=4 et sw=4: diff --git a/scripts/net-rename b/scripts/net-rename new file mode 100755 index 0000000..1a51ffe --- /dev/null +++ b/scripts/net-rename @@ -0,0 +1,26 @@ +#!/bin/bash + +. /usr/lib/network/globals + +case "$1" in + start) + report_try "Renaming network devices" + ifrename -p -t + report_success + + add_daemon net-rename + ;; + stop) + rm_daemon net-rename + # No stop neccesary, but add one to look nice on shutdown. + /bin/true + ;; + restart) + "$0" start + ;; + *) + exit_stderr "Usage: $0 {start|stop|restart}" + ;; +esac + +# vim: ft=sh ts=4 et sw=4: diff --git a/scripts/netcfg b/scripts/netcfg new file mode 100755 index 0000000..e00794d --- /dev/null +++ b/scripts/netcfg @@ -0,0 +1,101 @@ +#!/bin/bash + +. /usr/lib/network/network + +NETCFG_VER=2.6 + +version() +{ + echo "netcfg v$NETCFG_VER" +} + +usage() +{ + version + echo "Usage:" + echo " Start specified profile: netcfg profile " + echo " Other functions: netcfg argument profile" + echo "Arguments:" + echo " current Report currently running profiles" + echo "-a, all-down Take all active profiles down" + echo "-c, check-iface Do not start profile if interface is already up" + echo "-d, down Take specified profile down" + echo "-h, help This help message" + echo "-i, iface-down Take down profile active on specified interface" + echo "-l, list List all available profiles" + echo "-r, reconnect Disconnect and reconnect specified profile" + echo "-u, up Start specified profile" + echo "-v, version Output version information and exit" + echo " all-resume Resume previously suspended profiles and reconnect them" + echo " all-suspend Store a list of current running profiles and suspend them" +} + +# TODO: Re-add ROOT check and rewrite with getopts from BashFAQ + +case "$1" in + --version|-v|version) + version + exit 0;; + --help|-h|help) + usage + exit 0;; + list|-l) + list_profiles + exit 0;; + current|-s|status) + if [[ -d "$STATE_DIR/profiles/" ]]; then + ls "$STATE_DIR/profiles/" + exit 0 + else + exit_stderr "No active profiles." + fi;; +esac + +if [[ $(id -u) -gt 0 ]]; then + exit_stderr "This script should be run as root." +fi + +# Ensure cwd is not in a transient directory, which may prevent unmounting due to netcfg children +cd / + +case "$1" in + + -c|check-iface|-u|up) + CHECK="YES" + profile_up "$2";; + clean) + rm "$STATE_DIR/interfaces"/* 2> /dev/null + rm "$STATE_DIR/profiles"/* 2> /dev/null + rm "$STATE_DIR/suspend"/* 2> /dev/null + rm "$STATE_DIR/last_profile" 2> /dev/null + killall wpa_supplicant 2> /dev/null + killall dhcpcd 2> /dev/null + ;; + -d|down) + profile_down "$2";; + -i|iface-down) + interface_down "$2";; + -a|all-down) + all_down;; + -r|reconnect) + profile_down "$2" + profile_up "$2";; + all-resume) + all_resume;; + all-suspend) + all_suspend;; + -*|--*) + usage + exit 1;; + *) + if [[ -n "$1" ]]; then + profile_up "$1" + else + usage + exit 1 + fi + ;; +esac +exit $? + +# vim: ft=sh ts=4 et sw=4: diff --git a/scripts/netcfg-menu b/scripts/netcfg-menu new file mode 100755 index 0000000..ff57005 --- /dev/null +++ b/scripts/netcfg-menu @@ -0,0 +1,69 @@ +#! /bin/bash + +. /usr/lib/network/network + +if [[ ! -x /usr/bin/dialog ]]; then + echo "Please install 'dialog' to use netcfg-menu" + exit 1 +fi + +check_make_state_dir + +# JP: we'll use $STATE_DIR/menu to record what profile is being connected in this way +rm -f "$STATE_DIR/menu" + +# Scan all profiles +i=0 +# JP: change for prof to while read prof to avoid assumption that profile names are always single tokens (no spaces etc.) +while read prof; do + # if there is a profile called "main", Use as default + [[ "$prof" = "main" ]] && DEFAULT="main" + profiles[$i]="$prof" + let i++ + profiles[$i]=$(. "$PROFILE_DIR/$prof"; echo "$DESCRIPTION") + let i++ +done < <(list_profiles | sort) # JP: re-use list_profiles instead of duplicating it; avoid subshell we'd get by piping it to the while read... + +if [[ ${#profiles} -eq 0 ]]; then + exit_err "No profiles were found in $PROFILE_DIR" +fi + +[[ -n "$NETWORKS_MENU_DEFAULT" ]] && DEFAULT="$NETWORKS_MENU_DEFAULT" +# if no default yet, use the first entry +[[ -z "$DEFAULT" ]] && DEFAULT="${profiles[0]}" +ANSWER=$(mktemp --tmpdir menu.XXXXXXXX) || exit 1 + +# Set timeout +if [[ -z "$1" ]]; then + TIMEOUT="0" +else + TIMEOUT="$1" +fi + +# Display Dialog +dialog --timeout "$TIMEOUT" --default-item "$DEFAULT" \ + --menu "Select the network profile you wish to use" \ + 13 50 6 "${profiles[@]}" 2> "$ANSWER" + +ret=$? + +case $ret in + 1) ;; # Cancel - do nothing + 255) # timeout - use default + profile_up "$DEFAULT" # JP: use profile_up and catch $? + ret=$? + if [[ $ret -eq 0 ]]; then echo "$DEFAULT" > "$STATE_DIR/menu"; fi + ;; + 0) # User selection + profile_up "$(cat "$ANSWER")" + ret=$? + if [[ $ret -eq 0 ]]; then mv "$ANSWER" "$STATE_DIR/menu"; fi + ;; + *) # Shouldnt happen + exit_err "Abnormal ret code from dialog: $ret" + ;; +esac +rm -f "$ANSWER" # JP: add -f +exit $ret # JP: exit with caught $? + +# vim: ft=sh ts=4 et sw=4: |