简体   繁体   English

如何通过 ssh sudo 运行本地脚本

[英]How to sudo run a local script over ssh

I try to sudo run a local script over ssh,我尝试通过 ssh sudo 运行本地脚本,

ssh $HOST < script.sh

and I tried我试过

ssh -t $HOST "sudo -s && bash" < script.sh

Actually, I searched a lot in google, find some similar questions, however, I don't find a solution which can sudo run a local script.实际上,我在谷歌上搜索了很多,找到了一些类似的问题,但是,我没有找到可以 sudo 运行本地脚本的解决方案。

Reading the error message of读取错误信息

$ ssh -t $HOST "sudo -s && bash" < script.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.

makes it pretty clear what's going wrong here.很清楚这里出了什么问题。

You can't use the ssh parameter -t (which sudo needs to ask for a password) whilst redirecting your script to bash's stdin of your remote session.在将脚本重定向到远程会话的 bash 标准输入时,您不能使用 ssh 参数-tsudo需要要求输入密码)。

If it is acceptable for you, you could transfer the local script via scp to your remote machine and then execute the script without the need of I/O redirection:如果您可以接受,您可以通过scp将本地脚本传输到您的远程机器,然后执行脚本而无需 I/O 重定向:

scp script.sh $HOST:/tmp/ && ssh -t $HOST "sudo -s bash /tmp/script.sh"

Another way to fix your issue is to use sudo in non-interactive mode -n but for this you need to set NOPASSWD within the remote machine's sudoers file for the executing user.解决问题的另一种方法是在非交互模式下使用sudo -n但为此您需要在远程机器的 sudoers 文件中为执行用户设置NOPASSWD Then you can use然后你可以使用

ssh $HOST "sudo -n -s bash" < script.sh

To make Edward Itrich's answer more scalable and geared towards frequent use, you can set up a system where you only run a one line script that can be quickly ported to any host, file or command in the following manner:为了使 Edward Itrich 的答案更具可扩展性并适合频繁使用,您可以设置一个系统,在该系统中只运行一行脚本,该脚本可以通过以下方式快速移植到任何主机、文件或命令:

Create a script in your Scripts directory if you have one by changing the name you want the script to be (I use this format frequently to change 1 word for my script name and create the file, set permissions and open for editing):如果您有一个脚本,请在您的 Scripts 目录中创建一个脚本,方法是更改​​您想要的脚本名称(我经常使用这种格式来更改脚本名称的 1 个单词并创建文件、设置权限并打开以进行编辑):

newscript="runlocalscriptonremotehost.sh"
touch $newscript && chmod +x $newscript && nano $newscript

In nano fill out the script as follows placing the directory and name information of the script you want to run remotely in the variable lines of runlocalscriptonremotehost.sh (only need to edit lines 1-3):在nano中填写脚本如下,将要远程运行的脚本的目录和名称信息放在runlocalscriptonremotehost.sh的变量行中(只需要编辑第1-3行):

HOSTtoCONTROL="sudoadmin@192.168.0.254"
PATHtoSCRIPT="/home/username/Scripts/"
SCRIPTname="scripttorunremotely.sh"
scp $PATHtoSCRIPT$SCRIPTname $HOSTtoCONTROL:/tmp/ && ssh -t $HOSTtoCONTROL "sudo -s bash /tmp/$SCRIPTname"

Then just run:然后运行:

sh ./runlocalscriptonremotehost.sh

Keep runlocalscriptonremotehost.sh open in a tabbed text editor for quick updating, go ahead and create a bash alias for the script and you have yourself an app-ified version of this frequently used operation.在选项卡式文本编辑器中保持runlocalscriptonremotehost.sh打开以进行快速更新,继续为脚本创建 bash 别名,您将拥有此常用操作的应用程序版本。

First of all divide your objective in 2 parts.首先,将您的目标分为两部分。 1) ssh to the host. 1) SSH 到主机。 2) run the command you want as sudo. 2)以sudo身份运行您想要的命令。 After you are certain that you can 1) access the host and 2) have sudo privileges then you can combine the two commands with && .在确定您可以 1) 访问主机和 2) 拥有 sudo 权限后,您可以将这两个命令与&&结合使用。 What x_cmd && y_cmd does is that the y_cmd gets executed after x_cmd has exited successfully.什么x_cmd && y_cmd确实是, y_cmd之后被执行x_cmd已成功退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM