wp6.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/bin/bash
  2. # WiFi Pineapple Connector for Linux
  3. # EULA https://www.wifipineapple.com/licence/eula.txt
  4. # License https://www.wifipineapple.com/licence/software_licence.txt
  5. wpver=6.5
  6. spineapplenmask=255.255.255.0
  7. spineapplenet=172.16.42.0/24
  8. spineapplelan=eth1
  9. spineapplewan=wlan0
  10. spineapplegw=192.168.1.1
  11. spineapplehostip=172.16.42.42
  12. spineappleip=172.16.42.1
  13. sfirsttime=1
  14. if [ "$EUID" -ne 0 ]
  15. then echo "这个WiFi菠萝连接脚本需要root。"
  16. sudo su -s "$0"
  17. exit
  18. fi
  19. function banner {
  20. # Show random banner because 1337
  21. b=$(( ( RANDOM % 5 ) + 1 ))
  22. case "$b" in
  23. 1)
  24. echo $(tput setaf 3)
  25. echo " _ ___ _______ ____ _ __ ";
  26. echo " | | / (_) ____(_) / __ \\(_)___ ___ ____ _____ ____ / /__ ";
  27. echo " | | /| / / / /_ / / / /_/ / / __ \/ _ \/ __ '/ __ \/ __ \/ / _ \\";
  28. echo " | |/ |/ / / __/ / / / ____/ / / / / __/ /_/ / /_/ / /_/ / / __/ ";
  29. echo " |__/|__/_/_/ /_/ /_/ /_/_/ /_/\___/\__,_/ .___/ .___/_/\___/ ";
  30. echo " $(tput setaf 3)/_/ /_/$(tput sgr0) v$wpver";
  31. ;;
  32. 2)
  33. echo $(tput setaf 3)
  34. echo " ___ __ ___ __ __ ___ ";
  35. echo " | | | |__ | |__) | |\ | |__ /\ |__) |__) | |__ ";
  36. echo " |/\| | | | | | | \| |___ /~~\ | | |___ |___ ";
  37. echo " $(tput sgr0)v$wpver"
  38. ;;
  39. 3)
  40. echo $(tput setaf 3)
  41. echo " ▄▄▌ ▐ ▄▌▪ ·▄▄▄▪ ▄▄▄·▪ ▐ ▄ ▄▄▄ . ▄▄▄· ▄▄▄· ▄▄▄·▄▄▌ ▄▄▄ .";
  42. echo " ██· █▌▐███ ▐▄▄·██ ▐█ ▄███ •█▌▐█▀▄.▀·▐█ ▀█ ▐█ ▄█▐█ ▄███• ▀▄.▀·";
  43. echo " ██▪▐█▐▐▌▐█·██▪ ▐█· ██▀·▐█·▐█▐▐▌▐▀▀▪▄▄█▀▀█ ██▀· ██▀·██▪ ▐▀▀▪▄";
  44. echo " ▐█▌██▐█▌▐█▌██▌.▐█▌ ▐█▪·•▐█▌██▐█▌▐█▄▄▌▐█ ▪▐▌▐█▪·•▐█▪·•▐█▌▐▌▐█▄▄▌";
  45. echo " ▀▀▀▀ ▀▪▀▀▀▀▀▀ ▀▀▀ .▀ ▀▀▀▀▀ █▪ ▀▀▀ ▀ ▀ .▀ .▀ .▀▀▀ ▀▀▀ ";
  46. echo " $(tput sgr0)v$wpver"
  47. ;;
  48. 4)
  49. echo $(tput setaf 3)
  50. echo " ▄ ▄ ▄█ ▄████ ▄█ █ ▄▄ ▄█ ▄ ▄███▄ ██ █ ▄▄ █ ▄▄ █ ▄███▄ ";
  51. echo " █ █ ██ █▀ ▀ ██ █ █ ██ █ █▀ ▀ █ █ █ █ █ █ █ █▀ ▀";
  52. echo "█ ▄ █ ██ █▀▀ ██ █▀▀▀ ██ ██ █ ██▄▄ █▄▄█ █▀▀▀ █▀▀▀ █ ██▄▄ ";
  53. echo "█ █ █ ▐█ █ ▐█ █ ▐█ █ █ █ █▄ ▄▀ █ █ █ █ ███▄ █▄ ▄";
  54. echo " █ █ █ ▐ █ ▐ █ ▐ █ █ █ ▀███▀ █ █ █ ▀ ▀███▀ ";
  55. echo " ▀ ▀ ▀ ▀ █ ██ █ ▀ ▀ $(tput sgr0)v$wpver";
  56. ;;
  57. 5)
  58. echo $(tput setaf 3)
  59. echo " ( ( ";
  60. echo " ( ( )\ ) )\ ) ( ";
  61. echo " )\))( ' ( (()/( ( (()/( ( ( ) )\ ( ";
  62. echo "((_)()\ ) )\ /(_)) )\ /(_)))\ ( ))\ ( /( \` ) \` ) ((_) ))\ ";
  63. echo "_(())\_)()((_)(_))_|((_) (_)) ((_) )\ ) /((_))(_)) /(/( /(/( _ /((_) ";
  64. echo "\ \((_)/ / (_)| |_ (_) | _ \ (_) _(_/( (_)) ((_)_ ((_)_\ ((_)_\ | |(_)) ";
  65. echo " \ \/\/ / | || __| | | | _/ | || ' \))/ -_)/ _\` || '_ \)| '_ \)| |/ -_) ";
  66. echo " \_/\_/ |_||_| |_| |_| |_||_||_| \___|\__,_|| .__/ | .__/ |_|\___| ";
  67. echo " |_| |_| $(tput sgr0)v$wpver";
  68. ;;
  69. esac
  70. }
  71. function showsettings {
  72. printf "\n\
  73. $(tput bold)Saved Settings$(tput sgr0): Share Internet connection from $spineapplewan\n\
  74. to WiFi Pineapple at $spineapplelan through default gateway $spineapplegw\n"
  75. }
  76. function menu {
  77. printf "\n\
  78. [$(tput bold)C$(tput sgr0)]onnect using saved settings\n\
  79. [$(tput bold)G$(tput sgr0)]uided setup (recommended)\n\
  80. [$(tput bold)M$(tput sgr0)]anual setup\n\
  81. [$(tput bold)A$(tput sgr0)]dvanced IP settings\n\
  82. [$(tput bold)Q$(tput sgr0)]uit\n\n "
  83. read -r -sn1 key
  84. case "$key" in
  85. [gG]) guidedsetup;;
  86. [mM]) manualsetup;;
  87. [cC]) connectsaved;;
  88. [aA]) advancedsetup;;
  89. [bB]) bunny;;
  90. [qQ]) printf "\n"; exit;;
  91. esac
  92. }
  93. function manualsetup {
  94. ipinstalled=$(which ip)
  95. if [[ "$?" == 0 ]]; then
  96. ifaces=($(ip link show | grep -v link | awk {'print $2'} | sed 's/://g' | grep -v lo))
  97. printf "\n Select WiFi Pineapple Interface:\n"
  98. for i in "${!ifaces[@]}"; do
  99. printf " [$(tput bold)%s$(tput sgr0)]\t%s\t" "$i" "${ifaces[$i]}"
  100. printf "$(ip -4 addr show ${ifaces[$i]} | grep inet | awk {'print $2'} | head -1)\n"
  101. done
  102. read -r -p " > " planq
  103. if [ "$planq" -eq "$planq" ] 2>/dev/null; then
  104. spineapplelan=(${ifaces[planq]})
  105. else
  106. printf "\n 响应必须是一个列出的数字选项\n"; manualsetup
  107. fi
  108. printf "\n Select Internet Interface:\n"
  109. for i in "${!ifaces[@]}"; do
  110. printf " [$(tput bold)%s$(tput sgr0)]\t%s\t" "$i" "${ifaces[$i]}"
  111. printf "$(ip -4 addr show ${ifaces[$i]} | grep inet | awk {'print $2'} | head -1)\n"
  112. done
  113. read -r -p " > " inetq
  114. if [ "$inetq" -eq "$inetq" ] 2>/dev/null; then
  115. spineapplewan=(${ifaces[inetq]})
  116. else
  117. printf "\n 响应必须是一个列出的数字选项\n"; manualsetup
  118. fi
  119. printf "\n$(netstat -nr)\n\n"
  120. read -r -p " 指定默认网关IP地址: " spineapplegw
  121. savechanges
  122. else
  123. printf "\n\n Configuration requires the 'iproute2' package (aka the 'ip' command).\n Please install 'iproute2' to continue.\n"
  124. menu
  125. fi
  126. }
  127. function guidedsetup {
  128. pineappledetected=$(ip addr | grep '00:[cC]0:[cC][aA]\|00:13:37' -B1 | awk {'print $2'} | head -1 | grep 'eth\|en')
  129. if [[ "$?" == 0 ]]; then
  130. printf "\n 检测到WiFi菠萝。请从断开WiFi菠萝\n this computer and $(tput bold)press any key$(tput sgr0) to continue with guided setup.\n "
  131. read -r -sn1 anykey
  132. guidedsetup
  133. fi
  134. hasiproute2=$(which ip)
  135. if [[ "$?" == 1 ]]; then
  136. printf "\n\n Configuration requires the 'iproute2' package (aka the 'ip' command).\n Please install 'iproute2' to continue.\n"; menu
  137. fi
  138. hasdefaultroute=$(ip route)
  139. if [[ "$?" == 1 ]]; then
  140. printf "\n No route detected. Check connection and try again.\n"; menu
  141. fi
  142. printf "\n $(tput setaf 3)Step 1 of 3: Select Default Gateway$(tput sgr0)\n\
  143. Default gateway reported as $(tput bold)$(ip route | grep default | awk {'print $3'} | head -1)$(tput sgr0)\n"
  144. read -r -p " Use the above reported default gateway? [Y/n]? " usedgw
  145. case $usedgw in
  146. [yY][eE][sS]|[yY]|'')
  147. spineapplegw=($(ip route | grep default | awk {'print $3'}))
  148. ;;
  149. [nN][oO]|[nN])
  150. printf "\n$(ip route)\n\n"
  151. read -r -p " Specify the default gateway by IP address: " spineapplegw
  152. ;;
  153. esac
  154. printf "\n $(tput setaf 3)Step 2 of 3: Select Internet Interface$(tput sgr0)\n\
  155. Internet interface reported as $(tput bold)$(ip route | grep default | awk {'print $5'} | head -1)$(tput sgr0)\n"
  156. read -r -p " Use the above reported Internet interface? [Y/n]? " useii
  157. case $useii in
  158. [yY][eE][sS]|[yY]|'')
  159. spineapplewan=($(ip route | grep default | awk {'print $5'}))
  160. ;;
  161. [nN][oO]|[nN])
  162. printf "\n Available Network Interfaces:\n"
  163. ifaces=($(ip link show | grep -v link | awk {'print $2'} | sed 's/://g' | grep -v lo))
  164. for i in "${!ifaces[@]}"; do
  165. printf " \t%s\t" "${ifaces[$i]}"
  166. printf "$(ip -4 addr show ${ifaces[$i]} | grep inet | awk {'print $2'} | head -1)\n"
  167. done
  168. read -r -p " Specify the internet interface by name: " spineapplewan
  169. ;;
  170. esac
  171. printf "\n $(tput setaf 3)Step 3 of 3: Select WiFi Pineapple Interface$(tput sgr0)\n Please connect the WiFi Pineapple to this computer.\n "
  172. a="0"
  173. until pineappleiface=$(ip addr | grep '00:[cC]0:[cC][aA]\|00:13:37' -B1 | awk {'print $2'} | head -1 | grep 'eth\|en')
  174. do
  175. printf "."
  176. sleep 1
  177. a=$[$a+1]
  178. if [[ $a == "51" ]]; then
  179. printf "\n "
  180. a=0
  181. fi
  182. done
  183. printf "[Checking]"
  184. sleep 5 # Wait as the system is likely to rename interface. Sleeping rather than more advanced error handling becasue reasons. Try: dmesg | tail | grep -E 'asix.*renamed'
  185. pineappleiface=$(ip addr | grep '00:[cC]0:[cC][aA]\|00:13:37' -B1 | awk {'print $2'} | head -1 | grep 'eth\|en' | sed 's/://g')
  186. printf "\n Detected WiFi Pineapple on interface $(tput bold)$pineappleiface$(tput sgr0)\n";
  187. read -r -p " 使用上面检测到的WiFi菠萝接口? [Y/n]? " pi
  188. case $pi in
  189. [yY][eE][sS]|[yY]|'')
  190. spineapplelan=$pineappleiface
  191. ;;
  192. [nN][oO]|[nN])
  193. printf "\n Available Network Interfaces:\n"
  194. ifaces=($(ip link show | grep -v link | awk {'print $2'} | sed 's/://g' | grep -v lo))
  195. for i in "${!ifaces[@]}"; do
  196. printf " \t%s\t" "${ifaces[$i]}"
  197. printf "$(ip -4 addr show ${ifaces[$i]} | grep inet | awk {'print $2'} | head -1)\n"
  198. done
  199. read -r -p " 按名称指定WiFi菠萝接口: " spineapplelan
  200. ;;
  201. esac
  202. savechanges
  203. }
  204. function advancedsetup {
  205. printf "\n\
  206. By default the WiFi Pineapple resides on the $(tput bold)172.16.42.0/24$(tput sgr0) network\n\
  207. with the IP Address $(tput bold)172.16.42.1$(tput sgr0) and Ethernet default route $(tput bold)172.16.42.42$(tput sgr0).\n\n\
  208. WiFi菠萝期望从172.16.42.42通过互联网连接\n\
  209. 默认设置,该脚本将帮助您进行配置。这些IP地址可以\n\
  210. 如果需要,可通过修改WiFi菠萝上的网络配置进行更改。\n\n"
  211. read -r -p " 继续高级IP配置 [y/N]? " qcontinue
  212. case $qcontinue in
  213. [nN][oO]|[nN]|'') menu ;;
  214. [yY][eE][sS]|[yY])
  215. read -r -p " WiFi菠萝网 [172.16.42.0/24]: " spineapplenet
  216. if [[ $spineapplenet == '' ]]; then
  217. spineapplenet=172.16.42.0/24 # Pineapple network. Default is 172.16.42.0/24
  218. fi
  219. read -r -p " WiFi菠萝子网掩码 [255.255.255.0]: " spineapplenmask
  220. if [[ $spineapplenmask == '' ]]; then
  221. spineapplenmask=255.255.255.0 #Default netmask for /24 network
  222. fi
  223. read -r -p " 主机IP地址 [172.16.42.42]: " spineapplehostip
  224. if [[ $spineapplehostip == '' ]]; then
  225. spineapplehostip=172.16.42.42 #IP Address of host computer
  226. fi
  227. read -r -p " WiFi菠萝IP地址 [172.16.42.1]: " spineappleip
  228. if [[ $spineappleip == '' ]]; then
  229. spineappleip=172.16.42.1 #Don't forget your towel
  230. fi
  231. printf "\n 高级IP设置将被保存以供将来的会话使用。\n 可以通过选择高级IP设置和恢复默认设置\n 当提示输入IP设置时,按[ENTER]。\n\n 按任意键继续"
  232. savechanges
  233. ;;
  234. esac
  235. }
  236. function savechanges {
  237. sed -i "s/^spineapplenmask.*/spineapplenmask=$spineapplenmask/" $0
  238. sed -i "s&^spineapplenet.*&spineapplenet=$spineapplenet&" $0
  239. sed -i "s/^spineapplelan.*/spineapplelan=$spineapplelan/" $0
  240. sed -i "s/^spineapplewan.*/spineapplewan=$spineapplewan/" $0
  241. sed -i "s/^spineapplegw.*/spineapplegw=$spineapplegw/" $0
  242. sed -i "s/^spineapplehostip.*/spineapplehostip=$spineapplehostip/" $0
  243. sed -i "s/^spineappleip.*/spineappleip=$spineappleip/" $0
  244. sed -i "s/^sfirsttime.*/sfirsttime=0/" $0
  245. sfirsttime=0
  246. printf "\n Settings saved.\n"
  247. showsettings
  248. menu
  249. }
  250. function connectsaved {
  251. if [[ "$sfirsttime" == "1" ]]; then
  252. printf "\n 错误:设置未保存。首先运行引导式或手动安装。\n"; menu
  253. fi
  254. ifconfig $spineapplelan $spineapplehostip netmask $spineapplenmask up #Bring up Ethernet Interface directly connected to Pineapple
  255. printf "检测WiFi菠萝..."
  256. until ping $spineappleip -c1 -w1 >/dev/null
  257. do
  258. printf "."
  259. ifconfig $spineapplelan $spineapplehostip netmask $spineapplenmask up &>/dev/null
  260. sleep 1
  261. done
  262. printf "...found.\n\n"
  263. printf " $(tput setaf 6) _ . $(tput sgr0) $(tput setaf 7)___$(tput sgr0) $(tput setaf 3)\||/$(tput sgr0)\n"
  264. printf " $(tput setaf 6) ( _ )_ $(tput sgr0) $(tput setaf 2)<-->$(tput sgr0) $(tput setaf 7)[___]$(tput sgr0) $(tput setaf 2)<-->$(tput sgr0) $(tput setaf 3),<><>,$(tput sgr0)\n"
  265. printf " $(tput setaf 6) (_ _(_ ,)$(tput sgr0) $(tput setaf 7)\___\\$(tput sgr0) $(tput setaf 3)'<><>'$(tput sgr0)\n"
  266. ifconfig $spineapplelan $spineapplehostip netmask $spineapplenmask up #Bring up Ethernet Interface directly connected to Pineapple
  267. echo '1' > /proc/sys/net/ipv4/ip_forward # Enable IP Forwarding
  268. iptables -X #clear chains and rules
  269. iptables -F
  270. iptables -A FORWARD -i $spineapplewan -o $spineapplelan -s $spineapplenet -m state --state NEW -j ACCEPT #setup IP forwarding
  271. iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
  272. iptables -A POSTROUTING -t nat -j MASQUERADE
  273. route del default #remove default route
  274. route add default gw $spineapplegw $spineapplewan #add default gateway
  275. printf "\n 浏览至 http://$spineappleip:1471\n\n"
  276. exit
  277. }
  278. function bunny {
  279. printf "\nNetmask $spineapplenmask\nPineapple Net $spineapplenet\nPineapple LAN $spineapplelan\nPineapple WAN $spineapplewan\nPineapple GW $spineapplegw\nPineapple IP $spineappleip\nHost IP $spineapplehostip\n"
  280. printf "\n$(lsusb | grep ASIX)\n\n$(ifconfig -a)\n\n$(route)\n\n$(dmesg | grep -E '[aA][sS][iI[[xX].*|00:[cC]0:[cC][aA]')\n"
  281. printf "\n/)___(\ \n(='.'=)\n(\")_(\")\n"
  282. exit
  283. }
  284. banner #remove for less 1337
  285. showsettings
  286. if [[ "$sfirsttime" == "1" ]]; then
  287. printf "
  288. 因为这是第一次运行WP6互联网连接共享\n\
  289. 脚本,建议使用引导式设置来保存初始配置。\n\
  290. 后续会话可以使用保存的设置快速连接。\n"
  291. fi
  292. menu