1

I'm trying to build a raspberry pi cluster with 4 raspberry pis. I'm writing a script to add new users to the system such that the user can be added on all the 4 Pis without having to enter it multiple times. The command I'm using is adduser and I'm adding users on to all the 4 Pis using ssh. This is how that line in the script looks:

echo `ssh pi@10.10.10.13 "sudo adduser --shell /bin/bash user1"`

This is the line I'm using to add a new user. However, the command after this is where I need to change the password. If I use

sudo passwd user1

in the script, for each Pi, it prompts the user for a password 4 times. I don't want this to happen. Is there any way to get the user to enter the password once and store it in a variable and supply this to the passwd command?

Ahmis
  • 43
  • 6
  • Also, why `echo` with backticks instead of just the command? – Biffen May 20 '14 at 19:03
  • I finally managed to solve it by using useradd instead of adduser and providing an option for the user to enter a password and encrypting it using the crypt() function in perl. These questions were helpful: http://stackoverflow.com/questions/1020534/useradd-using-crypt-password-generation http://stackoverflow.com/questions/714915/using-the-passwd-command-from-within-a-shell-script – Ahmis May 22 '14 at 18:42

2 Answers2

0

Here's a way to execute a number of commands through SSH with sudo on the remote side (in case logging in as root is not an option):

#!/bin/bash

ssh user@host 'sudo bash -es' <<EOF
command 1
command 2
EOF

-e to "fail on failure" (optional).

-s to read from STDIN.

(I gave it a quick try and it seems to work.)

Edit

I tried with -x (I like the feedback) but it made the output garbled, probably because STDIN and STDERR were "out of sync" (could be "solved" with 2>&1).

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • Thank you for your answer. I'm not sure this will help me though. Assuming command 1 is adduser and command 2 is passwd, the user will still get prompted for the new password every time these three lines execute (which in my case is 4 times). – Ahmis May 20 '14 at 19:24
  • @user3657965 Oh, I must've misunderstood then. – Biffen May 20 '14 at 19:26
0

Assuming there's still no "useradd" on Pi, I'd suggest you start looking into adduser's command line parameters?

adduser <username> -p <password> ...
metaphor_set
  • 136
  • 3
  • Actually, there is useradd command also. I just used adduser because I'm familiar with that command. There is --disable-password option in adduser but I think that only disables the password and allows you to log in via ssh or something. I don't really understand how it works. The useradd command has a --password option by which I can set a default password. I think I'll try that for now. Thanks! – Ahmis May 20 '14 at 19:37
  • @user3657965: I only know of --disabled-password which is useful if the Server's admin doesn't want to know a user's password. However, this still requires some action because the user should then set the password on the account using passwd. If you really want to automate the process of user creation I'd suggest useradd with the --passwd option. I do this quite often when preparing preconfigured Samba servers... – metaphor_set May 20 '14 at 21:48
  • Thanks. I'm trying to do that now but I'm having a huge problem encrypting the password. Having never dealt with encryption before, I'm a little lost and the crypt() function apparently works for C? Any advice on how to go about this? – Ahmis May 20 '14 at 22:35