1
0

ports.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #!/bin/bash
  2. clear
  3. port(){
  4. local portas
  5. local portas_var=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" |grep -v "COMMAND" | grep "LISTEN")
  6. i=0
  7. while read port; do
  8. var1=$(echo $port | awk '{print $1}') && var2=$(echo $port | awk '{print $9}' | awk -F ":" '{print $2}')
  9. [[ "$(echo -e ${portas}|grep -w "$var1 $var2")" ]] || {
  10. portas+="$var1 $var2 $portas"
  11. echo "$var1 $var2"
  12. let i++
  13. }
  14. done <<< "$portas_var"
  15. }
  16. verify_port () {
  17. local SERVICE="$1"
  18. local PORTENTRY="$2"
  19. [[ ! $(echo -e $(port|grep -v ${SERVICE})|grep -w "$PORTENTRY") ]] && return 0 || return 1
  20. }
  21. edit_squid(){
  22. if [[ -e /etc/squid/squid.conf ]]; then
  23. local CONF="/etc/squid/squid.conf"
  24. elif [[ -e /etc/squid3/squid.conf ]]; then
  25. local CONF="/etc/squid3/squid.conf"
  26. fi
  27. local line="$(cat ${CONF}|sed -n '/http_port/='|head -1)"
  28. local NEWCONF="$(cat ${CONF}|sed "$line c ADMR_port"|sed '/http_port/d')"
  29. title "$(fun_trans "REDEFINIR PUERTOS SQUID")"
  30. msg -ne " $(fun_trans " Digite Puertos"): " && read DPORT
  31. tput cuu1 && tput dl1
  32. TTOTAL=($DPORT)
  33. for((i=0; i<${#TTOTAL[@]}; i++)); do
  34. [[ $(mportas|grep -v squid|grep -v '>'|grep -w "${TTOTAL[$i]}") = "" ]] && {
  35. echo -e "\033[1;33m Puerto Elegido:\033[1;32m ${TTOTAL[$i]} OK"
  36. PORT="$PORT ${TTOTAL[$i]}"
  37. } || {
  38. echo -e "\033[1;33m Puerto Elegido:\033[1;31m ${TTOTAL[$i]} FAIL"
  39. }
  40. done
  41. [[ -z $PORT ]] && {
  42. msg -bar
  43. print_center -verm2 "Ningun Puerto Valido"
  44. return 1
  45. }
  46. rm ${CONF}
  47. while read varline; do
  48. if [[ ! -z "$(echo "$varline"|grep 'ADMR_port')" ]]; then
  49. for i in `echo $PORT`; do
  50. echo -e "http_port ${i}" >> ${CONF}
  51. done
  52. continue
  53. fi
  54. echo -e "${varline}" >> ${CONF}
  55. done <<< "${NEWCONF}"
  56. msg -bar
  57. print_center -azu "$(fun_trans "AGUARDE")"
  58. [[ -d "/etc/squid/" ]] && {
  59. service ssh restart > /dev/null 2>&1
  60. /etc/init.d/squid start > /dev/null 2>&1
  61. service squid restart > /dev/null 2>&1
  62. }
  63. [[ -d "/etc/squid3/" ]] && {
  64. service ssh restart > /dev/null 2>&1
  65. /etc/init.d/squid3 start > /dev/null 2>&1
  66. service squid3 restart > /dev/null 2>&1
  67. }
  68. sleep 2s
  69. tput cuu1 && tput dl1
  70. print_center -verd "$(fun_trans "PUERTOS REDEFINIDOS")"
  71. }
  72. edit_apache(){
  73. local CONF="/etc/apache2/ports.conf"
  74. local line="$(cat ${CONF}|sed -n '/Listen/='|head -1)"
  75. local NEWCONF="$(cat ${CONF}|sed "$line c ADMRufu")"
  76. let line++
  77. while [[ ! -z $(echo "$NEWCONF"|sed -n "${line}p"|grep 'Listen') ]]; do
  78. NEWCONF=$(echo "$NEWCONF"|sed "${line}d")
  79. done
  80. title "$(fun_trans "REDEFINIR PUERTOS APACHE")"
  81. msg -ne " $(fun_trans " Digite Puertos"): " && read DPORT
  82. tput cuu1 && tput dl1
  83. TTOTAL=($DPORT)
  84. for((i=0; i<${#TTOTAL[@]}; i++)); do
  85. [[ $(mportas|grep -v apache|grep -v '>'|grep -w "${TTOTAL[$i]}") = "" ]] && {
  86. echo -e "\033[1;33m Puerto Elegido:\033[1;32m ${TTOTAL[$i]} OK"
  87. PORT="$PORT ${TTOTAL[$i]}"
  88. } || {
  89. echo -e "\033[1;33m Puerto Elegido:\033[1;31m ${TTOTAL[$i]} FAIL"
  90. }
  91. done
  92. [[ -z $PORT ]] && {
  93. msg -bar
  94. print_center -verm2 "Ningun Puerto Valido"
  95. return 1
  96. }
  97. rm ${CONF}
  98. while read varline; do
  99. if [[ ! -z "$(echo "$varline"|grep 'ADMRufu')" ]]; then
  100. for i in `echo $PORT`; do
  101. echo -e "Listen ${i}" >> ${CONF}
  102. done
  103. continue
  104. fi
  105. echo -e "${varline}" >> ${CONF}
  106. done <<< "${NEWCONF}"
  107. msg -bar
  108. print_center -azu "$(fun_trans "AGUARDE")"
  109. service apache2 restart &>/dev/null
  110. sleep 2s
  111. tput cuu1 && tput dl1
  112. print_center -verd "$(fun_trans "PUERTOS REDEFINIDOS")"
  113. }
  114. edit_openvpn(){
  115. msg -azu "$(fun_trans "REDEFINIR PUERTOS OPENVPN")"
  116. msg -bar
  117. local CONF="/etc/openvpn/server.conf"
  118. local CONF2="/etc/openvpn/client-common.txt"
  119. local NEWCONF="$(cat ${CONF}|grep -v [Pp]ort)"
  120. local NEWCONF2="$(cat ${CONF2})"
  121. msg -ne "$(fun_trans "Nuevos puertos"): "
  122. read -p "" newports
  123. for PTS in `echo ${newports}`; do
  124. verify_port openvpn "${PTS}" && echo -e "\033[1;33mPort $PTS \033[1;32mOK" || {
  125. echo -e "\033[1;33mPort $PTS \033[1;31mFAIL"
  126. return 1
  127. }
  128. done
  129. rm ${CONF}
  130. while read varline; do
  131. echo -e "${varline}" >> ${CONF}
  132. if [[ ${varline} = "proto tcp" ]]; then
  133. echo -e "port ${newports}" >> ${CONF}
  134. fi
  135. done <<< "${NEWCONF}"
  136. rm ${CONF2}
  137. while read varline; do
  138. if [[ $(echo ${varline}|grep -v "remote-random"|grep "remote") ]]; then
  139. echo -e "$(echo ${varline}|cut -d' ' -f1,2) ${newports} $(echo ${varline}|cut -d' ' -f4)" >> ${CONF2}
  140. else
  141. echo -e "${varline}" >> ${CONF2}
  142. fi
  143. done <<< "${NEWCONF2}"
  144. msg -azu "$(fun_trans "AGUARDE")"
  145. service openvpn restart &>/dev/null
  146. /etc/init.d/openvpn restart &>/dev/null
  147. sleep 1s
  148. msg -bar
  149. msg -azu "$(fun_trans "PUERTOS REDEFINIDOS")"
  150. msg -bar
  151. }
  152. edit_dropbear(){
  153. title "$(fun_trans "REDEFINIR PUERTOS DROPBEAR")"
  154. msg -ne " $(fun_trans " Digite Puertos"): " && read DPORT
  155. tput cuu1 && tput dl1
  156. TTOTAL=($DPORT)
  157. for((i=0; i<${#TTOTAL[@]}; i++)); do
  158. [[ $(mportas|grep -v 'dropbear'|grep "${TTOTAL[$i]}") = "" ]] && {
  159. echo -e "\033[1;33m Puerto Elegido:\033[1;32m ${TTOTAL[$i]} OK"
  160. PORT="$PORT ${TTOTAL[$i]}"
  161. } || {
  162. echo -e "\033[1;33m Puerto Elegido:\033[1;31m ${TTOTAL[$i]} FAIL"
  163. }
  164. done
  165. [[ -z $PORT ]] && {
  166. echo -e "\033[1;31m Ningun Puerto Valida Fue Elegido\033[0m"
  167. return 1
  168. }
  169. cat <<EOF > /etc/default/dropbear
  170. NO_START=0
  171. DROPBEAR_PORT=VAR1
  172. DROPBEAR_EXTRA_ARGS="VAR"
  173. DROPBEAR_BANNER="/etc/dropbear/banner"
  174. DROPBEAR_RECEIVE_WINDOW=65536
  175. EOF
  176. n=0
  177. for i in $(echo $PORT); do
  178. p[$n]=$i
  179. let n++
  180. done
  181. sed -i "s/VAR1/${p[0]}/g" /etc/default/dropbear
  182. if [[ ! -z ${p[1]} ]]; then
  183. for (( i = 0; i < ${#p[@]}; i++ )); do
  184. [[ "$i" = "0" ]] && continue
  185. sed -i "s/VAR/-p ${p[$i]} VAR/g" /etc/default/dropbear
  186. done
  187. fi
  188. sed -i "s/VAR//g" /etc/default/dropbear
  189. msg -bar
  190. print_center -azu "$(fun_trans "AGUARDE")"
  191. service dropbear restart &>/dev/null
  192. sleep 2s
  193. tput cuu1 && tput dl1
  194. print_center -verd "$(fun_trans "PUERTOS REDEFINIDOS")"
  195. }
  196. edit_openssh(){
  197. local CONF="/etc/ssh/sshd_config"
  198. local line="$(cat ${CONF}|sed -n '/[Pp]ort/='|head -1)"
  199. local NEWCONF="$(cat ${CONF}|sed "$line c ADMRufu"|sed '/[Pp]ort/d')"
  200. title "$(fun_trans "REDEFINIR PUERTOS OPENSSH")"
  201. msg -ne " $(fun_trans " Digite Puertos"): " && read DPORT
  202. tput cuu1 && tput dl1
  203. TTOTAL=($DPORT)
  204. for((i=0; i<${#TTOTAL[@]}; i++)); do
  205. [[ $(mportas|grep -v ssh|grep -v '>'|grep -w "${TTOTAL[$i]}") = "" ]] && {
  206. echo -e "\033[1;33m Puerto Elegido:\033[1;32m ${TTOTAL[$i]} OK"
  207. PORT="$PORT ${TTOTAL[$i]}"
  208. } || {
  209. echo -e "\033[1;33m Puerto Elegido:\033[1;31m ${TTOTAL[$i]} FAIL"
  210. }
  211. done
  212. [[ -z $PORT ]] && {
  213. msg -bar
  214. print_center -verm2 "Ningun Puerto Valido"
  215. return 1
  216. }
  217. rm ${CONF}
  218. while read varline; do
  219. if [[ ! -z "$(echo "$varline"|grep 'ADMRufu')" ]]; then
  220. for i in `echo $PORT`; do
  221. echo -e "Port ${i}" >> ${CONF}
  222. done
  223. continue
  224. fi
  225. echo -e "${varline}" >> ${CONF}
  226. done <<< "${NEWCONF}"
  227. msg -bar
  228. print_center -azu "$(fun_trans "AGUARDE")"
  229. service ssh restart &>/dev/null
  230. service sshd restart &>/dev/null
  231. sleep 2s
  232. tput cuu1 && tput dl1
  233. print_center -verd "$(fun_trans "PUERTOS REDEFINIDOS")"
  234. }
  235. main_fun(){
  236. title "$(fun_trans "GESTOR DE PUERTOS")"
  237. unset newports
  238. i=0
  239. new=$(mportas|cut -d ' ' -f1|grep -E 'squid|apache|dropbear|ssh')
  240. [[ ! -z $(echo "$new"|grep squid) ]] && {
  241. let i++
  242. echo -e "$(msg -verd "[$i]") $(msg -verm2 ">") $(msg -azu "$(fun_trans "REDEFINIR PUERTOS SQUID")")"
  243. squid=$i
  244. }
  245. [[ ! -z $(echo "$new"|grep apache) ]] && {
  246. let i++
  247. echo -e "$(msg -verd "[$i]") $(msg -verm2 ">") $(msg -azu "$(fun_trans "REDEFINIR PUERTOS APACHE")")"
  248. apache=$i
  249. }
  250. #[[ ! -z $(echo "$new"|grep openvpn) ]] && {
  251. # let i++
  252. # echo -e "$(msg -verd "[$i]") $(msg -verm2 ">") $(msg -azu "$(fun_trans "REDEFINIR PUERTOS OPENVPN")")"
  253. # openvpn=$i
  254. #}
  255. [[ ! -z $(echo "$new"|grep dropbear) ]] && {
  256. let i++
  257. echo -e "$(msg -verd "[$i]") $(msg -verm2 ">") $(msg -azu "$(fun_trans "REDEFINIR PUERTOS DROPBEAR")")"
  258. dropbear=$i
  259. }
  260. [[ ! -z $(echo "$new"|grep ssh) ]] && {
  261. let i++
  262. echo -e "$(msg -verd "[$i]") $(msg -verm2 ">") $(msg -azu "$(fun_trans "REDEFINIR PUERTOS SSH")")"
  263. ssh=$i
  264. }
  265. back
  266. opcion=$(selection_fun $i)
  267. case $opcion in
  268. $squid)edit_squid;;
  269. $apache)edit_apache;;
  270. #$openvpn)edit_openvpn;;
  271. $dropbear)edit_dropbear;;
  272. $ssh)edit_openssh;;
  273. esac
  274. }
  275. main_fun
  276. enter
  277. return 1