Bash/Perl script - provide list of hosts to choose from and ssh to chosen host
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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
Last edited by loadedmind; 04-06-2010 at 02:25 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.