在shell脚本中使用su

我正在自动化一个部署过程,我希望能够在我的机器上调用一个.sh文件,让它执行我的构build,并将.zip上传到服务器,然后在服务器上做一堆东西。 我需要做的事情之一就是要成为根。 所以,我想要做的是这样的:

ssh [email protected] <<END_SCRIPT su - #password... somehow... #stop jboss service server_instance stop #a bunch of stuff here #all done! exit END_SCRIPT 

这甚至有可能吗?

你有没有考虑一个密码较less的sudo?

su代替sudo,使用sudo中的NOPASSWD设置适当的命令。 你会想让允许的命令设置尽可能有限。 让它调用为根命令运行一个脚本可能是最简洁的方法,如果你不熟悉sudoer文件的语法,最简单的做法是保证安全。 对于需要加载完整环境的命令/脚本, sudo su - -c command可以工作,虽然可能是矫枉过正的。

你所描述的可能是预料之中的 。

您可以将一个命令作为parameter passing给SSH,以在服务器上运行该命令,然后退出:

ssh user@host "command to run"

这也适用于多个命令的列表:

ssh user@host "command1; command2; command3"

或者:

ssh user@host "
command1
command2
command3
"

正如其他用户在我之前指出的那样,在服务器上运行su会启动一个新的shell,而不是以root身份执行脚本中的后续命令。 您需要做的是使用sudosu -c ,并使用-t开关强制TTY分配到SSH(如果需要inputroot密码,则需要):

ssh -t user@host 'su - -c "command"'
ssh -t user@host 'sudo command'

总结一下,完成你想要做的一个方法是:

#!/bin/bash
ssh -t [email protected] "
sudo some_command
sudo service server_instance stop
sudo some_other_command
"

由于sudo通常会在再次询问root密码之前记住您的授权级别,因此只需将sudo预设为需要以root用户身份运行的所有命令即可,这是在服务器上以root用户身份运行命令的最简单方法。 在/etc/sudoers为你的用户添加一个NOPASSWD规则会使这个过程更平滑。

我希望这有帮助 :-)

不可以。即使你把密码设为su ,你仍然需要处理su会打开一个新的shell,这意味着你的进一步命令不会被传递给它。 您将需要为根操作编写一个脚本以及可以使用适当的权限调用它的助手可执行文件。

编写一个期望的脚本。 我知道你已经find了这个答案,但是如果你需要login到一些需要交互式input密码的服务器,这可能会有帮助。

这是一种基于TCL的语言,所以与普通的旧shell脚本相比,它可能有点奇怪。 但是它处理文本input的自动化,并且你猜对了,在进入任何自动input密码或者权限升级之前,“期望”输出的某些位。

以下是一个很好的链接: http : //bash.cyberciti.biz/security/expect-ssh-login-script/

以防万一网站出现故障:

 #!/usr/bin/expect -f # Expect script to supply root/admin password for remote ssh server # and execute command. # This script needs three argument to(s) connect to remote server: # password = Password of remote UNIX server, for root user. # ipaddr = IP Addreess of remote UNIX server, no hostname # scriptname = Path to remote script which will execute on remote server # For example: # ./sshlogin.exp password 192.168.1.11 who # ------------------------------------------------------------------------ # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- # set Variables set password [lrange $argv 0 0] set ipaddr [lrange $argv 1 1] set scriptname [lrange $argv 2 2] set arg1 [lrange $argv 3 3] set timeout -1 # now connect to remote UNIX box (ipaddr) with given script to execute spawn ssh root@$ipaddr $scriptname $arg1 match_max 100000 # Look for passwod prompt expect "*?assword:*" # Send password aka $password send -- "$password\r" # send blank line (\r) to make sure we get back to gui send -- "\r" expect eof 

我知道这有点迟,但是我会亲自使用CPAN提供的Net :: SSH :: Expect。

http://search.cpan.org/~bnegrao/Net-SSH-Expect-1.09/lib/Net/SSH/Expect.pod

你可以使用'ssh example.com su -lc“$ cmd”'。

当命令包含选项时,你需要引用它,否则“su”可能会吃掉它们(“su:无效选项 – 'A')。

 ssh -AX -tt example.com 'su -lc "tmux new-session -A"'