BASH SSH 到另一台服务器的脚本和 运行 作为用户的命令
BASH Script to SSH to another server and run commands as user
我通过论坛和 Google 阅读了很多内容,但是我无法找到针对这个特定问题的解决方法。
基本上我的脚本是 运行ning 在我的本地机器上,然后使用不同的用户名 SSH-ing 到服务器上(直到这里它才有效),我需要在那台机器上 运行 一些命令(它们没有 运行)。
以下是代码中不起作用的部分:
if [[ "${TYPE}" == "cPanel" ]]; then
#Connect to the server with credentials defined earlier in the script - works properly.
ssh $USER@$HOST -p $PORT
#Get the domain by the username - works properly
user=$(sudo grep ${DOM} /etc/userdomains)
#Extract only the username from the above string - works properly
userNew=$(echo ${user} | awk 'END {print $NF}')
#Log in as the new username - works properly
sudo su -s /bin/bash ${userNew}
fi
这是一个 cPanel 服务器,我需要以其中一个 cPanel 用户的身份登录。
请帮忙。
P.S。当 运行 到 cPanel 服务器本身时,上面的脚本 运行s 正确。它仅在调用我本地计算机上的 SSH 脚本时才起作用。
如果你想运行远程服务器上的命令,那么你需要把它们放在SSH命令的末尾。你的脚本目前正在使用 SSH 连接到远程服务器,但随后只是给你一个交互式的 shell。
你需要这样的东西:
ssh $USER@$HOST -p $PORT <command to execute>
使用 here document:
if [[ "${TYPE}" == "cPanel" ]]; then
ssh $USER@$HOST -p $PORT << \EOF
#Get the domain by the username - works properly
user=$(sudo grep ${DOM} /etc/userdomains)
#Extract only the username from the above string - works properly
userNew=$(echo ${user} | awk 'END {print $NF}')
#Log in as the new username - works properly
sudo su -s /bin/bash ${userNew}
EOF
fi
我通过论坛和 Google 阅读了很多内容,但是我无法找到针对这个特定问题的解决方法。
基本上我的脚本是 运行ning 在我的本地机器上,然后使用不同的用户名 SSH-ing 到服务器上(直到这里它才有效),我需要在那台机器上 运行 一些命令(它们没有 运行)。
以下是代码中不起作用的部分:
if [[ "${TYPE}" == "cPanel" ]]; then
#Connect to the server with credentials defined earlier in the script - works properly.
ssh $USER@$HOST -p $PORT
#Get the domain by the username - works properly
user=$(sudo grep ${DOM} /etc/userdomains)
#Extract only the username from the above string - works properly
userNew=$(echo ${user} | awk 'END {print $NF}')
#Log in as the new username - works properly
sudo su -s /bin/bash ${userNew}
fi
这是一个 cPanel 服务器,我需要以其中一个 cPanel 用户的身份登录。
请帮忙。
P.S。当 运行 到 cPanel 服务器本身时,上面的脚本 运行s 正确。它仅在调用我本地计算机上的 SSH 脚本时才起作用。
如果你想运行远程服务器上的命令,那么你需要把它们放在SSH命令的末尾。你的脚本目前正在使用 SSH 连接到远程服务器,但随后只是给你一个交互式的 shell。
你需要这样的东西:
ssh $USER@$HOST -p $PORT <command to execute>
使用 here document:
if [[ "${TYPE}" == "cPanel" ]]; then
ssh $USER@$HOST -p $PORT << \EOF
#Get the domain by the username - works properly
user=$(sudo grep ${DOM} /etc/userdomains)
#Extract only the username from the above string - works properly
userNew=$(echo ${user} | awk 'END {print $NF}')
#Log in as the new username - works properly
sudo su -s /bin/bash ${userNew}
EOF
fi