【问题标题】:remote NFS config via script results通过脚本结果进行远程 NFS 配置
【发布时间】:2014-07-18 14:40:42
【问题描述】:

我对此真的很陌生 - 我主要是为了练习。我正在尝试创建一个脚本,该脚本将允许跨多个服务器部署 LEMP,并允许根据服务器 IP 等进行“NFS”配置。基本上,只是一个允许向上扩展并尽可能自动化的脚本。对不起,如果我听起来很无知 - 我很新!

到目前为止,我自动安装 LEMP 对我来说非常简单易行。 SSHkeys 肯定有点乱。一旦我弄清楚我在变量部分做错了什么,我会尽可能多地清理它。本质上,除了 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

【问题讨论】:

    标签: python linux bash ssh


    【解决方案1】:

    如果你想将远程变量的值设置为本地,你必须使用这样的语法。

    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
    ...
    

    【讨论】:

    • 尽量不要使用 -c 来运行你的脚本。
    • 它成功了,我只需要在 ( ) 之前和之后添加一个 " 以便它看起来像 ssh root@$NFS 'echo -e /var/www/html "'$IPREMOTE'( rw,sync,no_root_squash,no_subtree_check)" >> /etc/exports'
    • 有人支持这位绅士 - 我是新手,还没有 15 个代表:(
    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2017-03-14
    • 2011-05-09
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多