所有版本的实现的前提是进行ssh公钥的分发,也就是脚本2
1.简单版 (需要秘钥分发)
#!/bin/bash
echo "nfs服务的部署,请在服务器端进行脚本的执行"
read -p "nfs服务器ip:" sip
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 "客户端的配置开始了!!"
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)
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
}
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
}
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
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