LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2016, 10:03 AM   #1
rajkamalhm
LQ Newbie
 
Registered: Apr 2016
Posts: 4

Rep: Reputation: Disabled
In a for loop that operates on multiple servers, need both the options to enter the password or to skip to next server in loop


I have a for loop which logs in to multiple servers and execute commands in those servers. Some of them asks for password. pressing ctrl+c exits the entire script. How to skip that server alone and proceed to next server. cannot use batchmode in ssh because for some servers I know the password. So basically i should have a choice of entering the password or to skip the server and proceed to the next one in the loop.
 
Old 06-08-2016, 10:25 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,689

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by rajkamalhm View Post
I have a for loop which logs in to multiple servers and execute commands in those servers. Some of them asks for password. pressing ctrl+c exits the entire script. How to skip that server alone and proceed to next server. cannot use batchmode in ssh because for some servers I know the password. So basically i should have a choice of entering the password or to skip the server and proceed to the next one in the loop.
You write your script to do what you want.

Since you don't actually post your script, tell us ANY details about your environment, what you're actually trying to accomplish, version/distro of Linux, etc., there is little we can tell you past "modify your own script". We will be happy to HELP you, so post what you've written/tried, and tell us where you're stuck.
 
1 members found this post helpful.
Old 06-08-2016, 11:02 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Actually, without seeing the script, I know of at least one solution to this problem.

Here are a couple of friendly suggestions:
- Use the internet, search for similar problems.
- Check the man page for ssh.

As for the latter, pay extra attention to the -o switch:
Code:
     -o option
             Can be used to give options in the format used in the
             configuration file.  This is useful for specifying
             options for which there is no separate command-line
             flag.  For full details of the options listed below, and
             their possible values, see ssh_config(5).
 
1 members found this post helpful.
Old 06-08-2016, 11:11 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As above, hard to help without seeing anything, but possible suggestion would be, seeing you know ahead of time which servers require passwords and those that don't, why not place all the password ones at the head of the queue and enter passwords as required and then let the rest continue as needed.
 
1 members found this post helpful.
Old 06-08-2016, 01:00 PM   #5
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by rajkamalhm View Post
I have a for loop which logs in to multiple servers and execute commands in those servers. Some of them asks for password. pressing ctrl+c exits the entire script. How to skip that server alone and proceed to next server. cannot use batchmode in ssh because for some servers I know the password. So basically i should have a choice of entering the password or to skip the server and proceed to the next one in the loop.
Show us your work.
 
Old 06-08-2016, 08:12 PM   #6
rajkamalhm
LQ Newbie
 
Registered: Apr 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Apologies for not providing enough information.

below is the script

Code:
for i in `cat serverlist.txt`
do
ssh $i "commands...."
done
it's a bash script executed in RHEL 6.x machine.

most of the servers have passwordless login enabled, but a few are requesting password. For the servers that requests password, I have it for a few servers. So, the requirement is:
If a server asks for a password then i can enter it if I know the password and the script will execute the commands in that server.
If I dont know the password I should skip that server and proceed to the next one in the loop(ctrl+c exits entirely from the script itself)

I dont know what are all the servers that will comeup in the "serverlist.txt" file because it is provided by the client.

Thanking you in advance
 
Old 06-08-2016, 08:42 PM   #7
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
What CTRL+C actually does is to send a signal (specifically SIGINT) to your script. You can have your script catch this signal and do something (or ignore it). Some details can be found here and googling for bash signal handling should prove fruitful.

BTW ssh in a loop is not a very scalable way to manage servers; you might want to look at something like Puppet, Chef, or Ansible to accomplish your goals.
 
Old 06-08-2016, 09:28 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Actually, by far the best solution to this sort of problem is to remove the requirement to "ask for a password!"

The machines in question, presumably, "trust each other." Therefore, it is reasonable to install SSH certificates on each of the SSH-clients, and to configure the SSH-servers to accept these certificates in lieu of password authentication. (Note that this arrangement is much stronger than "passwords" ever could be ... leading to the very serious suggestion that you might wish to remove 'passwords' as an alternative option!)

The servers now introduce themselves to one another by presenting (through the magic of an "ssh-agent") a truly-unique digital credential. (And, I suggest ..., this is now the only form of identification that will do.) Password-prompts entirely cease to be an issue.
 
  


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
Please help the this script syntax not loop to multiple servers dotran Linux - Newbie 2 10-18-2013 07:38 PM
bash script does not enter loop bluethundr Linux - Newbie 3 07-29-2012 06:31 AM
C++, loop problem, never seems to enter it WetFroggy Programming 9 10-20-2009 02:17 PM
python exit loop by 'enter' sycamorex Programming 7 08-29-2008 09:44 PM
command to skip enter current password ssy68 Linux - Newbie 6 02-15-2008 12:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:07 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
Open Source Consulting | Domain Registration