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
Linux - NewbieThis 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
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.
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.
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.
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).
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.