#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0

    Menu Driven UNIX Admin


    Hiya there all, I am newbie to UNIX.

    How would i write a script in a bash shell that can:
    a) reboot the box
    b) prefix a reboot with a suitable warning and pause
    c) ping an IP address specified by the user and report on the response
    d) kill a processes as specified by the user
    e) add and delete a user

    Hope someone can help or guide me to some good resources.

    Thanks for your time.

    Kind Regards
    Ad4m88
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Kalamazoo, MI
    Posts
    38
    Rep Power
    21
    Originally Posted by ad4m88
    Hiya there all, I am newbie to UNIX.

    How would i write a script in a bash shell that can:
    a) reboot the box
    b) prefix a reboot with a suitable warning and pause
    c) ping an IP address specified by the user and report on the response
    d) kill a processes as specified by the user
    e) add and delete a user

    Hope someone can help or guide me to some good resources.

    Thanks for your time.

    Kind Regards
    Ad4m88
    You can do most of those with a single bash command, no need to write a script to do it... If you want to put it into a script for the sake of a cronjob, simply put the command in a text file with #!/bin/bash on the first line.

    A) command is 'reboot'
    B) I think there is some kind of flag you can pass to the reboot command to do it after a certain amount of time. ('man reboot' in console)
    C)'ping ipaddress'
    D)kill -9 PID
    E) 'deluser' and 'adduser' should be pathed to work if you're logged in as root for deleting and adding users, on most nix systems.

    Sorry, didn't read the menu driven part in the topic. If it really needs to be menu driven, the bash script wouldn't be too hard to write. Start off with echo'ing different numerical values to screen and the corresponding task they do. Use if/else to parse through the different numerical values that the script prompts for and have it execute the command.

    Could be done easily this way as well:
    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-9.html#ss9.1
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0
    Thanks a lot for your help.

    Hope not asking for too much from you, but in a script, how could I do it so the user can for example enter an IP address, and then it pings that IP address, or the user states he wants to kill a certain process and that process is killed?

    I have done most of the menu driven script, where the user simply selects [1-7] to do different things, e.g. memory usage, date, cal etc.... but can’t seem to do for when a user can enter (as above, maybe an IP address) then the script runs.

    Hope you understand.

    Thanks once again for helping earlier.

    Best Regards
    P.S. I tried sending you a pm, but couldn’t for some reason, to show you my script so far...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Kalamazoo, MI
    Posts
    38
    Rep Power
    21
    Originally Posted by ad4m88
    Thanks a lot for your help.

    Hope not asking for too much from you, but in a script, how could I do it so the user can for example enter an IP address, and then it pings that IP address, or the user states he wants to kill a certain process and that process is killed?

    I have done most of the menu driven script, where the user simply selects [1-7] to do different things, e.g. memory usage, date, cal etc.... but can’t seem to do for when a user can enter (as above, maybe an IP address) then the script runs.

    Hope you understand.

    Thanks once again for helping earlier.

    Best Regards
    P.S. I tried sending you a pm, but couldn’t for some reason, to show you my script so far...

    echo "Enter IP To Ping:"
    read IP
    ping $IP

    Same idea for killing a process:

    echo "Enter PID:"
    read THEPID
    kill -9 $THEPID
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0
    Thanks alot mate ... you really know your stuff.

    How could i check a user exists on the system then change their password? And provide a summary of details for a user such as : the number of files in their
    home directory, details of when they last logged on, total disk space used by the user, number of processes currently running, details of the groups they belong to etc ..

    Hope you don't mind.

    Thanks alot for your help anywayz.

    Regards
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0
    How would i ping a list of host addresses which are in a file, without typing each one.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Kalamazoo, MI
    Posts
    38
    Rep Power
    21
    Originally Posted by ad4m88
    How would i ping a list of host addresses which are in a file, without typing each one.
    Took me a couple bouts, but this should work: format for hostsfile is a plaintext doc with ip's or hostnames seperated by spaces.

    msgln=`cat hostsfile |strings |awk '{print NF}'`
    let msgln=msgln+1
    COUNTER=1
    while [ $COUNTER -lt $msgln ]; do
    ping `cat hostsfile |strings |awk '{print $'$COUNTER'}'`
    let COUNTER=COUNTER+1
    done


    Originally Posted by ad4m88
    Thanks alot mate ... you really know your stuff.

    How could i check a user exists on the system then change their password? And provide a summary of details for a user such as : the number of files in their
    home directory, details of when they last logged on, total disk space used by the user, number of processes currently running, details of the groups they belong to etc ..

    Hope you don't mind.

    Thanks alot for your help anywayz.

    Regards
    Sounds like I'm starting to do your job for you

    I'd suggest looking into these aspects for actually writing it:
    finger/grep/passwd

    It shouldn't be too hard to piece something together, there are a lot of good bash docs on google.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0
    Sorry bout that ... but thnxs alot for your help. Really appreciate it.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0
    Sorry ... but one last question.

    You know when I asked in the first post how to have a script that can do an automated IPL function (how to reboot the box). You state ‘reboot’ would do the trick.

    How would I go about doing it, for example if the user states from a menu script that I would like to reboot now. The problem is when I do the ‘reboot’ command I get a message (obviously) because I am not the super user. But I do have another gem network in which I have root superuser access.

    So how would I from the below script which I need to write whilst I am not in super user mode do the ‘reboot’ command (and add/delete user command) from the normal user mode.

    Is there a way of having, for example when the user chooses option 1, then it states enter the other gem network root username and password and then be able to do the ‘reboot’ (& adduser) command?

    I hope I have not made it difficult to understand for you.

    P.S. I have cut out alot of the code so it wasier for you to see.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Kalamazoo, MI
    Posts
    38
    Rep Power
    21
    Originally Posted by ad4m88
    Sorry ... but one last question.

    You know when I asked in the first post how to have a script that can do an automated IPL function (how to reboot the box). You state ‘reboot’ would do the trick.

    How would I go about doing it, for example if the user states from a menu script that I would like to reboot now. The problem is when I do the ‘reboot’ command I get a message (obviously) because I am not the super user. But I do have another gem network in which I have root superuser access.

    So how would I from the below script which I need to write whilst I am not in super user mode do the ‘reboot’ command (and add/delete user command) from the normal user mode.

    Is there a way of having, for example when the user chooses option 1, then it states enter the other gem network root username and password and then be able to do the ‘reboot’ (& adduser) command?

    I hope I have not made it difficult to understand for you.

    Code:
    echo "-------------------------------------"
    echo " UNIX Menu Driven Administration"
    echo "-------------------------------------"
    echo "1. Automated IPL Function"
    echo "2. Add User"
    echo "3. Network Check"
    echo "4. System Information"
    echo "====================================="
    echo "Make Your Selection: "
            read menunumber
    
             if [ $menunumber -eq 3 ]
    
                    then
                            echo " NETWORK CHECK "
                            echo "-------------------------------------"
                            echo " NETWORK CHECK ">>report.txt
                            echo "-------------------------------------">>report.txt
    sleep 1
                            echo " Your IP Address is: $IPAddress"
                            echo "-------------------------------------"
                            echo " Your IP Address is: $IPAddress">>report.txt 
                            echo "-------------------------------------">>report.txt
    sleep 1 
                            echo " Enter IP To Ping:"
                            read IP
                            /bin/ping $IP  -w 2 > /dev/null 
                            if [ $? = 0 ] ; then 
                                            echo "-------------------------------------"  
                                            echo "Machine $IP is on the network"
                                            echo "Machine $IP is on the network">>report.txt
                            else 
                                            echo "-------------------------------------"
                                            echo "Machine $IP is not on the network"
                                            echo "Machine $IP is not on the network">>report.txt
                            fi 
                            echo "-------------------------------------"
                            echo "-------------------------------------">>report.txt
    P.S. I have cut out alot of the code so it wasier for you to see.
    either run the script as root or "sudo reboot" or "su -c reboot" if you have the root pw.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2008
    Location
    Manchester, UK
    Posts
    7
    Rep Power
    0
    I cant run it on root whilst in my current script, but i have admin access to another gem network. However this is not too secure and therefore it is not wise to run and save the full script in there.

    Is there a way of having, when the user chooses option 1, then it states enter the other gem network root username and password and then be able to do the ‘reboot’ (& adduser) command from there. But once reboot is done i can go back to the network i am working on.

    P.S. sudo reboot requires the root password for the network i am currently working on, but i dont have a pw for this. (Hope not confused you)

IMN logo majestic logo threadwatch logo seochat tools logo