2

I am trying to automate my server provisioning process using chef. Since I don't want to run chef as root, I need a chef/deployer user. But I don't want to create this user manually. Instead, I want to automate this step. So I took a shot at scripting it but ran into an issue:

The problem is that if I run

>ssh root@123.345.345.567 '/bin/bash -e' < ./add_user.sh

where add_user contains

//..if the username doesnt exist already
adduser $USERNAME --gecos ''

I never see the output or the prompts of the command.

  1. Is there a way to run interactive commands in this way?
  2. Is there a better way to add users in an automated fashion?
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
udit
  • 2,745
  • 3
  • 33
  • 44

2 Answers2

2

Try this:

ssh -t root@<ipaddress> adduser $USERNAME --gecos

Not sure why you have a $ in the IP address in your original example - that's likely to cause ssh to fail to connect, but since you didn't indicate that sort of failure, I'm assuming that's just a typo.

Since add_user.sh is just a simple command, there's no need for the added complexity of explicitly running bash or the redirection, just run the adduser command via ssh.

And lastly, since $USERNAME is likely defined on the local end, and not on the remote end, even if you could get your original command to "do what you said", you'd end up running adduser --gecos on the remote end, which isn't what you intended.

twalberg
  • 59,951
  • 11
  • 89
  • 84
  • I removed the $ from the IP (typo). There is in fact a check for the pre-existence of the user in the script. That's the only reason why its still a script. – udit Nov 02 '12 at 21:52
0

Try using :

ssh -t root@$123.345.345.567 '/bin/bash -e' < ./add_user.sh

instead.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • I tried this and got: >> Pseudo-terminal will not be allocated because stdin is not a terminal. So I looked up the interwebz and found that -t -t should force it. But that just prints the contents of the shell script to the terminal console. – udit Nov 02 '12 at 21:04
  • When I remove the "-e" with the "-t -t", the script actually runs but doesnt seem to be accepting my user input and continuing. Its stuck on waiting for me to enter something even though I did. – udit Nov 02 '12 at 21:14