我正在尝试从 mac osx 计算机通过 SSH 连接到基于 Solaris 的计算机并传递一些命令,但我不确定最好的方法,并且使用我在网上找到的不同方法遇到了各种问题。现在我要做的最接近的事情是:
欺诈检查.sh
#!/bin/sh
#
SUPNODE="X.X.X.X"
ssh -i /Users/jessebutryn/.ssh/Jesse_id_rsa root@$SUPNODE < /Users/jessebutryn/Documents/scripts/incomplete/fraudcommands.sh
欺诈命令.sh
#!/bin/sh
#
source /root/toolbox/toolbox
jbutryn
fraud 1
当我手动运行这些命令时,输出如下:
joyentmac2252:~ jessebutryn$ ssh [email protected]
[root@sup-jpc-west1 ~]# source /root/toolbox/toolbox
Enter your JPC Headnode username: jbutryn
Toolbox - Version 4.4.6 - It's Wednesday, this must be Devon (20161116)
---- Recent Changes ----
Replaced sdc-ldap/ufds with OpenLDAP client
vm/vmi now shows Swap
Moved creds to environment
Added random element to disabler string
fraud() speed up
Added over() for PD overrides
Added RAM to vms_by_pkg / fraud
Added dates to cust()
Big speed up for cust_vms(). You are welcome.
------------------------
[root@sup-jpc-west1 ~]# fraud 1
2017-06-29|2017-06-30
DC ALIAS UUID RAM BILLING_ID OWNER CREATED
sw_1 SRADB02PRJOY f70b0642-020c-cd72-d644-e0453ccd0e56 65536 14bc8e86-d0f8-11e5-a148-b39af8e5cb7b bc1e2a34-e6fd-c421-d362-aeccb09eb9a4 2017-06-30T20:29:36.853Z
...
但是,当我运行脚本时,它会给出以下输出:
joyentmac2252:~ jessebutryn$ ~/Documents/scripts/incomplete/fraudcheck.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Toolbox - Version 4.4.6 - It's Wednesday, this must be Devon (20161116)
---- Recent Changes ----
Replaced sdc-ldap/ufds with OpenLDAP client
vm/vmi now shows Swap
Moved creds to environment
Added random element to disabler string
fraud() speed up
Added over() for PD overrides
Added RAM to vms_by_pkg / fraud
Added dates to cust()
Big speed up for cust_vms(). You are welcome.
------------------------
/root/toolbox/toolbox: line 1888: [: =: unary operator expected
2017-06-29|2017-06-30
DC ALIAS UUID RAM BILLING_ID OWNER CREATED
下面是工具箱脚本中的第 1888 行,我什至不确定它的目的是什么,但我知道我与该用户不匹配:
[root@sup-jpc-west1 ~]# sed '1888!d' sup-toolbox/toolbox
if [ $(who am i | /usr/bin/cut -d' ' -f 1) = "some_user" ]; then
我尝试了各种传递命令的解决方案,包括:
echo "jbutryn" | ssh -i ...
ssh -i ... "
jbutryn
fraud 1
"
我尝试过echo
欺诈read
命令脚本中的命令。我对这一切都很陌生,所以非常感谢您的帮助。谢谢!
答案1
您收到[: =: unary operator expected
错误是因为who am i
左侧的命令$(who am i | ...)
没有返回任何文本,因此您应该始终放置此类字段/变量/等。用引号引起来。
原因who am i
是不返回任何文本,因为它为您提供了有关终端的信息(仅当您“手动”运行它时才存在)。不幸的是,如果您运行命令(所谓的非交互式会话),ssh 不会启动“真实/完整”终端会话。您应该尝试将其替换为 eg $USER
,这甚至更有效(变量访问比启动新程序要快得多):
if [ "$USER" = "some_user" ]; then
作为解决方法,您可以使用 运行它ssh -t <script>
,这将分配一个伪终端,从而who am i
再次工作。