通过SSH运行交互式Bash命令 [英] Running interactive Bash commands over ssh
问题描述
我正在尝试使用Chef自动执行服务器供应过程。由于我不想以root身份运行chef,因此我需要一个chef / deployer用户。但我不想手动创建此用户。相反,我想使这一步骤自动化。因此,我对脚本进行了拍摄,但是遇到了一个问题:
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:
问题是如果我运行
>ssh root@123.345.345.567 '/bin/bash -e' < ./add_user.sh
其中add_user包含
where add_user contains
//..if the username doesnt exist already
adduser $USERNAME --gecos ''
我从没看到命令的输出或提示。
I never see the output or the prompts of the command.
- 有没有办法以这种方式运行交互式命令?
- 是否有更好的方式以自动方式添加用户?
推荐答案
尝试一下:
ssh -t root@<ipaddress> adduser $USERNAME --gecos
不知道为什么会有 $ 您原始示例的IP地址中的code>-可能会导致
ssh
无法连接,但是由于您没有指出这种故障,我
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.
由于 add_user.sh
只是一个简单的命令,因此不需要显式运行 bash
或重定向的复杂性增加了,只需通过 ssh运行
。 adduser
命令
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
.
最后,由于 $ USERNAME
很可能是在本地定义的,而不是在远程端,即使您可以使用原始命令执行您所说的话,您最终也会在远程端运行 adduser --gecos
,这不是您想要的。
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.
这篇关于通过SSH运行交互式Bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!