NFS服务自动搭建及挂载脚本

一、写脚本的动机

由于最近老是搭建NFS,虽然不复杂,但是很繁琐。安装服务、修改配置文件、手动挂载、写入开机自动挂载等于是就写了一个脚本

二、脚本说明及审明

作用:该脚本主要实现NFS自动安装,客户端的自动挂载、写入开机自动挂载
使用环境:centos6、nfs客户端的个数为2个
参数:nfs服务端ip、第1个客户端IP、第2个客户端IP、第1个客户端密码、第2个客户端密码、NFS目录
申明:该脚本在本人的服务器上跑是正常的,如果你要用于自己的环境需先测试,该脚本完全处于作者自己爱好,使用脚本请三思。脚本中的参数请根据实际的情况填写,脚本内容如下:

#!/bin/bash

#######################################################################################################################################
##                                                                                                                                   ##
##Function: this script mainly realizes NFS automatic installation and automatic mounting of client.                                 ##
##Usage environment: the number of centos6 and NFS clients is 2.                                                                     ##
##Parameters: NFS server side IP, first client IP, second client IP, first client password, second client password, NFS directory    ##
##Author: heruiguo                                                                                                                   ##
#######################################################################################################################################

#Check whether the IP address is legitimate
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/rc.d/init.d/functions
function check_ip()
{
     IP=$1
    VALID_CHECK=$(echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
    if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
        if [ $VALID_CHECK == "yes" ]; then
         echo "IP $IP  Correct!"
            return 0
        else
            echo "IP $IP no Correct!"
            return 1
        fi
    else
        echo "IP error!"
        return 1
    fi
}

#mkdir  nfs  dir
mkdir_nfs_dir()
{
mkdir  -p  $nfs_dir
}

#start nfs  server
nfs_start()
{
service  rpcbind  restart
service  nfs  restart
}


#Determine whether the server and client are installing NFS services. If there is no installation service, install and start it first.
pd_nfs_install()
{
rpm -aq |grep  nfs-utils >/dev/null
if [ $? -eq 0 ];then
        echo "NFS service has been installed"
else
    echo "############################The NFS service is being installed############################"
        yum   install   nfs-utils  -y >/dev/null 
        echo "############################The NFS service is being started############################"
        nfs_start
fi
}

#NFS directory permissions definition
qx="(rw,no_root_squash)"

#Verify that NFS server side IP is legitimate
while true; do
    read -p "Please enter the IP address of the NFS server: " NFS_SERVER_IP
    check_ip $NFS_SERVER_IP
    [ $? -eq 0 ] && break
done


#Verify whether the NFS client IP is legitimate
while true; do
read -p "Please enter the IP of the first NFS client: " nfs_client1
 check_ip $nfs_client1
    [ $? -eq 0 ] && break
done

#Verify whether the NFS client IP is legitimate
while true; do
read -p "Please enter the IP of second NFS clients: " nfs_client2
check_ip $nfs_client2
    [ $? -eq 0 ] && break
done

read -p "Please enter the password for the first NFS client: "     nfs_passwd_1
read -p "Please enter the second NFS client's password: "     nfs_passwd_2
read -p "Please enter the NFS directory: "           nfs_dir






echo "###########################Server execution###################################"
#1、Close the firewall
service  iptables   stop
#2、Create the NFS directory
mkdir_nfs_dir

#3、Increase the access rights of the client
cat >/etc/exports<<EOF
$nfs_dir   $nfs_client1$qx
$nfs_dir   $nfs_client2$qx
EOF
#4、Start the NFS service
nfs_start


echo "###########################NFS customer 1 terminal execution###################################"
sshpass -p  $nfs_passwd_1  ssh root@$nfs_client1  -o StrictHostKeyChecking=no <<EOF
server  iptabes   stop
yum   install   nfs-utils  -y
service  rpcbind  start
service  nfs  start
mkdir  -p $nfs_dir
umount $nfs_dir
mount  -t  nfs  $NFS_SERVER_IP:$nfs_dir   $nfs_dir
sed -i '/nfs/d' /etc/fstab 
echo "$NFS_SERVER_IP:$nfs_dir        $nfs_dir           nfs         defaults       0     0 " >>/etc/fstab
EOF

echo "###########################NFS customer 2 terminal execution###################################"
sshpass -p  $nfs_passwd_2  ssh root@$nfs_client2  -o StrictHostKeyChecking=no <<EOF
server  iptabes   stop
yum   install   nfs-utils  -y
service  rpcbind  start
service  nfs  start
mkdir  -p $nfs_dir
umount $nfs_dir
mount  -t  nfs  $NFS_SERVER_IP:$nfs_dir   $nfs_dir
sed -i '/nfs/d' /etc/fstab 
echo "$NFS_SERVER_IP:$nfs_dir        $nfs_dir           nfs         defaults       0     0 " >>/etc/fstab
EOF

猜你喜欢

转载自www.cnblogs.com/heruiguo/p/9156209.html
今日推荐