简体   繁体   中英

Using Netstat -anlp within a menu system breaks my menu when I ctrl-c to stop output

I have several machines that I monitor through various means, tailing the access_log for apache2, running tcpdump, and also using.netstat. I decided to set all these commands up in a menu system to make things easier for other users and so far things work great. That is until I got to.netstat. commands like:

tcpdump-vho1mc1()
{
  echo "Starting TCP Dump on VHO1MC1 Primary"
  ssh vho1mc1-primary sudo /usr/sbin/tcpdump -i vlan654 port 50000 -vvnn -s0 -c50
}

and

apache2tail-vho1uc1()
{
  echo "Tailing access log on VHO1UC1"
  ssh vho1uc1-primary sudo tail -f /var/log/apache2/access_log
}

These break just fine when I hit Ctrl-c and jump back to the menu but when I use.netstat:

netstat-vho1uc1()
{
  while true;
  do
    export var10=$(ssh vho1uc1-primary sudo netstat -anlp | grep ":80")
    sleep 3
    echo $var10
  done
}

it breaks the menu and drops back to the command line forcing the user to restart the menu script. What I pasted above is just 1 iteration of several I've tried but none will break and go back to the menu. The command itself works fine, it's just breaking out of it when I'm done is the issue.

The menu system is as follows:

######################
#Selection parameters#
######################

case $selection in
1  ) clear ; ucnmls ; press_enter ;;
2  ) clear ; mcnmls ; press_enter ;;
3  ) clear ; uptime ; press_enter ;;
4  ) clear ; dss ; press_enter ;;
5  ) clear ; chkallsvcs ; press_enter ;;
6  ) clear ; network_menu_1 ; press_enter ;;
7  ) clear ; network_menu_2 ; press_enter ;;
8  ) clear ; network_menu_3 ; press_enter ;;
0  ) clear ; exit ;;
* ) clear ; incorrect_selection ; press_enter ;;
esac
done

network_menu_3() {
  local PS3='Please enter Selection: '
  local options=("VHO1UC1" "VHO2UC1" "VHO3UC1" "VHO1UC2" "VHO2UC2" "VHO3UC2" "Sub Menu 
  Quit")
  local opt
  select opt in "${options[@]}"
    do
    case $opt in
      "VHO1UC1")
          netstat-vho1uc1
          ;;
      "VHO2UC1")
          netstat-vho2uc1
          ;;
      "VHO3UC1")
          apache2tail-vho3uc1
          ;;
      "VHO1UC2")
          apache2tail-vho1uc2
          ;;
      "VHO2UC2")
          apache2tail-vho2uc2
          ;;
      "VHO3UC2")
          apache2tail-vho3uc2
          ;;
      "Sub Menu Quit")
          return
          ;;
      *) echo "invalid option $REPLY";;
    esac
    done
}

only options 1 and 2 are being used right now.

I tried using breaks, traps, and a few other things but it still wouldn't go back to the sub-menu. What I ended up doing is writing it so I could press any key and get it to stop. This actually worked once I put menu function .network_menu_3) at the end. Here is the code I used to do that. I didn't come up with it though. It was found at a website I can't find now. Anyway, here is the code.

netstat-vho3uc2() {

if [ -t 0 ]; then
  SAVED_STTY="`stty --save`"
  stty -echo -icanon -icrnl time 0 min 0
fi
echo "Logging into HOSTNAME (x.x.x.x)"
echo "Running Netstat..."
echo "Press any key to go back to the menu"
count=0
keypress=''
while [ "x$keypress" = "x" ]; do
  ssh HOSTNAME sudo netstat -anlp | grep ":80"|wc -l
  sleep 3
#  echo -ne $count'\r'
  keypress="`cat -v`"
done

if [ -t 0 ]; then stty "$SAVED_STTY"; fi
network_menu_3
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM