2017-10-19 133 views
0

如果我运行的某些主机下面的脚本:从sudo的命令删除冗长的伪终端

ssh -t myhost /usr/bin/sudo /bin/bash <<'HERE_DOCUMENT' 
whoami 
uptime 
exit 
HERE_DOCUMENT 

我得到以下输出:

Pseudo-terminal will not be allocated because stdin is not a terminal. 
sudo: sorry, you must have a tty to run sudo 

为须藤设置启用requiretty。我不想改变这个设置。

所以我从existing StackOverflow answer的修复是添加-tt选项到ssh。

ssh -tt myhost /usr/bin/sudo /bin/bash <<'HERE_DOCUMENT' 
whoami 
uptime 
exit 
HERE_DOCUMENT 

但是,创建另一个问题,输出变得太冗长:

[[email protected] ec2-user]# whoami 
root 
[[email protected] ec2-user]# uptime 
03:44:29 up 2 days, 3:26, 2 users, load average: 0.00, 0.01, 0.05 
[[email protected] ec2-user]# exit 
exit 
Connection to myhost closed. 

什么是去除提示和命令呼应的最佳方式?
这些命令只是示例,我确实需要使用sudo访问来运行特权命令。

+0

既然你有'sudo'访问,也许你可以在你的公钥添加到'〜根/的.ssh /授权keys',然后做'SSH根@ myhost的/ bin/bash

+0

您应该将其作为答案发布。这可以帮助其他人。在我的情况下,我想尽量减少对远程系统的任何更改。根并不总是被允许直接SSH到主机。 – dabest1

回答

1

如果您不想要交互式shell会话,请勿在tty上启动交互式shell。刚开始那非交互式运行您的命令外壳:

ssh -tt host "sudo bash -c ' 
    whoami 
    uptime 
    '"