栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

shell脚本nfs

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shell脚本nfs

nfs脚本实现
  • 所有版本的实现的前提是进行ssh公钥的分发,也就是脚本2
  • 1.简单版 (需要秘钥分发)
  • 2. 秘钥分发脚本
  • 3. 升级版
  • 4. 最终版

所有版本的实现的前提是进行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 </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 "yesr"; exp_continue }
         "password:" { send "$passwdr" }
     }
   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 </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 </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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/450599.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号