wpc-tools 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/bin/sh
  2. # Copyright (C) 2023 DSR!
  3. COMMANDS="format_sd correct_sd_mount missing_packages theme_install set_panel_port set_router_ip set_pineap_interface handle_lost_phys"
  4. HELP="
  5. Available commands:
  6. format_sd Format SD/pendrive for use with Pineapple
  7. correct_sd_mount Fix ghost SD/pendrive issues
  8. missing_packages Install the missing OpenWRT packages
  9. theme_install Deploys the tool to change panel theme
  10. set_panel_port Change the port used by panel
  11. set_router_ip Change the IP used by the hardware
  12. set_pineap_interface Change the interface used by PineAP
  13. handle_lost_phys Fix unrecognized wifi interfaces
  14. "
  15. PACKAGES="python-logging python-openssl python-sqlite3 python-codecs"
  16. to_logger() {
  17. logger -s -t wpc "$1"
  18. }
  19. format_sd() {
  20. to_logger "[+] Formatting SD using the panel script..."
  21. /pineapple/modules/Advanced/formatSD/format_sd
  22. to_logger "Process finished. Read the log to see if it was completed correctly."
  23. to_logger "The partition may take a few seconds to become available."
  24. }
  25. correct_sd_mount() {
  26. SD_STATUS=$(/bin/mount | /bin/grep "on /sd" -c)
  27. SD_COUNT=$(ls /sd | wc -l)
  28. if [[ -d /sd && $SD_STATUS == "0" && $SD_COUNT == "0" ]]; then
  29. to_logger "[+] Fix sd status"
  30. rm -rf /sd
  31. fi
  32. }
  33. missing_packages() {
  34. if [[ ! -d "/usr/lib/python2.7" && ! -d "/sd/usr/lib/python2.7" ]]; then
  35. FREE_SPACE=$(df / | tail -1 | awk '{print $4}')
  36. if [[ ! -d /sd && $FREE_SPACE -lt 10240 ]]; then
  37. to_logger "[!] There is not enough space to install the packages"
  38. elif ping -q -c 1 -W 1 1.1.1.1 >/dev/null; then
  39. to_logger "[+] Installing missing packages..."
  40. INSTALL_ROUTE="--dest sd"
  41. if [[ $FREE_SPACE -gt 10240 ]]; then
  42. INSTALL_ROUTE=""
  43. to_logger "[*] Found available space in the system partition"
  44. fi
  45. opkg update && opkg $INSTALL_ROUTE install $PACKAGES && python -m compileall
  46. if [[ ! -d "/usr/lib/python2.7" && ! -d "/sd/usr/lib/python2.7" ]]; then
  47. to_logger "[!] Packages were not installed!"
  48. else
  49. to_logger "[*] Install Complete!"
  50. fi
  51. else
  52. to_logger "[!] Connect to the internet and run \"wpc-tools missing_packages\" command!"
  53. fi
  54. fi
  55. }
  56. theme_install() {
  57. to_logger "[+] Downloading theme manager..."
  58. wget -q "https://raw.githubusercontent.com/xchwarze/wifi-pineapple-community/main/themes/install.sh" -O /tmp/theme-install.sh
  59. chmod +x /tmp/theme-install.sh
  60. to_logger "[*] By running /tmp/theme-install.sh you will be able to see the available themes and choose the one you want"
  61. /tmp/theme-install.sh "$1"
  62. }
  63. set_panel_port() {
  64. new_port="$1"
  65. config_file="/etc/nginx/nginx.conf"
  66. if [ -z "$new_port" ] || ! echo "$new_port" | grep -qE '^[1-9][0-9]{0,4}$' || [ "$new_port" -gt 65535 ]; then
  67. to_logger "[!] Error: You must provide a valid TCP port (1-65535) as a parameter"
  68. exit 1
  69. fi
  70. old_port=$(awk '/listen/ {++counter; if(counter==2) print NR}' "$config_file")
  71. sed -i "${old_port}s/[0-9]\+/$new_port/" "$config_file"
  72. /etc/init.d/nginx restart
  73. uci set firewall.allowui.dest_port="$new_port"
  74. uci commit firewall
  75. /etc/init.d/firewall restart
  76. to_logger "[+] The port has been changed to: $new_port"
  77. }
  78. set_router_ip() {
  79. new_ip="$1"
  80. if [ -z "$new_ip" ] || ! echo "$new_ip" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then
  81. to_logger "[!] Error: You must provide a valid IP address as a parameter"
  82. exit 1
  83. fi
  84. gateway_ip="${new_ip%.*}.42"
  85. uci set network.lan.ipaddr="$gateway_ip"
  86. uci set network.lan.gateway="$new_ip"
  87. uci commit network
  88. to_logger "[+] The LAN IP address has been updated to: $new_ip"
  89. /etc/init.d/network restart
  90. }
  91. set_pineap_interface() {
  92. new_iface=$(echo $1 | sed 's/mon//')
  93. iface=$(uci get pineap.@config[0].pineap_interface | sed 's/mon//')
  94. if [[ "$new_iface" == "" ]]; then
  95. to_logger "[!] Error: You must select a new interface to assign to"
  96. exit 1
  97. fi
  98. to_logger "[+] Current interface : ${iface}"
  99. to_logger "[+] New interface : ${new_iface}"
  100. airmon-ng stop "${iface}mon" &>/dev/null
  101. airmon-ng stop "${new_iface}mon" &>/dev/null
  102. uci set pineap.@config[0].pineap_interface="${new_iface}mon"
  103. uci commit pineap
  104. /etc/init.d/pineapd restart
  105. }
  106. # based on airmon-ng code
  107. handle_lost_phys() {
  108. to_logger "[+] Looking for unrecognized wifi interfaces..."
  109. if [ -d /sys/class/ieee80211 ]; then
  110. for i in $(ls /sys/class/ieee80211/); do
  111. if [ ! -d /sys/class/ieee80211/${i}/device/net ]; then
  112. to_logger "[*] Found ${i} with no interface assigned!"
  113. find_free_interface ${i}
  114. fi
  115. done
  116. fi
  117. to_logger "[*] Check completed"
  118. }
  119. find_free_interface() {
  120. PHYDEV="${1}"
  121. target_mode="station"
  122. target_type="1"
  123. for i in $(seq 0 100); do
  124. if [ "$i" = "100" ]; then
  125. to_logger "[!] Unable to find a free name between wlan0 and wlan99"
  126. return 1
  127. fi
  128. if [ ! -e /sys/class/net/wlan${i} ] && [ ! -e /sys/class/net/wlan${i}mon ]; then
  129. to_logger "[*] Candidate wlan${i} and wlan${i}mon are both clear, creating wlan${i}"
  130. IW_ERROR="$(iw phy ${PHYDEV} interface add wlan${i} type ${target_mode} 2>&1)"
  131. if [ -z "${IW_ERROR}" ]; then
  132. if [ -d /sys/class/ieee80211/${PHYDEV}/device/net ]; then
  133. for j in $(ls /sys/class/ieee80211/${PHYDEV}/device/net/); do
  134. if [ "$(cat /sys/class/ieee80211/${PHYDEV}/device/net/${j}/type)" = "${target_type}" ]; then
  135. k=${j#wlan}
  136. i=${k%mon}
  137. fi
  138. done
  139. else
  140. to_logger "[!] Unable to create wlan${i} and no error received"
  141. return 1
  142. fi
  143. to_logger "[!] mac80211 ${target_mode} mode vif enabled on [${PHYDEV}]wlan${i}"
  144. unset IW_ERROR
  145. break
  146. else
  147. to_logger "[!] Error: Adding ${target_mode} mode interface: ${IW_ERROR}"
  148. break
  149. fi
  150. fi
  151. done
  152. }
  153. # handle commands
  154. if [[ $# -gt 0 ]]; then
  155. if echo "${COMMANDS}" | grep -wq "$1"; then
  156. $1 "$2"
  157. else
  158. to_logger "Unknown command: $1"
  159. echo "${HELP}"
  160. fi
  161. else
  162. echo "${HELP}"
  163. fi