Interacitve script for Healthcheckup of nodes

Hi
This is my first post to this site.:smiley:
I want to develop a script with following steps:
1.Script1,2,3 placed at particular path on local server
2.ask for an IP and will directly pick up the correct script matching to the IP (IP could be defined in same script or a new script could be called).
3.By using ssh and scp and execute on the destination server.
4.send the output of script back to local server where all scripts are lying.

Waiting for response...:slight_smile:
:smiley:

As far as I'm concerned, unless you demonstrate that you have put forth some effort to solve your own problem, you can continue to wait.

We are here to help, but we are not a free, script-writing service.

Also, if this is homework, there is a dedicated forum with additional requirements. Read the FAQ.

Regards,
Alister

1 Like

Code:

#!/bin/bash
 
# clear the screen
tput clear
 
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
 
# Set a foreground colour using ANSI escape
tput setaf 3
echo "HealthCheckup"
tput sgr0
 
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
 
tput cup 7 15
echo "1. SDP"
 
tput cup 8 15
echo "2. CCN"
 
tput cup 9 15
echo "3. AIR"
 
tput cup 10 15
echo "4. EMM"
 
# Set bold mode 
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
 
tput clear
tput sgr0
tput rc
scp /your/script/path/monitor.sh user@node1-IP-address:/tmp/. ;
ssh user@node1-IP-address -t "sudo sh /tmp/monitor.sh > /tmp/node1.txt" ;
scp user@node1-IP-address:/tmp/node1.txt /path/of/your/local/server/. ;
ssh user@node1-IP-address -t "rm -rf /tmp/monitor.sh"

The problem is that i am in search of a command which could corelate the option 1,2,3 with the script 1,2,3 like as soon as i put option1 it should pick correct script and log on to the destination server with the correct script.

So you need to know how to make a choice from a selection?

The case statement may be of use:-

read -p "Give me a number" choice
case $choice in
   1) echo "You picked option 1"
      echo This is another statement for option 1"     ;;
   2) echo "You chose the second"                      ;;
   3) echo "The last valid option"                     ;;
   *) echo "Choice $choice is not valid                ;;
esac

Note the end-of-section ;; and the esac to end the statement.

Does that do what you are after?

Robin

I always wonder how people can know 'so much' about tput, but lack to know about select and case statements.
Actualy, its not a selection yet, as the user could write just anything.

Just as an addition to Robin, a select statement which uses a case statement.

select CHOICE in Back SDP CCN AIR EMM
do	case $CHOICE in
	Back)	break				;;
	SDP)	echo "Selected $CHOICE"		;;
	CCN)	echo "Do stuff for $CHOICE"	;;
	AIR)	echo "Love is in the $CHOICE"	;;
	EMM)	echo "$CHOICE what?"		;;
	*)	echo "$CHOICE invalid."		;;
	esac
done

Hope this helps
Simon

NOTE: This might not present the 'options' that nice as with tput, but it also works well with diffrent terminal-window screensizes.

You need some sort of exit from the select loop. Either a break to step on further in the script if that is applicable, or an exit or return to terminate the script if that is the preference.

#!/bin/ksh
# What does select do?

echo "Here 1"
select CHOICE in Back SDP CCN AIR EMM QUIT
do      case $CHOICE in
        Back)   break                           ;;
        SDP)    echo "Selected $CHOICE"         ;;
        CCN)    echo "Do stuff for $CHOICE"     ;;
        AIR)    echo "Love is in the $CHOICE"   ;;
        EMM)    echo "$CHOICE what?"            ;;
        QUIT)   echo "$CHOICE chosen"
                return                          ;;
        *)      echo "$CHOICE invalid."         ;;
        esac
done
echo "Here 2"

Hi

What i want to do is that when i select option1, it picks up script1 accordingly from another path and then scp to destination server?

In between selecting the correct script and moving to other server, it will prompt for IP address.

Just post what you attempt.
Place your code instead of the echo "string" commands.

#!/bin/bash
echo "Enter the opt"
read opt
case $opt in
[1]) echo "SDP" ;;
break;
echo "Enter the IP:"
read IP
scp /tmp/test/SDP.sh sdpuser@$IP:/tmp/. ;
ssh sdpuser@$IP -t "sudo sh /tmp/SDP.sh > /tmp/sdp1.txt" ;
scp sdpuser@$IP:/tmp/sdp1.txt /tmp/. ;
ssh sdpuser@$IP 'rm /tmp/SDP.sh'
[2]) echo "AIR" ;;
[3]) echo "EMM" ;;
esac

I am unable to echo SDP and input IP at the same time.
is it not allowed in case statements?
Pls tel if any other option can be used

---------- Post updated 05-10-14 at 05:04 AM ---------- Previous update was 05-09-14 at 01:46 PM ----------

ssh hostname@IP exec /tmp/SDP.sh > /tmp/sdp1.txt ;

What is wrong with this command?
It is showing no error but sdp1.txt is not getting created or even if it is created it is a blank file

Given the situation you mention before, i'd go save that data (servername, url, local_script_location, remote_script_location,user, (password?)) either in a csv file or
as arrays directly inside the script.

So you then could let the user select among the "${servername[@]}" array, count out its index number, and read the other arrays index number into variables, so you can then re-use the actual 'work-code' one time for all the 'diffrent' servers.

Also, you could 'connect' the the ssh commands so you dont have to call it twice, thought that might be inconvienient.

ssh sdpuser@$IP -t  << EOF
sudo sh /tmp/SDP.sh > /tmp/sdp1.txt
rm /tmp/SDP.sh
EOF

However, would more imagine you want the txt-file removed, and leave the script there for reuse, so the above example would be un-usable?
Why you must run it as sudo (does that even work remote?) and invoke sh to execute an sh script which should be executeable anyway, is something i wonder about.
And this is what i would consider the cause for the either empty or non existing file...

Hope this helps and good night

I must admit I'm a bit confused by this bit:-

case $opt in
[1]) echo "SDP" ;;
break;

I would drop the square brackets, unless you are planning to use an expression, but I think you have a structural error in the code. From what I understand, the actions taken when a case statement finds a match (in this case $opt is 1 ) is ended by a ;; , so I don't see how the rest of that block of code runs, indeed I would expect a syntax error of some sort, unless perhaps the error is missed because the break steps you out somehow.

I get this:-

# ./case_test
Enter the opt
1
./case_test: line 6: syntax error near unexpected token `;'
./case_test: line 6: `break;'
# ./case_test
Enter the opt
2
./case_test: line 6: syntax error near unexpected token `;'
./case_test: line 6: `break;'
# 

May I suggest that you change it like this:-

#!/bin/bash
echo "Enter the opt"
read opt
case $opt in
   1) echo "SDP"                                                # Note that ;; is removed
                                                                # Note that break; is removed
echo "Enter the IP:"
read IP
echo scp /tmp/test/SDP.sh sdpuser@$IP:/tmp/.                    # ; removed
echo ssh sdpuser@$IP -t "sudo sh /tmp/SDP.sh > /tmp/sdp1.txt"   # ; removed
echo scp sdpuser@$IP:/tmp/sdp1.txt /tmp/.                       # ; removed
echo ssh sdpuser@$IP 'rm /tmp/SDP.sh'    ;;                     # End of case section here
[2]) echo "AIR" ;;
[3]) echo "EMM" ;;
esac

I now get the following output:-

# ./case_test  
Enter the opt
1
SDP
Enter the IP:
123.456.789.abc
scp /tmp/test/SDP.sh sdpuser@123.456.789.abc:/tmp/.
ssh sdpuser@123.456.789.abc -t sudo sh /tmp/SDP.sh > /tmp/sdp1.txt
scp sdpuser@123.456.789.abc:/tmp/sdp1.txt /tmp/.
ssh sdpuser@123.456.789.abc rm /tmp/SDP.sh
# ./case_test
Enter the opt
2
AIR
# 

Obviously I didn't want to action the later commands, so they're just displays to show them. You will want some validity checking on the IP address too.. Mine is illegal in so many ways.

I hope that this helps,
Robin