一、背景描述
因为公司用到sftp结合nfs共享文件场景,sftp服务器挂载了许多其他服务器,在主备切换的时候,一台台挂上去不符合现实,所以写了下面的shell脚本。
二、思路
- 把所有写好的,可以直接拿来用的mount语句写在一个单独的文件,每次从这个文件读取一行,进行挂载,只要把这个文件保持好一定的格式规范,那么就没有问题。
- 挂载前做一定的条件判断,比如端口不通,不符合条件的不挂载了,以免浪费时间。
- 在判断端口的时候记录日志,在挂载的时候记录日志,运维不可缺少的就是日志。
三、脚本详细
# Wirter: liangge
# Date: 2022.06.09
# Description: 批量挂载
#挂载文件路径,并判断(文件可以有多个,但要符合规范:挂载文件里只写mount语句)
dateday=`date "+%Y%m%d"`
logfile="/var/log/mount-${dateday}.log"
echo "BEGIN POSITION" >> $logfile
filelist="/root/mount_sftp_file.sh"
for file in $filelist
do
if [[ ! -f $file ]];then
echo "${file}文件不存在,直接退出脚本。"
exit;
fi
done
#判断包,nc命令属于nmap-ncat包
rpm -qa|grep nmap-ncat > /dev/null
if [[ "$?" != "0" ]]; then
echo "nmap-ncat packet not install,please install first."
exit;
fi
#提示,且记录日志
function PromLog()
{
echo $1
#这里时间是变动的,所以不能封装成变量。
echo "$(date "+%Y%m%d %H:%M:%S") $1" >> $logfile
continue
}
#一.Linux挂载的函数
function LinuxMount()
{
#先判断目录,不通的打印出来;时间限制2s;
local mutSentence=$1
#如果最后是以斜杠结尾,把斜杠去掉
if [[ $mutSentence == */ ]]; then mutSentence=`echo -n $mutSentence | sed 's/.$//'`; fi
local mpoint=`echo -n $mutSentence | awk '{print $NF}'`
if [[ ! -d $mpoint ]]; then
PromLog "${mpoint} mount point not exist."
fi
#是否挂载过
mount | grep $mpoint >> $logfile 2>&1
if [[ $? -eq 0 ]]; then
PromLog "$mpoint is already mounted, ignore."
fi
#showmount地址
local ip=`echo -n $mutSentence|awk -F ":" '{print $1}'|awk -F " " '{print $2}'` #输出ip地址(注:-n不能去掉,否则会执行两次,会失败)
timeout -k 1 1 showmount -e $ip >> $logfile 2>&1 #2秒后如果进程还存在,就是要-k参数强制杀死。
if [[ $? -ne 0 ]]; then
PromLog "${ip} showmount failed."
fi
#开始挂载;若2秒后如果挂载失败,中止,打印出来.
timeout -k 2 2 $mutSentence >> $logfile 2>&1
local result=$?
if [[ "$result" != 0 ]]; then
PromLog "Exec failed: ${mutSentence}"
fi
}
#二.windows挂载的函数
function WindowsMount()
{
local mutSentence=$1
#如果最后是以斜杠结尾,把斜杠去掉
if [[ $mutSentence == */ ]]; then mutSentence=`echo -n $mutSentence | sed 's/.$//'`; fi
#判断挂载点目录是否存在
local mpoint=`echo -n $mutSentence | awk '{print $NF}'` #NF最后一列
if [[ ! -d $mpoint ]]; then
PromLog "${mpoint} mount point not exist."
fi
#是否挂载过
mount | grep $mpoint >> $logfile 2>&1
if [[ $? -eq 0 ]]; then
PromLog "$mpoint is already mounted, ignore."
fi
#判断端口连通性
local ip=`echo -n $mutSentence | awk '{print $6}' | awk -F "/" '{print $3}'` #输出ip地址(注:-n 不能去掉,否则会执行两次,会失败)
nc -v -z -w 1 $ip 445 >> $logfile 2>&1 #超时时间1秒
if [[ ! $? -eq 0 ]]; then
PromLog "$ip port probe failed"
fi
#开始挂载
timeout -k 2 2 $mutSentence >> $logfile 2>&1
local result=$?
if [[ "$result" != 0 ]]; then
PromLog "Exec failed: ${mutSentence}"
fi
}
#三、先筛选linux、windows机器,然后进入相应的挂载函数
for file in $filelist
do
#a,筛选出linux机器,然后执行linux挂载函数;read每次读取一行;行中的单引号替换掉,否则会报错,所以密码中不能包含单引号,单引号也不属于特殊字符
cat $file | grep -v "^#" | grep :/ | sed "s/[';]//g" | while read r
do
#$r参数里面包含空格,一定要加上双引号
LinuxMount "$r"
done
#b,筛选出windows机器,然后执行windows挂载函数;read每次读取一行;行中的单引号替换掉,否则会报错
cat $file | grep // | grep -v "^#" | sed "s/[';]//g" | while read r
do
#$r参数里面包含空格,一定要加上双引号
WindowsMount "$r"
done
done
四、效果展示
[root@node3 ~]# cat mount_sftp_file.sh #写挂载语句的文件
mount 127.0.0.1:/tmp/a /tmp/a1;
mount 127.0.0.2:/tmp/a /tmp/a1;
mount 127.0.0.2:/tmp/a /tmp/a1;
mount 127.0.0.2:/tmp/a /tmp/a1
[root@node3 ~]# ./batch_mount.sh #一行一行挂载
127.0.0.1 showmount failed.
127.0.0.2 showmount failed.
127.0.0.2 showmount failed.
127.0.0.2 showmount failed.
/home/sftp/custom/riuxintiansuan/121.32 mount point not exist.
10.36.120.61 port probe failed
[root@node3 ~]# tail /var/log/mount-20220610.log -n 30 #日志文件
20220610 07:30:05 /home/sftp/custom/riuxintiansuan/121.32 mount point not exist.
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connection timed out.
20220610 07:30:06 10.36.120.61 port probe failed
BEGIN
timeout: failed to run command ‘showmount’: No such file or directory
20220610 07:30:12 127.0.0.1 showmount failed.
timeout: failed to run command ‘showmount’: No such file or directory
20220610 07:30:12 127.0.0.2 showmount failed.
timeout: failed to run command ‘showmount’: No such file or directory
20220610 07:30:12 127.0.0.2 showmount failed.
timeout: failed to run command ‘showmount’: No such file or directory
20220610 07:30:12 127.0.0.2 showmount failed.
20220610 07:30:12 /home/sftp/custom/riuxintiansuan/121.32 mount point not exist.
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connection timed out.
20220610 07:30:13 10.36.120.61 port probe failed