【发布时间】:2016-02-23 14:22:27
【问题描述】:
问题是如何在远程 SSH 会话中替换本地和远程变量。
我在本地服务器上有一个脚本。以下是其中的摘录。
#!/bin/bash
Date=`/bin/date`
Schedule="2016-02-01 14:30:00"
Startup_loop()
{
#ssh connection to remote host $1 and start of loop of statements to be executed remotely
ssh root@$1 << EOF
#To display the Remote Hostname
hostname
#Check if the following local variables are available remotely as well
echo $Date
echo $Schedule
#Check and set the variable sysconf_clock_var on remote host
clock_var=`/usr/bin/grep BLAH /etc/Command | /usr/bin/awk -F\" '{ print $2}'`
echo $clock_var
#Modify the proc based on $startup_schedule
echo $clock_var >> /proc/Schedule
#Change the paramter on the remote file /etc/Command
sed -i -e 's/BLAH="yes"/BLAH="no"/' /etc/Command
echo "The remote ssh is completed" >> /tmp/File_$Schedule_$Date.log
EOF
}
#Main Loop
##Accept the system IP from the User
echo Hello Please enter the system IP
read systemIP
#Call the procedure by passing the system IP
Startup_loop $systemIP
脚本未按预期运行。例如,ssh 循环中的 hostname 命令的值显示本地主机名。这里有什么问题?
【问题讨论】:
-
@Kenster 修复了您的代码格式。怎么又破了?
-
顺便说一句,我忽略了很多 shellcheck.net 会自动找到并识别修复程序的错误 - 一般而言,请在此处提问之前运行您的代码。
-
我只是按照 Shelter 给出的说明进行操作。那它坏了吗?
-
我会在 shellcheck 上检查脚本并还原。谢谢顺便说一句。
-
@SachinH,缺少引号并不是次要的。