python - 通过脚本结果进行远程 NFS 配置

标签 python linux bash ssh

我对此真的很陌生 - 我这样做主要是为了练习。我正在尝试创建一个脚本,该脚本允许跨多个服务器部署 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/

相关文章:

python - Tensorflow,预测值概率 (ROI)

linux - 运行命令并显示控制台输出的 Bash 脚本,直到出现某些短语或超时

.net - 告诉 mono/.NET 只使用 X 内存量?

c - 错误 : expected declaration specifiers or '...' before XXX (all kinds of parameters)

python - 在函数中定义类是一种可接受的模式吗?

python - 是否可以将 "range"(即max-min)添加到python中的pandas描述函数中?

alias - ~/.bashrc 别名更改目录不起作用

linux - 如何在 Bash 中运行程序时播放声音文件

linux - bash中使用 "declare -a"声明数组有什么好处?

python - Python 应用程序的 GUI,使用最终将在 EC2 上运行的交互式代理 API