|
1 |
| -#!/usr/bin/env bash |
2 |
| - |
3 |
| -## About |
4 |
| -# Effectively this script acts as a toggle between a dhcp network and a static |
5 |
| -# configured router. |
6 |
| -# This script connects a linux network_interface to a FIRST configured access point |
7 |
| -# either via wireless or ethernet from dhcp or from a static IP to dhcp |
8 |
| -# depending on the current status of the computer's connection |
9 |
| -# |
10 |
| -# For example, if I'm connected to a static FIRST network at 10.29.45.5 |
11 |
| -# over ethernet and I run this script(assuming my $default_network_interface or options |
12 |
| -# select the ethernet network interface, I will be switched to dhcp. If I run this script |
13 |
| -# again with the same options, I will be switched from dhcp to the pre-set static ip. |
14 |
| -# Options: |
15 |
| -# --wireless |
16 |
| -# Use the wireless network interface specified as $wireless_network_interface in the script |
17 |
| -# --wired |
18 |
| -# Use the wired network interface specified as $wireless_network interface in the script |
19 |
| -# |
20 |
| -# Requirements: |
21 |
| -# - bash |
22 |
| -# - network-tools |
23 |
| -# - wicd |
24 |
| - |
25 |
| -## User set variables -- Using text files |
26 |
| -# Unique identifier for the computer you're running the script on |
27 |
| -ip_identifier="4" # 4 |
28 |
| -netmask="255.255.255.0" # 255.255.255.0 |
29 |
| -team_number="2945" # 2945 |
30 |
| -wired_network_interface="eth0" # eth0 |
31 |
| -wireless_network_interface="wlan0" #wlan0 |
32 |
| -access_point="2945" # only needed for wireless. Assumes network has no |
33 |
| -# encryption |
34 |
| -# wireless or wired default |
35 |
| -default_network_interface="$wired_network_interface" |
36 |
| - |
37 |
| -## Functions |
38 |
| - |
39 |
| -# Figure out what the connection ip will look like based on the team number |
40 |
| -# and the unique identifier |
41 |
| -function return_final_ip { |
42 |
| - # IP format: |
43 |
| - # 29 = 10.0.29.$(ip_identifier) |
44 |
| - # 294 = 10.2.94.$(ip_identifier) |
45 |
| - # 2945 = 10.29.45.$(ip_identifier) |
46 |
| - # 107 = 10.1.7.$(ip_identifier) |
47 |
| - # 170 = 10.1.70.$(ip_identifier) |
48 |
| - # 1700 = 10.17.0.$(ip_identifier) |
49 |
| - team_number="$1" |
50 |
| - ip_identifier="$2" |
51 |
| - |
52 |
| - team_number_length=${#team_number} |
53 |
| - sub_ip="" # The team configured ip substring |
54 |
| - |
55 |
| - if [[ $team_number_length == 1 || $team_number_length == 2 ]]; then |
56 |
| - sub_ip="0.${team_number}" |
57 |
| - elif [[ $team_number_length == 3 ]]; then |
58 |
| - if [[ ${team_number:1:1} == "0" ]]; then # ex: 107 |
59 |
| - sub_ip="${team_number:0:1}.${team_number:2:1}" # ex 107 -> 1.7 |
60 |
| - else |
61 |
| - sub_ip="${team_number:0:1}.${team_number:1:2}" # ex 294 -> 2.94 |
62 |
| - fi |
63 |
| - |
64 |
| - elif [[ $team_number_length == 4 ]]; then |
65 |
| - sub_ip="10.29.${team_number}.${ip_identifier}" |
66 |
| - if [[ ${team_number:2:1} == 1 ]]; then # ex 1700 -> 17.0 or 1707 -> 17.7 |
67 |
| - sub_ip="${team_number:0:2}.${team_number:3:1}" |
68 |
| - else |
69 |
| - sub_ip="${team_number:0:2}.${team_number:2:2}" # ex 2945 -> 29.45 |
70 |
| - fi |
71 |
| - else |
72 |
| - echo "Incorrect team name" |
73 |
| - exit 1 |
74 |
| - fi |
75 |
| - final_ip="10.${sub_ip}.${ip_identifier}" |
76 |
| - echo "$final_ip" |
77 |
| -} |
78 |
| - |
79 |
| - |
80 |
| -# accept arguments |
81 |
| -# function wireless_to_static { # I cannot get wicd to set IP, so this doesn't work |
82 |
| -# final_ip="$1" |
83 |
| -# access_point="$2" |
84 |
| -# |
85 |
| -# wicd_cli=`wicd-cli -l --wireless` |
86 |
| -# # returns the access point name |
87 |
| -# available_essid=$(echo "$wicd_cli" | grep -i "$access_point" | awk '{ print $4 }') |
88 |
| -# |
89 |
| -# if [[ "$access_point" == "$available_essid" ]]; then |
90 |
| -# # Find number and connect or disconnect |
91 |
| -# number=$( echo "$wicd_cli" | grep "$access_point" | awk '{ print $1 }') |
92 |
| -# wicd-cli |
93 |
| -# else |
94 |
| -# echo "Cannot find access point to connect to" >&2 |
95 |
| -# exit 1 |
96 |
| -# fi |
97 |
| -# } |
98 |
| - |
99 |
| -function wireless_to_dhcp { |
100 |
| - # Let wicd automatically connect to your dhcp network |
101 |
| - # wicd-cli -x --wireless |
102 |
| - # wicd-cli -c --wireless |
103 |
| - echo "foo" |
104 |
| -} |
105 |
| - |
106 |
| -function wired_to_static { |
107 |
| - sudo ifconfig $selected_network_interface $final_ip netmask $netmask |
108 |
| -} |
109 |
| - |
110 |
| -function wired_to_dhcp { |
111 |
| - # wicd-cli -x --wired |
112 |
| - # wicd is a piece of shit |
113 |
| - echo "foo" |
114 |
| -} |
115 |
| - |
116 |
| -final_ip=`return_final_ip $team_number $ip_identifier` |
117 |
| -echo "Connecting as $final_ip on $selected_network_interface" |
118 |
| - |
119 |
| -# options |
120 |
| -connect_wireless=false |
121 |
| -connect_wired=false |
122 |
| - |
123 |
| -if [[ "$1" == "--wireless" ]]; then |
124 |
| - selected_network_interface="$wireless_network_interface" |
125 |
| - connect_wireless=true |
126 |
| -elif [[ "$1" == "--wired" ]]; then |
127 |
| - selected_network_interface="$wired_network_interface" |
128 |
| - connect_wired=true |
129 |
| -else |
130 |
| - # Grep returns 1 if the desired search is not matched |
131 |
| - # I exploit this to determine wether the output of iwconfig shows the |
132 |
| - # selected network interface to be wireless or wired |
133 |
| - |
134 |
| - selected_network_interface="$default_network_interface" |
135 |
| - network_interface_type=`iwconfig | grep "$default_network_interface"` |
136 |
| - network_interface_is_wired=$? |
137 |
| - if (( $network_interface_is_wired )); then |
138 |
| - connect_wired=true |
139 |
| - else |
140 |
| - connect_wireless=true |
141 |
| - fi |
142 |
| -fi |
143 |
| - |
144 |
| -ifconfig | grep -A2 "$selected_network_interface" | grep "$final_ip" |
145 |
| -is_current_network_dhcp="$?" # Doesn't totally determine if the current network |
146 |
| -# is using dhcp, but it's good enough for my purposes |
147 |
| - |
148 |
| -if $connect_wired; then |
149 |
| - if (( $is_current_network_dhcp )); then |
150 |
| - wired_to_static |
151 |
| - else |
152 |
| - wired_to_dhcp |
153 |
| - fi |
154 |
| -elif $connect_wireless; then |
155 |
| - if (( $is_current_network_dhcp )); then |
156 |
| - # wireless_to_static $final_ip $netmask $access_point |
157 |
| - echo "nothing to do here" |
158 |
| - else |
159 |
| - wiress_to_dhcp |
160 |
| - fi |
161 |
| -else |
162 |
| - echo "Something went wrong with determining with what nick to connect" |
163 |
| - exit 1 |
164 |
| -fi |
165 |
| -# main |
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +## About |
| 4 | +# Effectively this script acts as a toggle between a dhcp network and a static |
| 5 | +# configured router. |
| 6 | +# This script connects a linux network_interface to a FIRST configured access point |
| 7 | +# either via wireless or ethernet from dhcp or from a static IP to dhcp |
| 8 | +# depending on the current status of the computer's connection |
| 9 | +# |
| 10 | +# For example, if I'm connected to a static FIRST network at 10.29.45.5 |
| 11 | +# over ethernet and I run this script(assuming my $default_network_interface or options |
| 12 | +# select the ethernet network interface, I will be switched to dhcp. If I run this script |
| 13 | +# again with the same options, I will be switched from dhcp to the pre-set static ip. |
| 14 | +# Options: |
| 15 | +# --wireless |
| 16 | +# Use the wireless network interface specified as $wireless_network_interface in the script |
| 17 | +# --wired |
| 18 | +# Use the wired network interface specified as $wireless_network interface in the script |
| 19 | +# |
| 20 | +# Requirements: |
| 21 | +# - bash |
| 22 | +# - network-tools |
| 23 | +# - wicd |
| 24 | + |
| 25 | +## User set variables -- Using text files |
| 26 | +# Unique identifier for the computer you're running the script on |
| 27 | +ip_identifier="4" # 4 |
| 28 | +netmask="255.255.255.0" # 255.255.255.0 |
| 29 | +team_number="2945" # 2945 |
| 30 | +wired_network_interface="eth0" # eth0 |
| 31 | +wireless_network_interface="wlan0" #wlan0 |
| 32 | +access_point="2945" # only needed for wireless. Assumes network has no |
| 33 | +# encryption |
| 34 | +# wireless or wired default |
| 35 | +default_network_interface="$wired_network_interface" |
| 36 | + |
| 37 | +## Functions |
| 38 | + |
| 39 | +# Figure out what the connection ip will look like based on the team number |
| 40 | +# and the unique identifier |
| 41 | +function return_final_ip { |
| 42 | + # IP format: |
| 43 | + # 29 = 10.0.29.$(ip_identifier) |
| 44 | + # 294 = 10.2.94.$(ip_identifier) |
| 45 | + # 2945 = 10.29.45.$(ip_identifier) |
| 46 | + # 107 = 10.1.7.$(ip_identifier) |
| 47 | + # 170 = 10.1.70.$(ip_identifier) |
| 48 | + # 1700 = 10.17.0.$(ip_identifier) |
| 49 | + team_number="$1" |
| 50 | + ip_identifier="$2" |
| 51 | + |
| 52 | + team_number_length=${#team_number} |
| 53 | + sub_ip="" # The team configured ip substring |
| 54 | + |
| 55 | + if [[ $team_number_length == 1 || $team_number_length == 2 ]]; then |
| 56 | + sub_ip="0.${team_number}" |
| 57 | + elif [[ $team_number_length == 3 ]]; then |
| 58 | + if [[ ${team_number:1:1} == "0" ]]; then # ex: 107 |
| 59 | + sub_ip="${team_number:0:1}.${team_number:2:1}" # ex 107 -> 1.7 |
| 60 | + else |
| 61 | + sub_ip="${team_number:0:1}.${team_number:1:2}" # ex 294 -> 2.94 |
| 62 | + fi |
| 63 | + |
| 64 | + elif [[ $team_number_length == 4 ]]; then |
| 65 | + sub_ip="10.29.${team_number}.${ip_identifier}" |
| 66 | + if [[ ${team_number:2:1} == 1 ]]; then # ex 1700 -> 17.0 or 1707 -> 17.7 |
| 67 | + sub_ip="${team_number:0:2}.${team_number:3:1}" |
| 68 | + else |
| 69 | + sub_ip="${team_number:0:2}.${team_number:2:2}" # ex 2945 -> 29.45 |
| 70 | + fi |
| 71 | + else |
| 72 | + echo "Incorrect team name" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + final_ip="10.${sub_ip}.${ip_identifier}" |
| 76 | + echo "$final_ip" |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +# accept arguments |
| 81 | +# function wireless_to_static { # I cannot get wicd to set IP, so this doesn't work |
| 82 | +# final_ip="$1" |
| 83 | +# access_point="$2" |
| 84 | +# |
| 85 | +# wicd_cli=`wicd-cli -l --wireless` |
| 86 | +# # returns the access point name |
| 87 | +# available_essid=$(echo "$wicd_cli" | grep -i "$access_point" | awk '{ print $4 }') |
| 88 | +# |
| 89 | +# if [[ "$access_point" == "$available_essid" ]]; then |
| 90 | +# # Find number and connect or disconnect |
| 91 | +# number=$( echo "$wicd_cli" | grep "$access_point" | awk '{ print $1 }') |
| 92 | +# wicd-cli |
| 93 | +# else |
| 94 | +# echo "Cannot find access point to connect to" >&2 |
| 95 | +# exit 1 |
| 96 | +# fi |
| 97 | +# } |
| 98 | + |
| 99 | +function wireless_to_dhcp { |
| 100 | + # Let wicd automatically connect to your dhcp network |
| 101 | + # wicd-cli -x --wireless |
| 102 | + # wicd-cli -c --wireless |
| 103 | + echo "foo" |
| 104 | +} |
| 105 | + |
| 106 | +function wired_to_static { |
| 107 | + sudo ifconfig $selected_network_interface $final_ip netmask $netmask |
| 108 | +} |
| 109 | + |
| 110 | +function wired_to_dhcp { |
| 111 | + # wicd-cli -x --wired |
| 112 | + # wicd is a piece of shit |
| 113 | + echo "foo" |
| 114 | +} |
| 115 | + |
| 116 | +final_ip=`return_final_ip $team_number $ip_identifier` |
| 117 | +echo "Connecting as $final_ip on $selected_network_interface" |
| 118 | + |
| 119 | +# options |
| 120 | +connect_wireless=false |
| 121 | +connect_wired=false |
| 122 | + |
| 123 | +if [[ "$1" == "--wireless" ]]; then |
| 124 | + selected_network_interface="$wireless_network_interface" |
| 125 | + connect_wireless=true |
| 126 | +elif [[ "$1" == "--wired" ]]; then |
| 127 | + selected_network_interface="$wired_network_interface" |
| 128 | + connect_wired=true |
| 129 | +else |
| 130 | + # Grep returns 1 if the desired search is not matched |
| 131 | + # I exploit this to determine wether the output of iwconfig shows the |
| 132 | + # selected network interface to be wireless or wired |
| 133 | + |
| 134 | + selected_network_interface="$default_network_interface" |
| 135 | + network_interface_type=`iwconfig | grep "$default_network_interface"` |
| 136 | + network_interface_is_wired=$? |
| 137 | + if (( $network_interface_is_wired )); then |
| 138 | + connect_wired=true |
| 139 | + else |
| 140 | + connect_wireless=true |
| 141 | + fi |
| 142 | +fi |
| 143 | + |
| 144 | +ifconfig | grep -A2 "$selected_network_interface" | grep "$final_ip" |
| 145 | +is_current_network_dhcp="$?" # Doesn't totally determine if the current network |
| 146 | +# is using dhcp, but it's good enough for my purposes |
| 147 | + |
| 148 | +if $connect_wired; then |
| 149 | + if (( $is_current_network_dhcp )); then |
| 150 | + wired_to_static |
| 151 | + else |
| 152 | + wired_to_dhcp |
| 153 | + fi |
| 154 | +elif $connect_wireless; then |
| 155 | + if (( $is_current_network_dhcp )); then |
| 156 | + # wireless_to_static $final_ip $netmask $access_point |
| 157 | + echo "nothing to do here" |
| 158 | + else |
| 159 | + wiress_to_dhcp |
| 160 | + fi |
| 161 | +else |
| 162 | + echo "Something went wrong with determining with what nick to connect" |
| 163 | + exit 1 |
| 164 | +fi |
| 165 | +# main |
0 commit comments