LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   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 (https://www.linuxquestions.org/questions/linux-newbie-8/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-4175581758/)

rajkamalhm 06-08-2016 10:03 AM

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.

TB0ne 06-08-2016 10:25 AM

Quote:

Originally Posted by rajkamalhm (Post 5557762)
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.

HMW 06-08-2016 11:02 AM

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).


grail 06-08-2016 11:11 AM

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.

Habitual 06-08-2016 01:00 PM

Quote:

Originally Posted by rajkamalhm (Post 5557762)
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.

rajkamalhm 06-08-2016 08:12 PM

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

btmiller 06-08-2016 08:42 PM

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.

sundialsvcs 06-08-2016 09:28 PM

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.


All times are GMT -5. The time now is 11:24 PM.