LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2010, 12:52 PM   #1
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302
Blog Entries: 4

Rep: Reputation: Disabled
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
 
Old 04-06-2010, 01:00 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
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
 
Old 04-06-2010, 02:12 PM   #3
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302

Original Poster
Blog Entries: 4

Rep: Reputation: Disabled
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

Last edited by loadedmind; 04-06-2010 at 02:25 PM.
 
Old 04-06-2010, 08:54 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 04-07-2010, 01:19 PM   #5
loadedmind
Member
 
Registered: Sep 2003
Location: Texas
Distribution: Red Hat/CentOS
Posts: 302

Original Poster
Blog Entries: 4

Rep: Reputation: Disabled
Ok, sounds like I need to hire someone. Preciate your help.
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Can't SSH to some remove hosts: Connection Closed by Remote Host kvarkki Linux - Server 5 02-18-2010 06:44 AM
SSH: Delete an entry from the list of known hosts shaunak Linux - Newbie 1 09-16-2009 10:58 AM
perl script to choose top10 directiries! jimmyjiang Programming 27 05-17-2009 07:47 PM
You have chosen to open: installer.pl which is a perl Script? cucolin@ Linux - Server 3 03-27-2007 12:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 PM.

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
Open Source Consulting | Domain Registration