shell脚本实例三

练习一:获得连通主机的ip和hostname
1.脚本编写

vim  checkhost.sh
#!/bin/bash
Auto_conn()
{
/usr/bin/expect << EOF
set timeout 2
spawn ssh root@$1 $2
expect {
"yes/no" {send "yes\r";exp_continue }
"password" {send "westos\r" }
}
expect eof
EOF
}
for i in {75..79}
do
    ping -w1 -c1 172.25.254.$i &>> /dev/null && {
        IP=172.25.254.$i
    hn=`Auto_conn  $IP hostname|grep -E "ECDSA|authenticity|connecting|Warning|password|spawn" -v`
    echo $hn $IP >> /mnt/dns_file    

    sed  's/^M//g ' -i  /mnt/dns_file
    }
done  

2.测试结果 

   

练习二:有分针秒针的倒计时
vim  daojishi1.sh
#!/bin/bash
for ((i=70;i>0;i--))
do
        shi=$((i/60))
        miao=$((i%60))
        echo -ne "\r倒计时:还有$shi分 $miao 秒结束"
        sleep 2
done
echo -ne "\r倒计时:0分 0秒!!"
练习三:两个文件分别为userlist和passlist,每行作为对应的用户和密码并进行创建,并对各种情况做出说明!
[root@desktop5 mnt]# cat users_create.sh
#!/bin/bash
[ "$USER" = "root" ]||{
    echo -e "\033[31mplease use rootuser!!\033[0m"
    exit
}
[ -z "$1" -o -z "$2" ]&&{
    echo -e "\033[31mparamater is  not two!!\033[0m"
    exit
}
[ ! -e "$1" -o ! -e "$2" ]&&{
    echo -e "\033[31mfile  is not exist!!\033[0m"
    exit
}
num1=`wc -l $1|cut -d " " -f 1`
num2=`wc -l $2|cut -d " " -f 1`
[ "$num1" = "$num2" ]&&{
for i in `seq 1 $num1`
do
    user=`sed -n ${i}p $1`
    ps=`sed -n ${i}p $2`
    dge=`getent passwd $user`
    if [ -z  "$dge" ]
    then
    useradd $user
    echo $ps | passwd --stdin $user &> /dev/null
    else
    echo  -e "\033[31m$user is already  exit!!\033[0m"
    fi
done
}||{
    echo -e "\033[31m$1 和 $2 行数不一致!!\033[0m"
    exit
}
练习四:数据库的备份
[root@desktop5 mnt]# cat mariadb.sh
#!/bin/bash
[ -z "$1" ]&&{
    echo nopass
    exit
}

#用来判断mariadb是否安装

DBCHECK=`rpm -qa | grep mariadb-server`
if [  -z "$DBCHECK" ]
then
    echo database software no install!!
    exit 1
fi
#判断是否开启mariadb服务
DB_STATE=`mysql -uroot -p$1 -e "show databases;" 2>&1 | awk '/sock/{print}'`
if [ ! -z "$DB_STATE" ]
then
    echo DB not start!!
    exit 2
fi
#判断密码是否正确
DB_STATE=`mysql -uroot -p$1 -e "show databases;"`
if [ "$?" != "0" ]
then
        echo DB passwd erro!!
        exit 3
fi
#创建目录
until [ -e "/mnt/mysqldump" ]
do
    mkdir -p /mnt/mysqldump
done

for DB_NAME in `mysql -uroot -p$1 -e "show databases;" -NE | grep -E "\*|chema" -v `
do
    [ ! -e "/mnt/mysqldump/${DB_NAME}.sql" ]&&{
    mysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/$DB_NAME.sql
    }||{
    echo "/mnt/mysqldump/${DB_NAME}.sql  is exist you can do this:"
    echo "[S]kip  [B]ackup [O]verwrite"
    read -p "please input action:" ACTION1
    ACTION=`echo $ACTION1 | tr 'a-z' 'A-Z'`
    case $ACTION in
        S)
        echo skip it!!
        ;;
        B)
        mv /mnt/mysqldump/${DB_NAME}.sql /mnt/mysqldump/${DB_NAME}_backup.sql
        mysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/${DB_NAME}.sql
        echo backup file create success!!
        ;;
        O)
        mysqldump -uroot -p$1 $DB_NAME > /mnt/mysqldump/${DB_NAME}.sql
        echo overwrite is ok!!
        ;;
        *)
        echo no sign
    esac
    }
done

posted @ 2018-08-25 15:11  UTHN_B  阅读(240)  评论(0编辑  收藏  举报