shell脚本练习

1、通过两个文件passwd     username建立用户:

#!/bin/bash
USERNUM=$(wc -l /jiaoben/username | cut -d " " -f 1)
PASSNUM=$(wc -l /jiaoben/password | cut -d " " -f 1)
UNUM=$(wc -l /etc/passwd | cut -d " " -f 1)

count=0

if [ $USERNUM -le $PASSNUM ]
then
    for NUM in $(seq 1 $USERNUM)
    do
            USER=`sed-n ${NUM}p /jiaoben/username`
            PASSWD=`sed-n ${NUM}p /jiaoben/password`
        id $USER &> /dev/null
        if [ "$?" ="0" ]
        then
                echo"${USER}
已经存在"
        else
            useradd $USER -p $PASSWD&> /dev/null
                       count=$((count+1))
        fi
    done
else
    for NUM in $(seq 1 $PASSNUM)
        do
        USER=`sed -n ${NUM}p/jiaoben/username`
                PASSWD=`sed -n ${NUM}p/jiaoben/password`
                id $USER &>/dev/null
                if [ "$?" ="0" ]
                then
                        echo "${USER}
已经存在"
                else
                        useradd $USER -p$PASSWD &> /dev/null
            count=$((count+1))
                fi
        done
fi
echo  "
共建立了${count}个用户"
 

2.自动备份数据库:

#!/bin/bash
read -p "
请输入要登陆的用户名:" USER
read -p "
请输入登陆密码:" -s PASSWD
mysql -u$USER -p$PASSWD -e "SHOW DATABASES;" &>/dev/null
if [ "$?" != "0" ]
then
    echo -e "\n
您输入的用户名或密码有误。"
    exit
else   
    mysql -u$USER -p$PASSWD -e "SHOWDATABASES;"
    read -p "
请输入要备份的数据库:"  DBNAME
    read -p "
请输入要备份的路径(/XXX/XXX.sql:"  PP  

mysqldump -u$USER -p$PASSWD $DBNAME > $PP
fi
if [ "$?" = "0" ]
then
    echo "
备份完成。"
else
    echo "
备份失败。"
fi
 

3.自动ssh连接:

**********

ssh.sh:

*********

#!/bin/bash
read -p "Please input ip:" IP
read -p "Please input user:" USER
read -p "Please input password:" PASSWD
ssh $USER@$IP
 

**************

ssh.exp:

**************

#!/usr/bin/expect


set IP [ lindex $argv 0 ]
set USER [ lindex $argv 1 ]
set PASSWD [ lindex $argv 2 ]

spawn ./ssh.sh
expect {
     "continue connecting(yes/no)?"
        {   send"yes\n";
            exp_continue
        }
     "ip:"
        {   send"$IP\n";
            exp_continue
        }
     "user:"
        {
            send"$USER\n";
            exp_continue
        }
     "password:"
        {   send"$PASSWD\n";
            exp_continue
        }
expect eof
}
interact
 

4.ddns花生壳服务器自动配置(有yum源镜像)


#!/bin/bash
yum install bind -y
systemctl start named

###
修改防火墙
firewall-cmd --permanent --add-service=dns
firewall-cmd --reload

rm -fr Kwestos.*
##
生成密钥名字为westos
dnssec-keygen -a HMAC-MD5 -b 128 -n HOST westos
KEY=`sed -n 1p Kwestos.*.key | cut -d " " -f 7`

##
修改dns配置文件
#############################
sed s/127.0.0.1/any/ /etc/named.conf -i
sed s/localhost/any/ /etc/named.conf -i
cat >> /etc/named.conf <<END
key "westos" {       
            algorithm hmac-md5;
            secret"$KEY";
             };
END
###############################





##
配置正向解析文件 westoslinux.com.zone
##################################################
cat > /var/named/westoslinux.com.zone <<END
\$TTL 1D
@       IN SOA   dns.westoslinux.com. root.westoslinux.com.(
                                            0       ; serial
                                           1D      ; refresh
                                           1H      ; retry
                                           1W      ; expire
                                            3H )    ; minimum
                NS      dns.westoslinux.com.
dns             A       172.25.254.112
www             A       172.25.254.222
END

#############################################





##
配置named.rfc1912.zones
###############################################
cat >> /etc/named.rfc1912.zones <<END
zone "westoslinux.com" IN {
                 type master;
                 file"westoslinux.com.zone";
                 allow-update { keywestos; };
                 allow-transfer {172.25.254.212;};
                 also-notify     {172.25.254.212;};
                  };
END
###############################################
setsebool -P named_write_master_zones 1
chmod 770 /var/named
systemctl restart named



yum install dhcp -y &> /dev/null

##
修改dhcp配置文件
###########################################
cat > /etc/dhcp/dhcpd.conf <<END
option domain-name "westoslinux.com";
option domain-name-servers 172.25.254.212;
default-lease-time 600;
max-lease-time 7200;
ddns-update-style interim;
log-facility local7;
subnet 172.25.254.0 netmask 255.255.255.0 {
  range 172.25.254.13172.25.254.99;
  option routers 172.25.254.212;
}
key westos {
        algorithm hmac-md5;
        secret "$KEY";
};
zone westoslinux.com. {
    primary 127.0.0.1;
    key westos;
}
END
###########################################
systemctl restart dhcpd

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值