简体   繁体   English

如何使用bash脚本在远程计算机上运行命令?

[英]How to run commands on remote machine using bash script?

I am trying to run the below commands on remote machine, but nothing is being getting executed, Can anyone point out the issue with the below script? 我正在尝试在远程计算机上运行以下命令,但未执行任何操作,有人可以指出以下脚本的问题吗?

FYI settings.yml and fuscation_util.rb files are present in the testuser account FYI settings.yml和fuscation_util.rb文件存在于testuser帐户中

SCRIPT: 脚本:

sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=no -T root@HOSTNAME <<EOF
    su - testuser
    NEWLINE=$'\n'
    read -s -p "Enter Presto Password:${NEWLINE}" VD_PASSWORD
    KEY=`cat settings.yml |grep '^key:'|sed 's/^key: //'`
    VALUE=`ruby fuscation_util.rb encrypt  "$KEY" "$VD_PASSWORD"`
    echo "$VALUE"
EOF

OUTPUT: 输出:

cat: settings.yml: No such file or directory
ruby: No such file or directory -- fuscation_util.rb (LoadError)

I am facing the error only when I try to assign the poutput to some variable, with the below script I didnt get any errors 仅当我尝试将poutput分配给某个变量时,我才遇到错误,使用以下脚本我没有得到任何错误

SCRIPT: 脚本:

sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=no -T root@HOSTNAME <<EOF
    su - testuser
    NEWLINE=$'\n'
    read -s -p "Enter Presto Password:${NEWLINE}" VD_PASSWORD
    cat settings.yml |grep '^key:'|sed 's/^key: //'
    ruby fuscation_util.rb encrypt  "testvalue" "testpassword"
EOF

OUTPUT: 输出:

WORKING FINE

I believe that because you are connecting as root, the directory you land in when connecting is the root user home directory. 我相信,因为您是以root用户身份连接的,所以连接时进入的目录是root用户主目录。 Even though you change users, you would still be in the same starting folder (probably /root ). 即使更改用户,您仍将位于同一起始文件夹(可能是/root )中。

If you explicitly cd into the desired location before executing the commands, you'll have access to the desired files. 如果您在执行命令之前将cd明确地插入所需的位置,则可以访问所需的文件。

Another option ( suggested by Shravan40 ) would be to specify absolute paths to the files you wish to handle. 另一个选项( 由Shravan40建议 )是指定要处理的文件的绝对路径。


It should be mentioned also that allowing root access via SSH is considered a security risk. 还应该提到的是,允许通过SSH进行根访问被视为安全风险。 I would suggest avoiding that if at all possible. 我建议尽可能避免这种情况。

As soon as your ssh connect to the remote host as root it goes to the root directory. 一旦ssh以root用户身份连接到远程主机,它将进入根目录。 So try a change directory inside or give absolute path to the files. 因此,请尝试在其中的更改目录或提供文件的绝对路径。

Buts its a security risk though to run commands remotely. 虽然远程运行命令具有安全风险。 Probably you can place a script on the remote host and pass parameter, rather using an echo inside ssh. 可能您可以将脚本放在远程主机上并传递参数,而不是在ssh中使用回显。

Update: 更新:

Just make all the assignments before the ssh and use key value as expressions while calling ruby. 只需在ssh之前进行所有分配,然后在调用ruby时将键值用作表达式即可。

Try this: 尝试这个:

read -s -p "Enter Presto Password:${NEWLINE}" VD_PASSWORD
sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=no -T root@HOSTNAME <<EOF
su - testuser
cat settings.yml |grep '^key:'|sed 's/^key: //'
ruby fuscation_util.rb encrypt  `cat settings.yml|grep '^key:'|sed 's/^key: //'` $VD_PASSWORD
EOF

除了使用完整路径之外,您还需要用<<'EOF'替换<<EOF或所有后向滴答声,否则它们将被发送到远程端之前在调用方进行处理。

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

相关问题 在远程机器 php 上运行 bash 脚本 - Run bash script on remote machine php 如何在远程计算机上运行脚本 - How to run a script to remote machine 如何编写bash shell脚本ssh到远程机器并更改用户并导出env变量并执行其他命令 - how to write a bash shell script to ssh to remote machine and change user and export a env variable and do other commands 如何在远程计算机上异步执行bash脚本 - How to execute bash script on a remote machine asynchronously 如何在Shell脚本中打开bash / ssh会话并在远程会话中运行命令? - How to open a bash/ssh session in shell script and run commands in the remote session? 如何在无业游民的测试中使用命令运行bash脚本? - How to run bash script with commands in tests in vagrant? 如何通过 jenkins 和 SSH 在远程机器中运行 docker 命令? - how to run docker commands in remote machine via jenkins and SSH? 使用 bash 脚本 ssh 远程机器时找不到命令 - Command not found when using bash script to ssh remote machine Bash:根据用户输入,在本地或远程计算机上的功能中运行所有命令 - Bash: Based on user input run all commands in a function on local OR on remote machine 在远程计算机中将Shell脚本转换为Sudo并执行命令 - Shell script to Sudo in Remote Machine and execute commands
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM