shell脚本nfs

所有版本的实现的前提是进行ssh公钥的分发,也就是脚本2

1.简单版 (需要秘钥分发)

#!/bin/bash
echo "nfs服务的部署,请在服务器端进行脚本的执行" 
read -p "nfs服务器ip:" sip

#安装nfs rpc
yum -y install nfs-utils rpcbind &>/dev/null

#创建一个存储目录
mkdir /data
chown -R nfsnobody.nfsnobody /data

#配置文件的编写
echo "/data 192.168.80.0/24(rw,sync)" > /etc/exports

#启动
systemctl start rpcbind &>/dev/null
if [ $? -eq 0 ];then
systemctl start nfs &>/dev/null
systemctl enable nfs &>/dev/null
fi
systemctl enable rpcbind &>/dev/null
echo  "nfs start success.."

#客户端的配置
echo "客户端的配置开始了!!"
read -p "请输入客户端的ip" kip
#安装软件
ssh  -t root@$kip<<-EOF
yum install -y nfs-utils &>/dev/null

systemctl start nfs &>/dev/null
systemctl enable nfs &>/dev/null
#创建挂载的目录
mkdir /nfs
#目录挂载
mount -t nfs $sip:/data  /nfs
EOF
echo "客户端完成!!请测试"

2. 秘钥分发脚本

#!/bin/bash

menu(){
cat <<EOF
========================
公钥分发程序
========================
1.进行公钥的分发
2.分发完成,退出检查ssh
========================
EOF
}

sshid(){
read -p "要进行公钥分发的主机ip:" gip
rpm -q expect &>/dev/null
if [ $? -eq 0 ];then
   yum -y install expect &>/dev/null && echo "ecpect is ok"
fi
if [ ! -f ~/.ssh/id_rsa  ];then
    ssh-keygen -P "" -f ~/.ssh/id_rsa && echo "公钥生成完毕"
fi
ping -c1  $gip &>/dev/null
if [ $?  -eq 0  ];then
    echo "start copy id"
    read -p "要操作的用户:" user
    read -p "密码:" passwd
    
   /usr/bin/expect <<-EOF
   spawn ssh-copy-id -i $user@$gip
   expect {
         "yes/no" { send "yes\r"; exp_continue }
         "password:" { send "$passwd\r" }
     }
   expect eof
EOF
   echo "公钥分发完毕"
else
   echo "$gip is not found"
fi
echo "ok"
}

while true
do
  menu
  read -p "请选择你要进行的操作:" cz
  case $cz in
     1)
      sshid
      ;;
     2)
      echo "退出完成"
      exit
      ;;
     *)
      echo "输入错误请重新选择:"
  esac
done

3. 升级版

#!/bin/bash
menu(){
cat <<EOF
====================
1.nfs服务器配置
2.nfs客户端进行配置
3.退出脚本           
====================
EOF
}

#服务器的挂载目录
server_dir=/data
nfsserver(){
ssh -tq $sip <<EOF
#安装nfs rpc
yum -y install nfs-utils rpcbind &>/dev/null
echo "安装完成"
#创建一个存储目录
echo "创建目录$server_dir"
if [ -d $server_dir ];then 
   rm -rf $server_dir &>/dev/null
   echo "$server_dir 已经存在删除,重新创建"
fi
mkdir $server_dir
chown -R nfsnobody.nfsnobody $server_dir
#配置文件的编写
echo "$server_dir 192.168.80.0/24(rw,sync)" > /etc/exports
echo "配置文件编辑完成"
#启动
systemctl start rpcbind &>/dev/null
if [ $? -eq 0 ];then
systemctl start nfs &>/dev/null
systemctl enable nfs &>/dev/null
fi
systemctl enable rpcbind &>/dev/null
echo  "启动成功"
echo  "nfs的服务器端配置完成"
EOF
}


#客户端要挂载的目录
client_dir=/nfs
nfsclient(){
#客户端的配置
echo "客户端的配置开始了!!"
#read -p "请输入客户端的ip" kip
#安装软件
ssh  -tq root@$kip<<-EOF
yum install -y nfs-utils &>/dev/null
systemctl start nfs &>/dev/null
systemctl enable nfs &>/dev/null
#创建挂载的目录
echo "挂载的目录 $client_dir"
if [ -d $client_dir ];then 
   rm -rf $client_dir &>/dev/null
   echo "$client_dir 已经存在删除,重新创建"
fi
mkdir $client_dir
#目录挂载
mount -t nfs $sip:$server_dir  $client_dir
EOF
echo "客户端完成!!请测试"a
}

read -p "请输入你要进行配置服务器的ip:" sip
while true
do
menu
read -p "请输入你要进行的操作:(数字):" cz
case $cz in
  1)
   #read -p "请输入你要进行配置服务器的ip:" sip
   ping -c1 $sip &>/dev/null
   if [ $? -eq 0 ];then
       nfsserver
   else
      echo "$sip 主机不存在,退出!!请你检查机器好吧"
      exit
   fi
   ;;
  2)
   read -p "请输入nfs客户端的ip:" kip
   ping -c1  $kip &>/dev/null
   if [ $? -eq 0 ];then
      nfsclient
   else
     echo "$cip 不存在"
   fi
  ;;
  3)
  echo "exit"
  exit
  ;;
  *)
  echo -e  "输入错误,请输入正确的选择:\n"
  ;;
esac
done

4. 最终版

#!/bin/bash
menu(){
cat <<EOF
====================
1.nfs服务器配置
2.nfs客户端进行配置
3.退出脚本           
====================
EOF
}

#服务器的挂载目录
#server_dir=/data
nfsserver(){
ssh -tq $sip <<EOF
#安装nfs rpc
yum -y install nfs-utils rpcbind &>/dev/null
echo "安装完成"
#创建一个存储目录
echo "创建目录$server_dir"
if [ -d $server_dir ];then 
   rm -rf $server_dir &>/dev/null
   echo "$server_dir 已经存在删除,重新创建"
fi
   
mkdir -p  $server_dir
chown -R nfsnobody.nfsnobody $server_dir
#配置文件的编写
echo "$server_dir 192.168.80.0/24(rw,sync)" >> /etc/exports
echo "配置文件编辑完成"
#启动
systemctl start rpcbind &>/dev/null
if [ $? -eq 0 ];then
systemctl start nfs &>/dev/null
systemctl enable nfs &>/dev/null
fi
systemctl enable rpcbind &>/dev/null
echo  "启动成功"
echo  "nfs的服务器端配置完成"
systemctl restart nfs
echo 
EOF
echo
}


#客户端要挂载的目录
#client_dir=/nfs
nfsclient(){
#客户端的配置
echo "客户端的配置开始了!!"
#安装软件
ssh  -tq root@$kip<<-EOF
yum install -y nfs-utils &>/dev/null
systemctl start nfs &>/dev/null
systemctl enable nfs &>/dev/null
#创建挂载的目录
echo "挂载的目录 $client_dir"
if [ -d $client_dir ];then 
   rm -rf $client_dir &>/dev/null
   echo "$client_dir 已经存在删除,重新创建"
fi
mkdir  -p $client_dir
#目录挂载
df -h | grep $client_dir
if [ $? -eq 0 ];then
   echo "$client_dir 该目录已经被挂载 "
   umount -lf $client_dir &>/dev/null
fi 
mount -t nfs $sip:$server_dir  $client_dir
echo "$client_dir 挂载成功"

EOF
echo "客户端完成!!请测试"
echo
}

read -p "请输入你要进行配置服务器的ip(服务器只有一台):" sip

#read -p "请输入服务的挂载目录:" server_dir

#read -p "输入要进行nfs客户端挂载的目录,有文件的请提前备份:" client_dir
while true
do
read -p "请输入服务的挂载目录:" server_dir

read -p "输入要进行nfs客户端挂载的目录,有文件的请提前备份:" client_dir

menu
read -p "请输入你要进行的操作:(数字):" cz
case $cz in
  1)
   ping -c1 $sip &>/dev/null
   if [ $? -eq 0 ];then
       nfsserver
   else
      echo "$sip 主机不存在,退出!!请你检查机器好吧"
      exit
   fi
   ;;
  2)
   read -p "请输入nfs客户端的ip:" kip
   ping -c1  $kip &>/dev/null
   if [ $? -eq 0 ];then
      nfsclient
   else
     echo "$cip 不存在"
   fi
  ;;
  3)
  echo "exit"
  exit
  ;;
  *)
  echo -e  "输入错误,请输入正确的选择:\n"
  ;;
esac
done
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长安有故里y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值