我对此真的很陌生 - 我这样做主要是为了练习。我正在尝试创建一个脚本,该脚本允许跨多个服务器部署 LEMP,并允许根据服务器 IP 等进行“NFS”配置。基本上,这只是一个允许向上扩展并尽可能自动化的脚本。抱歉,如果我听起来很无知 - 我很新!
到目前为止,LEMP 的自动安装对我来说非常简单且易于组装。 SSHkey 确实有点困惑。一旦我弄清楚我在变量部分做错了什么,我就会尽可能地清理它。本质上,除了 IP 变量之外,所有内容都被添加到/etc/exports 的新行中。我相信这是因为它是一个本地变量而我是远程执行的?谢谢!
这是我遇到问题的部分:
for NFS in $(cat nfs.txt)
do
for hostname in $(cat hostname.txt)
do
ssh-keygen -t rsa
ssh root@$NFS mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh root@$NFS 'cat >> .ssh/authorized_keys'
ssh root@$hostname mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh root@$hostname 'cat >> .ssh/authorized_keys'
ssh root@$hostname "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
ssh root@$NFS "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
IP=`ssh root@$hostname ifconfig | grep Bcast | cut -d: -f2 | cut -d" " -f1`
echo $IP > ip.txt
scp ip.txt root@$hostname:ip.txt
IPLOCAL=`cat ip.txt`
ssh root@$NFS 'echo -e "/var/www/html $IPLOCAL(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports'
done
done
这就是我原来的
for NFS in $(cat nfs.txt)
do
for hostname in $(cat hostname.txt)
do
ssh-keygen -t rsa
ssh root@$NFS mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh root@$NFS 'cat >> .ssh/authorized_keys'
ssh root@$hostname mkdir -p .ssh
cat .ssh/id_rsa.pub | ssh root@$hostname 'cat >> .ssh/authorized_keys'
ssh root@$hostname "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
ssh root@$NFS "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
IP=`ssh root@$hostname ifconfig | grep Bcast | cut -d: -f2 | cut -d" " -f1`
ssh root@$NFS 'echo -e "/var/www/html $IP(rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports'
done
done
最佳答案
如果你想将远程变量的值设置为本地变量,你必须使用这样的语法。
localValue='local'
ssh user@remote 'remoteValue='"$localValue;"'echo remoteValue'
因此,如果我正确理解您的目的,您应该编写如下代码:
...
IP=`ssh root@$hostname ifconfig | grep Bcast | cut -d: -f2 | cut -d" " -f1`
for i in $IP
do
ssh root@$NFS 'echo -e /var/www/html '"$i"'(rw,sync,no_root_squash,no_subtree_check) >> /etc/exports'
done
...
关于python - 通过脚本结果进行远程 NFS 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24816740/