udit
udit

Reputation: 2783

Running interactive Bash commands over ssh

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 [email protected] '/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?

Upvotes: 2

Views: 3034

Answers (2)

twalberg
twalberg

Reputation: 62379

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.

Upvotes: 2

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185053

Try using :

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

instead.

Upvotes: 0

Related Questions