我拥有的Shell脚本有两个不同的部分。
示例:
#!/bin/bash
# Setting Environment Platform
init(){
echo "Enter username of guest machine"
read USERNAME
echo "Enter IP of guest machine"
read GUEST_IP
echo "Guest IP : $GUEST_IP"
echo "Guest Username $USERNAME"
echo "running === ssh -l $USERNAME $GUEST_IP"
if ssh -l $USERNAME $GUEST_IP; then
install_packages
echo SUCCESS
else
retry_connection
echo FAIL
fi
}
# Scripts for package installation on remote host.
install_packages(){
sudo apt-get -y update && apt-get -y upgrade
sudo apt-get -y install aptitude
sudo apt-get -y install default-jre
sudo apt-get -y install default-jdk
}
retry_connection(){
if ssh -l $USERNAME $GUEST_IP; then
install_packages
echo SUCCESS
else
retry_connection
echo FAIL
fi
}
因此,第一部分init()
应该在我的机器本身中运行。在ssh连接成功时,install_packages()应该在init()中输入的客户机中运行。
我该怎么办?
答案 0 :(得分:1)
您必须将您的功能定义传递到远程端,然后在那里调用它。要做到这一点而不是调用TA1R
,你可能会这样做:
install_packages
参考文献:Shell script: Run function from script over ssh,https://serverfault.com/questions/649359/executing-local-function-code-on-a-remote-server