LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 07-17-2008, 10:08 AM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Menu bashed shell script for users shell


hello,

I am writing a script for users to interact with that will present a few, well defined options for usage. For instance, they will have an option to telnet to a device, ssh to a device and telnet to several other core routers. All is well with the initial usage of the menu, when the user logs in he is presented with the menu and able to interact with it. However, the problem is when they telnet, ssh, etc. they are forked out of the menu and into whatever program they executed.
Is there a way to prevent this behavior? I was thinking that it might be best to write each function into its own shell script, therefore never leaving the initial menu. Any ideas would be appreciated.

The script so far:
Code:
#!/bin/sh
#set -x
user=`whoami`;
CLEAR=`which clear`;
TELNET=`which telnet`;
MENU=/usr/local/query/menu;
SSH=`which ssh`;

_Menu ()
{
        $CLEAR;
        echo " "
        echo "        ============================================================"
        echo "                                 NS2 MENU                           "
        echo "        ============================================================"
        echo "           1. Telnet to a Device          12. CMTS Intfc Modem Count"
        echo "           2. SSH to a Device             13. Ping/Traceroute Device"
        echo "           3. Clear SSH Known Hosts       14.  "
        echo "           4. Access Core1 GSR            15.  "
        echo "           5. Access Core2 GSR            16.  "
        echo "           6. Access Core3 VXR            17.  "
        echo "           7.                             18.  "
        echo "           8.                             19.  "
        echo "           9.                             20.  "
        echo "          10.                             21.  "
        echo "          11. Access Server             22.  "
        echo "        ============================================================"
        echo "                    Select a Menu Number Option      "or"           "
        echo "                 Type  H# for Help on a Menu Option  "or"           "
        echo "                        --Q  to Quit Menu--                         "
        echo "        ============================================================"
        echo -n "Enter Menu Option ======> "
        read option;

        case "$option" in

                1)
                        _telnet;
                ;;

                2)
                        _ssh;
                ;;

                3)
                        _clearssh;
                ;;

                4)
                        _core1;
                ;;

                5)
                        _core2;
                ;;

                6)
                        _core3;
                ;;

                11)
                        _aov;
                ;;

                12)
                        _mdmcnt;
                ;;

                13)
                        _pingtrace;
                ;;

                *)
                        $CLEAR;
                        echo -e "You chose an invalid response. Please try again.\n Press any key to continue.";
                        read junk;
                        $MENU
                ;;

                esac
}

_telnet ()
{
        $CLEAR;
        echo -n "Enter IP address of device and press Return: ";
        read host;
        $TELNET $host;
}
_ssh ()
{
        $CLEAR;
        echo -n "Enter IP address of deivce and press return: ";
        read host;
        $SSH $host
}

_clearssh ()
{
        $CLEAR;
        echo -e "Know hosts has been cleared.\n Press any key to continue.";
        read junk;
        $MENU;
}

_core1 ()
{
        $CLEAR
        $TELNET <someIP>

}
_core2 ()
{
        $CLEAR
        $TELNET <someIP>
}
_core3 ()
{
        $CLEAR
        $TELNET <someIP>
}
_Menu
 
Old 07-17-2008, 10:13 AM   #2
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
I suppose one option would be to use the 'fork' command, eh?
 
Old 07-18-2008, 07:30 AM   #3
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
No one have any suggestions?
 
Old 07-18-2008, 07:50 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,409
Blog Entries: 55

Rep: Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582
Try "select"? Example: 'select selection in quit ssh telnet; do case $selection in quit) break;; ssh) ssh user@host;; *) echo $selection;; esac; done'
 
Old 07-18-2008, 08:49 AM   #5
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
I don't think i was clear enough when i wrote the initial post.

When the user logs in, if they choose option 11, they can access the server through the script with no problem. However, when they are ready to leave the server they went to through the script, it exits out completely. i.e. They lose connection to the initial server as well as the one they initially connected to.

Is there anyway to stop this behavior? Why does it do this?
 
Old 07-18-2008, 10:05 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,409
Blog Entries: 55

Rep: Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582Reputation: 3582
Scripts are traversed from start to finish, so if a) use replace the login shell with your menu and b) you're not keeping them in a continuous loop like my "select" example or point back to the main function inside a "child" function or follow a child function with calling _Menu, then the script will finish. Logging info from "set -x" debugging should show that.

Last edited by unSpawn; 07-18-2008 at 10:06 AM. Reason: condition
 
Old 02-14-2013, 06:00 AM   #7
mrmjolnir
LQ Newbie
 
Registered: Jan 2006
Posts: 1

Rep: Reputation: 0
Well, this answer comes after 5 years, but better late than never

Have you tried expect?
expect.sourceforge.net
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new users via Shell script coolfrog Linux - General 10 12-05-2010 10:47 AM
How do i switch users in a shell script? koobi Programming 8 01-05-2008 02:19 PM
shell script help for users cmontr Programming 11 11-15-2007 05:09 PM
shell script menu marsIV Programming 1 10-09-2007 08:56 PM
Shell Script: Add Users from file flobadon Programming 3 12-07-2004 03:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration