Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #---------------------------------------------------------------------------
  4. # Variables declaration
  5. #---------------------------------------------------------------------------
  6.  
  7. # Uncomment if broadcast-relaying is necessary
  8. #bcRelay_ON=1
  9.  
  10. wlanCXed=0
  11.  
  12. idx=0
  13. err_ON=0
  14.  
  15. scriptPath=""
  16.  
  17. #---------------------------------------------------------------------------
  18. # Local Functions
  19. #---------------------------------------------------------------------------
  20.  
  21. arp_routing(){
  22. local __iface1=$1
  23. local __iface2=$2
  24.  
  25. parprouted $__iface1 $__iface2 >> "$scriptPath" 2>&1
  26. if [ "$(echo $?)" -ne 0 ]
  27. then
  28. sleep 1
  29. return 1
  30. fi
  31. return 0
  32. }
  33.  
  34. dhcp_relay(){
  35. local __iface1=$1
  36. local __iface2=$2
  37.  
  38. #local dhcp_server=$(grep -w "dhcp-server" \
  39. #/var/lib/dhcp/dhclient.$__iface1.leases | tail -n1 | awk '{print $3}' \
  40. #| cut -d';' -f1)
  41. local dhcp_server=$(grep -R "offered" \
  42. /var/log/* | tail -n1 | awk '{print $(NF)}')
  43.  
  44. dhcp-helper -s "$dhcp_server" -i "$__iface2" >> "$scriptPath" 2>&1
  45. if [ "$(echo $?)" -ne 0 ]
  46. then
  47. sleep 1
  48. return 1
  49. fi
  50. return 0
  51. }
  52.  
  53. host_ap(){
  54. hostapd -B /etc/hostapd/hostapd.conf >> "$scriptPath" 2>&1
  55. if [ "$(echo $?)" -ne 0 ]
  56. then
  57. sleep 1
  58. return 1
  59. fi
  60. return 0
  61. }
  62.  
  63. bc_relay(){
  64. local __iface1=$1
  65. local __iface2=$2
  66.  
  67. bcrelay -d -n -i "$__iface1" -o "$__iface2" >> "$scriptPath" 2>&1
  68. if [ "$(echo $?)" -ne 0 ]
  69. then
  70. sleep 1
  71. return 1
  72. fi
  73. return 0
  74. }
  75.  
  76. kill_process(){
  77. local __proc2kill=$1
  78. local CMDresult=$(ps aux | grep -v grep | grep "$__proc2kill")
  79. if [ -n "$CMDresult" ]
  80. then
  81. killall "$__proc2kill" >> "$scriptPath" 2>&1
  82. #while read line
  83. #do
  84. # kill $(echo $line | cut -d' ' -f2) >> "$scriptPath" 2>&1
  85. #done <<< $CMDresult
  86. fi
  87. }
  88. RST_process(){
  89. until ( ifconfig wlan1 down >> "$scriptPath" 2>&1 )
  90. do
  91. sleep 1
  92. done
  93. sleep 1
  94.  
  95. until (ip addr flush dev wlan1 >> "$scriptPath" 2>&1)
  96. do
  97. sleep 1
  98. done
  99.  
  100. kill_process "parprouted"
  101.  
  102. kill_process "dhcp-helper"
  103.  
  104. kill_process "hostapd"
  105.  
  106. # Executes this section if bcRelay_ON is enabled (not commented)
  107. if [ -v bcRelay_ON ]
  108. then
  109. kill_process "bcrelay"
  110. fi
  111.  
  112. sleep 1
  113. }
  114.  
  115. CX_start(){
  116. err_ON=0
  117. RST_process
  118.  
  119. wlan0Info=""
  120. while [ "$wlan0Info" == "" ]
  121. do
  122. # Check if wlan0 has an IP. Drops results and logs error messages
  123. if [ "$(ifconfig wlan0 2>>"$scriptPath" | grep -w "inet" | awk '{print $2}')" != "" ]
  124. then
  125. # Using wlan0Info just as a test condition to help exit this loop.
  126. # wlan0Info is reused later
  127. wlan0Info="exit"
  128. else
  129. sleep 1
  130. fi
  131. done
  132.  
  133. wlan0Info=$(ip a show wlan0)
  134. wlan0IP=$(echo "$wlan0Info" | grep -w "inet" | awk '{print $2}' | cut -d'/' -f1)
  135. wlanIPMask=$(echo "$wlan0Info" | grep -w "inet" | awk '{print $2}' | cut -d'/' -f2)
  136. wlanbrd=$(echo "$wlan0Info" | grep -w "inet" | awk '{print $4}')
  137. wlan1IP1=$(echo "$wlanbrd" | cut -d'.' -f1)
  138. wlan1IP2=$(echo "$wlanbrd" | cut -d'.' -f2)
  139. wlan1IP3=$(echo "$wlanbrd" | cut -d'.' -f3)
  140. wlan1IP4=$(($(echo "$wlanbrd" | cut -d'.' -f4) - 1))
  141. wlan1IP=$(echo "$wlan1IP1"'.'"$wlan1IP2"'.'"$wlan1IP3"'.'"$wlan1IP4")
  142.  
  143. until ( ifconfig wlan1 up >> "$scriptPath" 2>&1 )
  144. do
  145. sleep 1
  146. done
  147. sleep 1
  148.  
  149. until (ip addr flush dev wlan1 >> "$scriptPath" 2>&1)
  150. do
  151. sleep 1
  152. done
  153.  
  154. until ( ip addr add $(echo "$wlan1IP"'/'"$wlanIPMask") brd + dev wlan1 \
  155. >> "$scriptPath" 2>&1 )
  156. do
  157. sleep 1
  158. done
  159.  
  160. until ( $(arp_routing "wlan0" "wlan1") )
  161. do
  162. idx=$(("$idx"+1))
  163. if [ "$idx" -eq 5 ]
  164. then
  165. echo "parprouted failed" >> "$scriptPath" 1>&1
  166. err_ON=1
  167. break
  168. fi
  169. done
  170.  
  171. # echo "here1"
  172.  
  173. if [ $err_ON -eq 0 ]
  174. then
  175. idx=0
  176. until ( $(dhcp_relay "wlan0" "wlan1") )
  177. do
  178. idx=$(("$idx"+1))
  179. if [ "$idx" -eq 5 ]
  180. then
  181. echo "dhcp-relay failed" >> "$scriptPath" 1>&1
  182. err_ON=1
  183. break
  184. fi
  185. done
  186. fi
  187.  
  188. if [ $err_ON -eq 0 ]
  189. then
  190. idx=0
  191. until ( $(host_ap) )
  192. do
  193. idx=$(("$idx"+1))
  194. if [ "$idx" -eq 5 ]
  195. then
  196. echo "Host AP failed" >> "$scriptPath" 1>&1
  197. err_ON=1
  198. break
  199. fi
  200. done
  201. fi
  202.  
  203. # Executes this section if bcRelay_ON is enabled (not commented)
  204. if [ -v bcRelay_ON ]
  205. then
  206. for i in $(seq 1 2)
  207. do
  208. if [ $err_ON -eq 0 ]
  209. then
  210. idx=0
  211. iface1="wlan0"
  212. iface2="wlan1"
  213. if [ $i -eq 2 ]
  214. then
  215. iface1="wlan1"
  216. iface2="wlan0"
  217. fi
  218. until ( $(bc_relay "$iface1" "$iface2") )
  219. do
  220. idx=$(("$idx"+1))
  221. if [ "$idx" -eq 5 ]
  222. then
  223. echo "BC_Relay failed" >> "$scriptPath" 1>&1
  224. err_ON=1
  225. break
  226. fi
  227. done
  228. fi
  229. done
  230. fi
  231.  
  232. if [ $err_ON -eq 0 ]
  233. then
  234. wlanCXed=1
  235. else
  236. sleep 10
  237. fi
  238. }
  239.  
  240. check_processes(){
  241. if [ "$(ps aux | grep -v grep | grep parprouted)" == "" ]
  242. then
  243. return 1
  244. elif [ "$(ps aux | grep -v grep | grep dhcp-helper)" == "" ]
  245. then
  246. return 1
  247. elif [ "$(ps aux | grep -v grep | grep hostapd)" == "" ]
  248. then
  249. return 1
  250. elif [ -v bcRelay_ON ]
  251. then
  252. if [ "$(ps aux | grep -v grep | grep bcrelay)" == "" ]
  253. then
  254. return 1
  255. fi
  256. fi
  257. return 0
  258. }
  259. CX_check(){
  260. if [ "$(ifconfig wlan0 | grep inet)" != "" ] && \
  261. [ $(check_processes; echo $?) -eq 0 ]
  262. then
  263. sleep 1m
  264. else
  265. RST_process
  266. wlanCXed=0
  267. fi
  268. }
  269.  
  270.  
  271. #---------------------------------------------------------------------------
  272. # Main
  273. #---------------------------------------------------------------------------
  274.  
  275. scriptPath="$(cd "$(dirname $0)"; pwd -P)""/log_err"
  276. echo "Start" > "$scriptPath" 1>&1
  277.  
  278. # Main Loop. Starts and keep checking on connection to not to break, and
  279. # all of the packages used to not to be ended. If any of this two conditions
  280. # happen, kills all processes, wait the connection to the router
  281. # to be re-stablished and start all processes again
  282. while true
  283. do
  284. if [ $wlanCXed -eq 0 ]
  285. then
  286. CX_start
  287. else
  288. CX_check
  289. fi
  290. done
  291.  
  292. exit 0
  293.  
  294. #---------------------------------------------------------------------------
  295. # END
  296. #---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement