rc.network 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/bin/sh
  2. #(c) Copyright Barry Kauler 2010, bkhome.org
  3. #2010 Lesser GPL licence v2 (file:///usr/share/doc/legal/lgpl-2.1.txt)
  4. #/etc/simple_network_setup/connections is created by SNS, /usr/local/simple_network_setup/sns
  5. #100308 first version of script, open and wep networks only.
  6. #100309 supports wpa. "version 1" released.
  7. #100314 support ndiswrapper.
  8. #100320 fix if essid has spaces. Add DHCPCDFIX parameter.
  9. #100325 added 'stop' commandline option. added DHCPCDFIX for wired.
  10. #100513 more delay, repeat scan attempt.
  11. #100703 fix disconnect/reconnect.
  12. #100804 while testing for module loaded, also test for valid macaddress.
  13. #101122 wait for module to load.
  14. #101216 more logging, to /tmp/simple_network_setup.
  15. #110203 improve scanning.
  16. #120204 BK: internationalized.
  17. #121117 rerwin: Add dropwait option to dhcpcd startup.
  18. #130411 skip dhcpcd if in frisbee mode
  19. export TEXTDOMAIN=sns___rc.network
  20. export OUTPUT_CHARSET=UTF-8
  21. LANGORIG=$LANG
  22. #each line of /etc/simple_network_setup/connections has everything known about a connection:
  23. #(please ignore spaces, put here for readability only)
  24. #Wireless:
  25. # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  26. #format: INTERFACE|IF_INTTYPE|IF_DRIVER|IF_BUS |IF_INFO |MACADDRESS |CELL_NUMBER|CELL_ADDRESS |CELL_CHANNEL|CELL_QUALITY|CELL_ENCRYPTIONKEY|CELL_ESSID|SEC_KEY |SEC_MGMT|ENCODEPROTO|WPA_DRIVER|DHCPCDFIX|
  27. #ex: wlan1 |Wireless |rt73usb |usb |Ralink RT73 USB Wireless LAN driver|00:26:19:F5:AC:3D|01 |00:17:3F:68:33:7E|11 |70/70 |off |belkin54g |000102030405060708090a0b0c|WEP | |wext | |
  28. #Wired:
  29. # 1 2 3 4 5 6 7
  30. #format: INTERFACE|IF_INTTYPE|IF_DRIVER|IF_BUS |IF_INFO |MACADDRESS |DHCPCDFIX|
  31. #ex: eth0 |Wired |sky2 |pci |Marvell Yukon 2 Gigabit Ethernet driver|00:17:36:84:E5:1A| |
  32. #IMPORTANT: the INTERFACE field may be different at boot time, due to different plugged in network devices.
  33. export LANG='C'
  34. [ ! -s /etc/simple_network_setup/connections ] && exit
  35. rm -f /tmp/sns_rc.network_exit 2>/dev/null #100703
  36. if [ $1 ];then #100325
  37. case $1 in
  38. stop)
  39. if [ -f /tmp/sns_interface_success ];then
  40. INTERFACE="`cat /tmp/sns_interface_success`"
  41. rm -f /tmp/sns_interface_success
  42. [ "`pidof wpa_supplicant`" != "" ] && wpa_cli terminate #kill wpa_supplicant.
  43. ifconfig $INTERFACE down
  44. [ "`iwconfig $INTERFACE | grep "$INTERFACE" | grep "ESSID"`" != "" ] && iwconfig $INTERFACE essid off
  45. dhcpcd --release $INTERFACE 2>/dev/null
  46. ip route flush dev $INTERFACE #100703
  47. #in situation bring down interface from desktop icon...
  48. [ "$DISPLAY" ] && LANG=$LANGORIG yaf-splash -placement bottom -bg pink -timeout 5 -text "$(gettext 'Network interface') ${INTERFACE} $(gettext 'has been disabled')" &
  49. fi
  50. exit
  51. ;;
  52. esac
  53. fi
  54. rm -f /tmp/sns_interface_success 2>/dev/null
  55. [ "`grep '|ndiswrapper|' /etc/simple_network_setup/connections`" != "" ] && modprobe ndiswrapper #100314
  56. #wait for interfaces to become available...
  57. #note, do not test for wlan0 or wlan1 (etc) to become available, as these can change depending on plugged-in devices.
  58. WAITCNT=0 ; TESTLIST='|'
  59. sleep 1
  60. echo -n "" > /tmp/sns_connections_available
  61. for ONEMODULE in `cut -f 3 -d '|' /etc/simple_network_setup/connections | sort -u | tr '\n' ' '` #100804
  62. do
  63. TESTLIST="${TESTLIST}${ONEMODULE}|" #101122
  64. oPATTERN='^'"$ONEMODULE"'$'
  65. mPATTERN='|'"$ONEMODULE"'|'
  66. while [ 1 ];do #101122 wait for module to load.
  67. [ "`lsmod | grep -v '^Module' | cut -f 1 -d ' ' | grep "$oPATTERN"`" != "" ] && break
  68. [ "`echo "$TESTLIST" | grep "$mPATTERN"`" != "" ] && continue 2 #avoid re-waiting on same module
  69. sleep 1
  70. WAITCNT=$(($WAITCNT + 1))
  71. [ $WAITCNT -gt 8 ] && continue 2 #no module loaded.
  72. done
  73. MACADDRESSES="`grep "$mPATTERN" /etc/simple_network_setup/connections | cut -f 6 -d '|' | sort -u | tr '\n' ' '`" #100804
  74. for MACADDRESS in $MACADDRESSES #100804
  75. do
  76. while [ 1 ];do
  77. ALL_MACADDRESS="`ifconfig -a | grep 'Link encap:Ethernet' | grep -o 'HWaddr .*' | cut -f 2 -d ' '`"
  78. if [ "`echo "$ALL_MACADDRESS" | grep "$MACADDRESS"`" != "" ];then
  79. #interface is ready. copy all connections with that module and macaddress...
  80. macPATTERN='|'"$MACADDRESS"'|'
  81. MACMATCH="`grep "$mPATTERN" /etc/simple_network_setup/connections | grep "$macPATTERN"`"
  82. [ "$MACMATCH" ] && echo "$MACMATCH" >> /tmp/sns_connections_available
  83. break
  84. fi
  85. WAITCNT=`expr $WAITCNT + 1`
  86. [ $WAITCNT -gt 12 ] && break 2 #timeout.
  87. sleep 1
  88. done
  89. done
  90. done
  91. [ ! -s /tmp/sns_connections_available ] && exit
  92. INTERFACES="`ifconfig -a | grep -F 'Link encap:Ethernet' | cut -f1 -d' ' | tr '\n' ' '`"
  93. ##########WIRELESS##########
  94. #121117 form dropwait argument for dhcpcd...
  95. DROPWAIT="$(dhcpcd_dropwait_option)"
  96. echo -n "" > /tmp/simple_network_setup/rc_network_wireless_connection_log
  97. grep '|Wireless|' /tmp/sns_connections_available > /tmp/sns_connections_wireless
  98. ESSIDSwant="$(grep '|Wireless|' /etc/simple_network_setup/connections | cut -f 12 -d '|')"
  99. echo -n "$ESSIDSwant" > /tmp/simple_network_setup/essids-want
  100. for INTERFACE in $INTERFACES #exs: wlan0 eth0
  101. do
  102. [ ! -d /sys/class/net/${INTERFACE}/wireless ] && continue #only want wireless.
  103. echo -e "\n ${INTERFACE}" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  104. ifconfig $INTERFACE up
  105. [ $? -ne 0 ] && continue
  106. echo " SUCCESS: ifconfig ${INTERFACE} up" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  107. sleep 3
  108. SECS=3
  109. SCANRESULT="`iwlist $INTERFACE scan`" ###SCANNING### 110203
  110. echo " EXECUTING SCAN: iwlist ${INTERFACE} scan | grep -v 'Scan completed'" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  111. echo " SCANRESULT=${SCANRESULT}" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  112. #note, possible to get: 'wlan0 No scan results' so delay then try again...
  113. if [ "$ESSIDSwant" ];then
  114. SFLAG="$(echo "$SCANRESULT" | grep 'ESSID' | grep -f /tmp/simple_network_setup/essids-want)"
  115. else
  116. SFLAG="$(echo "$SCANRESULT" | grep 'Scan completed')"
  117. fi
  118. if [ "$SFLAG" == "" ];then
  119. sleep 2
  120. SECS=5
  121. SCANRESULT="`iwlist $INTERFACE scan`" ###SCANNING###
  122. echo " EXECUTING SCAN AGAIN: iwlist ${INTERFACE} scan | grep -v 'Scan completed'" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  123. echo " SCANRESULT=${SCANRESULT}" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  124. fi
  125. ifconfig $INTERFACE down
  126. echo " EXECUTING: ifconfig $INTERFACE down" >> /tmp/simple_network_setup/rc_network_wireless_connection_log #110203
  127. if [ "`echo "$SCANRESULT" | grep 'Scan completed'`" = "" ];then #110203
  128. echo 'SCAN FAILURE, EXPECTED STRING "Scan completed"' >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  129. continue #110203
  130. fi
  131. #convert each found network into a single line... 110203
  132. SRLINES="`echo "$SCANRESULT" | grep -v 'Scan completed' | tr '|' ' ' | tr '\n' '|' | sed -e 's% Cell %\n%g' | tr -s ' '`"
  133. echo "$SRLINES" |
  134. while read ONELINE
  135. do
  136. [ "$ONELINE" = "" ] && continue
  137. [ "$ONELINE" = " " ] && continue
  138. CELL_ESSID="`echo -n "$ONELINE" | grep -o ' ESSID:.*' | cut -f 2 -d '"'`" #'geany
  139. essidPATTERN='|'"$CELL_ESSID"'|'
  140. CONNECTDATA="`grep "$essidPATTERN" /tmp/sns_connections_wireless`"
  141. if [ "$CONNECTDATA" != "" ];then
  142. MACADDRESS="`ifconfig -a $INTERFACE | grep -o 'HWaddr .*' | cut -f 2 -d ' '`"
  143. maPATTERN='|'"$MACADDRESS"'|'
  144. CONNECTDATA="`echo "$CONNECTDATA" | grep "$maPATTERN"`"
  145. echo " MACADDRESS=$MACADDRESS CONNECTDATA=$CONNECTDATA" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  146. if [ "$CONNECTDATA" != "" ];then
  147. #note, INTERFACE not necessarily the same as in first field of CONNECTDATA.
  148. CELL_CHANNEL="`echo -n "$CONNECTDATA" | cut -f9 -d'|'`"
  149. CELL_ENCRYPTIONKEY="`echo -n "$CONNECTDATA" | cut -f11 -d'|'`"
  150. SEC_KEY="`echo -n "$CONNECTDATA" | cut -f13 -d'|'`" #ex: thebigbaddog
  151. SEC_MGMT="`echo -n "$CONNECTDATA" | cut -f14 -d'|'`" #exs: WEP, WPA-PSK
  152. ENCODEPROTO="`echo -n "$CONNECTDATA" | cut -f15 -d'|'`" #exs: WEP: restricted, open. WPA*: AES, TKIP.
  153. WPA_DRIVER="`echo -n "$CONNECTDATA" | cut -f16 -d'|'`" #ex: wext
  154. DHCPCDFIX="`echo -n "$CONNECTDATA" | cut -f17 -d'|'`" #100320 ex: -I ''
  155. iwconfig $INTERFACE mode managed
  156. echo " RESULT=$? FOR: iwconfig $INTERFACE mode managed" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  157. iwconfig $INTERFACE channel $CELL_CHANNEL
  158. echo " RESULT=$? FOR: iwconfig $INTERFACE channel $CELL_CHANNEL" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  159. iwconfig $INTERFACE essid "$CELL_ESSID"
  160. echo " RESULT=$? FOR: iwconfig $INTERFACE essid \"$CELL_ESSID\"" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  161. RUNWPASUPP='no'
  162. if [ "$CELL_ENCRYPTIONKEY" == "on" ];then
  163. case $SEC_MGMT in
  164. WEP) iwconfig $INTERFACE key $ENCODEPROTO $SEC_KEY ;; #ex: iwconfig wlan0 key open 00112233445566778899aabbcc
  165. WPA*) RUNWPASUPP='yes' ;;
  166. esac
  167. fi
  168. MODTIME1="`stat --format=%Y /etc/resolv.conf`"
  169. ifconfig $INTERFACE up #hmmm, it seems need to bring up after all the iwconfig operations!
  170. if [ $? -eq 0 ];then
  171. sleep ${SECS}
  172. echo " RESULT=$? FOR: ifconfig $INTERFACE up" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  173. wCNT=0
  174. if [ "$RUNWPASUPP" = "yes" ];then
  175. if [ -f "/etc/simple_network_setup/wpa_supplicant.conf-${CELL_ESSID}-${MACADDRESS}" ];then
  176. wpa_supplicant -B -D${WPA_DRIVER} -i${INTERFACE} -c"/etc/simple_network_setup/wpa_supplicant.conf-${CELL_ESSID}-${MACADDRESS}"
  177. echo " RESULT=$? FOR: wpa_supplicant -B -D${WPA_DRIVER} -i${INTERFACE} -c\"/etc/simple_network_setup/wpa_supplicant.conf-${CELL_ESSID}-${MACADDRESS}\"" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  178. while [ $wCNT -le 20 ];do #100314 wait until wpa_supplicant has "connected".
  179. sleep 1
  180. echo " TEST: wpa_cli -i $INTERFACE status" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  181. wpa_cli -i $INTERFACE status >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  182. echo " RESULT=$? wCNT=$wCNT" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  183. [ "`wpa_cli -i $INTERFACE status | grep '^bssid=[0-9a-f]'`" != "" ] && break
  184. #[ "`wpa_cli -i $INTERFACE status | grep 'COMPLETED'`" != "" ] && break
  185. wCNT=$(($wCNT + 1))
  186. done
  187. fi
  188. fi
  189. grep -q -s '^frisbee_mode=1' /etc/frisbee/frisbee.conf && continue #130411
  190. [ $wCNT -le 20 ] && dhcpcd $DHCPCDFIX $DROPWAIT $INTERFACE #100320 121115
  191. fi
  192. MODTIME2="`stat --format=%Y /etc/resolv.conf`"
  193. [ "`grep -v '^#' /etc/resolv.conf`" == "" ] && MODTIME2=$MODTIME1 #fail.
  194. if [ "$MODTIME2" != "$MODTIME1" ];then
  195. echo " SUCCESS" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  196. #in situation bring up interface from desktop icon...
  197. [ "$DISPLAY" ] && yaf-splash -placement bottom -bg green -timeout 5 -text "Network interface ${INTERFACE} has been activated" &
  198. echo "$INTERFACE" > /tmp/sns_interface_success #100325
  199. #break
  200. touch /tmp/sns_rc.network_exit #'exit' only terminates this code block (which is running as a separate process).
  201. exit #one internet connection is enough!
  202. else
  203. echo " FAIL" >> /tmp/simple_network_setup/rc_network_wireless_connection_log
  204. [ "`pidof wpa_supplicant`" != "" ] && wpa_cli terminate #kill wpa_supplicant.
  205. ifconfig $INTERFACE down
  206. [ "`iwconfig $INTERFACE | grep "$INTERFACE" | grep "ESSID"`" != "" ] && iwconfig $INTERFACE essid off
  207. dhcpcd --release $INTERFACE 2>/dev/null
  208. ip route flush dev $INTERFACE #100703
  209. fi
  210. fi
  211. fi
  212. done
  213. [ -f /tmp/sns_rc.network_exit ] && break #100320 bug fix, if more than one wireless interface.
  214. done
  215. [ -f /tmp/sns_rc.network_exit ] && exit
  216. ##########WIRED##########
  217. grep '|Wired|' /tmp/sns_connections_available > /tmp/sns_connections_wired
  218. for INTERFACE in $INTERFACES #exs: wlan0 eth0
  219. do
  220. [ -d /sys/class/net/${INTERFACE}/wireless ] && continue #only want wired.
  221. ifconfig $INTERFACE up > /tmp/sns_wired_log 2>&1
  222. [ $? -ne 0 ] && continue
  223. if ! ifplugstatus-0.18 "$INTERFACE" | grep -F -q 'link beat detected' ;then
  224. sleep 1
  225. if ! ifplugstatus "$INTERFACE" | grep -F -q 'link beat detected' ;then
  226. sleep 1
  227. if ! ifplugstatus-0.18 "$INTERFACE" | grep -F -q 'link beat detected' ;then
  228. sleep 1
  229. if ! ifplugstatus "$INTERFACE" | grep -F -q 'link beat detected' ;then
  230. sleep 1
  231. if ! ethtool "$INTERFACE" | grep -Fq 'Link detected: yes' ; then
  232. ifconfig $INTERFACE down
  233. continue #no network.
  234. fi
  235. fi
  236. fi
  237. fi
  238. fi
  239. grep -q -s '^frisbee_mode=1' /etc/frisbee/frisbee.conf && continue #130411
  240. MACADDRESS="`ifconfig -a $INTERFACE | grep -o 'HWaddr .*' | cut -f 2 -d ' '`"
  241. maPATTERN='|'"$MACADDRESS"'|'
  242. DHCPCDFIX="`grep "$maPATTERN" /tmp/sns_connections_wired | head -n 1 | cut -f 7 -d '|'`" #100325 ex: -I ''
  243. MODTIME1="`stat --format=%Y /etc/resolv.conf`"
  244. dhcpcd $DHCPCDFIX $INTERFACE >> /tmp/sns_wired_log 2>&1
  245. MODTIME2="`stat --format=%Y /etc/resolv.conf`"
  246. [ "`grep -v '^#' /etc/resolv.conf`" == "" ] && MODTIME2=$MODTIME1 #fail.
  247. if [ "$MODTIME2" != "$MODTIME1" ];then
  248. echo "$INTERFACE" > /tmp/sns_interface_success #100325
  249. exit #success.
  250. else
  251. ifconfig $INTERFACE down
  252. dhcpcd --release $INTERFACE 2>/dev/null
  253. ip route flush dev $INTERFACE #100703
  254. fi
  255. done
  256. ###END###