远程Linux主机上执行本地脚本 [英] Execute local script on remote Linux host

查看:299
本文介绍了远程Linux主机上执行本地脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也不会没有远程主机上的root权限运行本地脚本。我已编辑并允许sudoers文件从主目录(/家/用户名/脚本)运行该脚本。该脚本创建的结果一个txt文件,但我的输出终端和管道输出重定向到本地计算机上的文本文件。

I have a local script that will not run without root privs on the remote host. I have edited and allowed the sudoers file to run the script from the home dir (/home/username/script). The script creates a txt file with the results but I redirect the output to terminal and pipe the output to a text file on the local machine.

我不能SSH用户@主机名须藤-sv< script.sh> RESULTS.TXT,因为这将不会运行从远程主机的脚本,特别是主目录。

I cannot "ssh user@hostname sudo -Sv < script.sh > results.txt" because this will not run the script from the remote host, specifically the home dir.

有谁知道的一种方式(一行)的脚本复制到远程主机和功放;在检索输出执行它的根?

Does anyone know of a way (one line) to copy the script to the remote host & execute it as root while retrieving the output?

感谢您的任何援助

推荐答案

脚本使用 SCP 复制,然后运行它。

Copy the script with scp, then run it.

scp script.sh user@hostname:
ssh user@hostname sudo ./script.sh > results.txt

要做到在同一行:

ssh user@hostname 'cat > script.sh; chmod 755 script.sh; sudo ./script.sh' < script.sh > results.txt

然而,如果你需要输入密码,须藤这是不行的。所有的SSH 的标准输入将被放入脚本。可能有办法做到这一点使用期望,但我没有太多的专业知识在那里。

However, this won't work if you need to enter a password into sudo. All of ssh's stdin will be put in the script. There might be a way to do this using Expect, but I don't have much expertise there.

这篇关于远程Linux主机上执行本地脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆