0

I have a script that executes same commands on multiple servers via ssh .

spawn ssh [lindex $argv 0]
send "sudo /usr/sbin/useradd ytkatchen"
expect "password:"
send "Unix\@999\r"

My password is not same/alike for all servers . I have 2 different passwords for a long list of servers .

My requirement is the script should make ssh connection and use correct valid password to keep the job going .

It should not execute if the password is wrong rather it should try other password and keep going.

2 Answers 2

1

The easier way would be to use ssh keys instead of passwords.

You can generate a ssh key pair per server, and use them to connect automatically without using passwords.

It's also better for security as you don't need to prompt or store any password with your script.

2
  • I already has passwordless logins to all machines but after logging to machine the command s needudo /usr/sbin/useradd ytkatchen is executed that needs password .
    – parpande
    Mar 18, 2016 at 13:36
  • You can try things like this : echo mypassword | sudo -S command. The -S to make sudo listen to stdin.
    – nex84
    Mar 18, 2016 at 13:53
0

You can setup passwordless sudo on your servers so that you don't have to worry about entering the passwords. A quick guide:

$ sudo visudo

Add a new line to the end of file:

EditThisToYourUsername ALL=(ALL) NOPASSWD:ALL

Notice: it's a tab character between Username and ALL.

Save the file (Escape, :x, Enter).

Another solution is you can listen to the exit code of your sudo command to know if your entered password is correct or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .