0

Is there a way to try multiple passwords when using sshpass command? I have a txt file named hosts.txt listing multiple system IPaddresses and each system uses different passwords (for example - 'mypasswd', 'newpasswd, nicepasswd'). The script reads the hosts.txt file and execute a set of commands on each system. Since I don't know which system uses which of these given passwords, i wanted to try all these set along with sshpass command and execute the script with the password that works.. Is that possible?

#!/bin/bash

while read host; do
  sshpass -p 'mypasswd' ssh -o StrictHostKeyChecking=no -n root@$host 'ls;pwd;useradd test'
done < hosts.txt
jacknad
  • 13,483
  • 40
  • 124
  • 194
user3331975
  • 2,647
  • 7
  • 28
  • 30

1 Answers1

1

Instead of trying to get password based authentication, isn't it an option to setup key based auth? You can then either add your one public key to ll systems or optionally generate different ones and use the -i keyfile option or create an entry in the ssh configuration file as below.

Host a
  IdentityFile /home/user/.ssh/host-a-key
fejese
  • 4,601
  • 4
  • 29
  • 36
  • can I generate the key and push to all systems using the same script? Sorry, I am not familiar with ssh-keygen.. how should I modify the script to make this happen? Thanks much. – user3331975 Mar 20 '14 at 05:36
  • you need to generate a public/private key pair with ssh-keygen. It's good to protect the key with a password but if you going to use it for non interactive stuff then you probably don't want a password on it. So just run `ssh-keygen` and follow instructions. After that, you need to add the public key (id_rsa.pub by default) to the users' `~/.ssh/authorized_keys` file on the target machines. From that on you can ssh in securely without password. – fejese Mar 20 '14 at 11:13