BASH   58

tunnel bash

Guest on 21st April 2022 01:31:47 AM

  1. #!/bin/bash
  2.  
  3. DEFAULTPORTS=(80 22)
  4. IFCONF=/sbin/ifconfig
  5. OUTPUTBLOCK=4
  6. SCREENNAME=tunneler
  7.  
  8. if [ "$#" -lt 4 ]; then
  9. cat << EOF
  10.  
  11. Help SSH tunnel creation from localhost
  12. Usage: $0 -h <user@remotehost> -p <first free port at remotehost> [-l <space separated list of forwarded local ports>] [-i <space separated list of forwarded local interfaces>] [-f <flags>]
  13. Flags:
  14.     s - create sessions inside screen
  15. Example: $0 -h -p 8081 -l 80 -f s
  16. Dependencies: ifconfig, whiptail, ssh, screen
  17.  
  18. EOF
  19. exit
  20. fi
  21.  
  22. declare -A clargs
  23. while [[ $# > 1 ]]; do
  24.     key="$1"
  25.     key=${key:1}
  26.     clargs["$key"]="$2"
  27.     shift # past argument or value
  28. done
  29.  
  30. flags=""
  31. if [ ${clargs[f]+1} ]; then
  32.     flags="${clargs[f]}"
  33. fi
  34.  
  35. ifaces=()
  36. declare -A ips
  37. while read ifacerow; do
  38.     IFS=' ' read -a ifacepieces <<< "${ifacerow}"
  39.     iprow=$($IFCONF "${ifacepieces[0]}" | grep 'inet addr:' )
  40.     if [ ! -z "$iprow" ]; then
  41.         ifaces=("${ifaces[@]}" "${ifacepieces[0]}")
  42.         IFS=': ' read -a ippieces <<< "$iprow"
  43.         ips["${ifacepieces[0]}"]="${ippieces[2]}"
  44.     fi
  45. done < <($IFCONF | grep 'Link encap:Ethernet')
  46.  
  47. if [ ${#ifaces[@]} -lt 1 ]; then
  48.     echo "Localhost isn't connected to internet!"
  49.     exit
  50. fi
  51.  
  52. whiptailresult=()
  53. function whiptailer()
  54. {
  55.     declare -a argAry=("${!1}")
  56.     choose=""
  57.     for i in "${!argAry[@]}"; do
  58.         choose="$choose ${argAry[$i]} on"
  59.     done
  60.     txt="$2"
  61.     height=$((7 + ${#argAry[@]}))
  62.     length=$((4 + ${#txt}))
  63.     chosen=$(whiptail --checklist "$txt" $height $length ${#argAry[@]} $choose --noitem 3>&1 1>&2 2>&3)
  64.     if [ ! -z "$chosen" ]; then
  65.         argAry=()
  66.         IFS=' "' read -a pieces <<< "${chosen}"
  67.         for i in "${!pieces[@]}"; do
  68.             if [ ! -z ${pieces[$i]} ]; then
  69.                 argAry=("${argAry[@]}" "${pieces[$i]}")
  70.             fi
  71.         done
  72.     fi
  73.     whiptailresult=("${argAry[@]}")
  74. }
  75.  
  76. if [ ${clargs[i]+1} ]; then
  77.     IFS=' ' read -a ifaces <<< "${clargs[i]}"
  78. else
  79.     if [ ${#ifaces[@]} -gt 1 ]; then
  80.         whiptailer ifaces[@] "Choose local interfaces"
  81.         ifaces=("${whiptailresult[@]}")
  82.     fi
  83. fi
  84.  
  85. if [ ${clargs[l]+1} ]; then
  86.     IFS=' ' read -a DEFAULTPORTS <<< "${clargs[l]}"
  87. else
  88.     whiptailer DEFAULTPORTS[@] "Choose local ports which will be forwarded"
  89.     DEFAULTPORTS=("${whiptailresult[@]}")
  90. fi
  91.  
  92. port=${clargs[p]}
  93. output=()
  94. tabno=0
  95. screencmds=("screen -S ${SCREENNAME} -X quit")
  96. for i in "${!ifaces[@]}"; do
  97.     for j in "${!DEFAULTPORTS[@]}"; do
  98.         iface="${ifaces[$i]}"
  99.         output=("${output[@]}" "##### ${port} ############################################################")
  100.         output=("${output[@]}" "ssh ${clargs[h]} -R ${port}:${ips[${ifaces[$i]}]}:${DEFAULTPORTS[$j]}")
  101.         output=("${output[@]}" "# log in and paste to command line: ")
  102.         output=("${output[@]}" "while true ; do d=\$(date '+%Y%m%d %H%M ${ifaces[$i]}:${DEFAULTPORTS[$j]}>${clargs[h]}:${port}') ;  echo -en \"\r\$d\"; sleep 60 ; done")
  103.         screencmd="$0 -h ${clargs[h]} -p ${port} -l ${DEFAULTPORTS[$j]} -i ${ifaces[$i]} -f S"
  104.         if [ "$tabno" -lt 1 ]; then
  105.             screencmds=("${screencmds[@]}" "screen -AdmS ${SCREENNAME} -t tab0 ${screencmd}")
  106.         else
  107.             screencmds=("${screencmds[@]}" "screen -S ${SCREENNAME} -X screen -t tab${tabno} ${screencmd}")
  108.         fi
  109.         port=$((1 + ${port}))
  110.         tabno=$((1 + ${tabno}))
  111.     done
  112. done
  113. screencmds=("${screencmds[@]}" "screen -r ${SCREENNAME}")
  114. simple_output=true
  115.  
  116. if [[ $flags == *"s"* ]]; then
  117.     for i in "${!screencmds[@]}"; do
  118.         eval ${screencmds[$i]}
  119.     done
  120.     simple_output=false
  121. fi
  122.  
  123. if [[ $flags == *"S"* ]]; then
  124.     echo ${output[2]}
  125.     echo ${output[3]}
  126.     eval ${output[1]}
  127.     simple_output=false
  128. fi
  129.  
  130.  
  131. if [ "$simple_output" = true ] ; then
  132.     if [ ${#output[@]} -gt $OUTPUTBLOCK ]; then
  133.         echo ""
  134.         echo "----- COPY COMMANDS: BEGIN --------------------------------------------"
  135.         for i in "${!output[@]}"; do
  136.             if [ $i -lt $OUTPUTBLOCK ]; then
  137.                 continue
  138.             fi
  139.             echo ${output[$i]}
  140.         done
  141.         echo "----- COPY COMMANDS: END ----------------------------------------------"
  142.     fi
  143.     echo ""
  144.     echo ${output[2]}
  145.     echo ${output[3]}
  146.     eval ${output[1]}
  147. fi

Raw Paste


Login or Register to edit or fork this paste. It's free.