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

7
  • By "logins and passwords" do you mean common SSH access, with password authentication? Dec 20, 2014 at 21:44
  • @DamianoVerzulli, yep, you're right. Something like that: stackoverflow.com/a/6239313/1756750 I just want to add pass to this.
    – Denis
    Dec 20, 2014 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? Dec 20, 2014 at 21:54
  • @DamianoVerzulli, each line like this: 162.221.191.111 admin password1.
    – Denis
    Dec 20, 2014 at 22:01
  • Are you able to rely on PERL and related modules, or common unix tools like "expect" to solve your problem? Dec 20, 2014 at 22:12

1 Answer 1

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

You must log in to answer this question.

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