SSH远程执行本地命令
服务器有run.sh 脚本
#!/bin/bash
# 请将以下IP地址替换为你要连接的远程电脑的IP地址
REMOTE_IP="127.0.0.1"
# 请将以下用户名替换为你要连接的远程电脑的用户名
REMOTE_USER="root"
if [ "$1" == "start" ];then
echo "start参数:$1" >> /mnt/server/run.log
ssh ${REMOTE_USER}@${REMOTE_IP} "sh /mnt/data/server/run.sh start"
elif [ "$1" == "stop" ];then
echo "stop参数:$1" >> /mnt/server/run.log
ssh ${REMOTE_USER}@${REMOTE_IP} "sh /mnt/data/server/run.sh stop"
else
echo "无效参数:$1" >> /mnt/server/run.log
fi
执行命令
./run.sh start
远程执行脚本并未生效,原因在于脚本中未有
source /etc/profile
将脚本更改为
#!/bin/bash
# 请将以下IP地址替换为你要连接的远程电脑的IP地址
REMOTE_IP="127.0.0.1"
# 请将以下用户名替换为你要连接的远程电脑的用户名
REMOTE_USER="root"
if [ "$1" == "start" ];then
echo "start参数:$1" >> /mnt/server/run.log
ssh ${REMOTE_USER}@${REMOTE_IP} "source /etc/profile; sh /mnt/data/server/run.sh start"
elif [ "$1" == "stop" ];then
echo "stop参数:$1" >> /mnt/server/run.log
ssh ${REMOTE_USER}@${REMOTE_IP} "sh /mnt/data/server/run.sh stop"
else
echo "无效参数:$1" >> /mnt/server/run.log
fi
远程执行脚本成功