LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash/Perl script - provide list of hosts to choose from and ssh to chosen host (https://www.linuxquestions.org/questions/programming-9/bash-perl-script-provide-list-of-hosts-to-choose-from-and-ssh-to-chosen-host-800394/)

loadedmind 04-06-2010 12:52 PM

Bash/Perl script - provide list of hosts to choose from and ssh to chosen host
 
Hi all. I'm not sure if this is best done in Perl or Bash. I'm thinking surely someone else has created something close to what I'm looking for. The results of the script would be that someone would kick off "linux_hosts.sh" or whatever you want to call it, then a top "folder" of options (with hosts contained within each of these top menu choices), then, based on which number corresponds to that top level, they're presented with a set of linux hosts that are relevant to that top level name. Example:

$ linux_hosts.sh

1. VMware hosts 4. Private Domain
2. ESX servers 5. Red Hat boxes
3. Public Domain 6. Debian boxes

Please choose: 4 [Enter]

You have chosen Private Domain...

Here are the linux hosts found in this category:

1. rhel555 6. deb444
2. rhel545 7. deb644
3. rhel655 8. deb549
4. rhel688 9. deb244
5. rhel299 10. deb491

Please choose host to ssh to: 8 [Enter]

You have chosen deb549. Now starting ssh session to host...

username@deb549's password:

$ ~ >

Does anyone know if such a think might already exist? What would be extra cool is if the menu prompt could also specify a username before the ssh to host prompt.

Thanks and regards,
John

rweaver 04-06-2010 01:00 PM

You could do this using 'select' and 'read' fairly easily in a bash script... here's some examples, give it a try and if you run into issues come back and post what the issues are and we'll give you a hand.

http://www.livefirelabs.com/unix_tip...3/05052003.htm
http://tldp.org/LDP/Bash-Beginners-G...ect_08_02.html

loadedmind 04-06-2010 02:12 PM

Thank you rweaver!

Ok, this is what I've managed to come up with so far:

#!/bin/bash

# Script to display listing of hosts to ssh into

PS3='Please choose from the following categories: '

select i in ESX_Hosts ESMD_Hosts Linux_Hosts SC_Hosts Quit
do
case $i in
ESX_Hosts);;
ESMD_Hosts);;
Linux_Hosts);;
SC_Hosts);;
Quit) break;;
esac
done

Where I'm lost is, for the command portion of the bash script, how to tell it to enter that category which contains a list of hosts. So, basically, the command would be, "Hey, cat out or list, with a number on the front for the user to choose, these linux hosts. I have created each of these as a filename which contains a list of hosts in them - in typical newline format. So, they choose 1 aka ESX_Hosts. What should happen, as I think of this logically, is that somehow ESX_Hosts is read and a number is placed in front of each hostname, then another prompt which says, "Please choose the number corresponding to host for ssh access: ".

Can I call the PS3 variable again or, does it matter?

Thanks ahead of time,
John

grail 04-06-2010 08:54 PM

You could store all the hosts in an array and then call that using a loop or select.

If you want a slightly fancier input (but maybe a bit more study) then you could also use dialog.
This will create an onscreen box with your choices and then pressing the associated choice moves to the next screen.
This would also allow for username input as well (although you can easily put that prior to ssh call with a read in bash too.

Hope that helps a little :)

loadedmind 04-07-2010 01:19 PM

Ok, sounds like I need to hire someone. Preciate your help.


All times are GMT -5. The time now is 06:13 AM.