[英]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.