-1

I have a users.txt with the following content:

ip1 user1 password1
ip2 user2 password2
....
ipN userN passwordN

I want to create one user on each of these servers. I want to use this script on each server:

user_login="some_user7"
password="blablabla"

sudo adduser $user_login --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password

echo -e "$password\n$password\n" | sudo passwd $user_login

I don't understand, how correctly connect on each server and execute this script.

P.S. I don't want to use SSH-public keys. I want to use just logins and passwords.

Denis
  • 145
  • 1
  • 5
  • By "logins and passwords" do you mean common SSH access, with password authentication? – Damiano Verzulli Dec 20 '14 at 21:44
  • @DamianoVerzulli, yep, you're right. Something like that: http://stackoverflow.com/a/6239313/1756750 I just want to add pass to this. – Denis Dec 20 '14 at 21:45
  • Does your "users.txt" contains SSH credentials for servers you need to connect to? Or for each of N servers, it contains the username/password to be assigned to the account you need to create? – Damiano Verzulli Dec 20 '14 at 21:54
  • @DamianoVerzulli, each line like this: `162.221.191.111 admin password1`. – Denis Dec 20 '14 at 22:01
  • Are you able to rely on PERL and related modules, or common unix tools like "expect" to solve your problem? – Damiano Verzulli Dec 20 '14 at 22:12
  • Definitely not the answer to your question, but the most common solution to avoid creating and maintaining accounts on many servers is by implementing something like an LDAP directory or IPA. – HBruijn Dec 21 '14 at 06:24
  • @HBruijn, yes, you are right. But in some case, LDAP is more powerful tool, that I need... – Denis Dec 21 '14 at 07:34

1 Answers1

3

To execute a script on a server using ssh is simple, something like:

cat script.sh | ssh someserver

However, you probably do not want to execute the same script on every server. You will need another script to read your users.txt and from that file create script files for each line in users.txt before calling ssh with a newly created script as input. You could use sed to replace user and password in some template file.

Or, you could use awk to do all the work:

awk '{system("ssh \""$1 " sudo adduser " $2 " --disable-password ; echo -e \\\""$3"\\n"$3"\\n\\\" | sudo passwd "$2"\"")}' < users.txt