shell_常用脚本.sh

shell_常用脚本.sh


##### 基础脚本 ######################### 
##### 01. 同时读取两个脚本 ##############
#!/bin/bash
#同时读取两个文件进行循环
exec 3<"1.txt"
exec 4<"2.txt"

while read line1<&3 && read line2<&4
do
        echo $line1 $line2
EOF
done


##### 02. 读取两个文件循环 批量修改文件 ##################
#!/bin/bash
# 读取两个文件循环 批量修改文件
# 可实现读取前5行输出到第一个文件中 再次5行输出到第二个文件中 
# 由于网站不识别utf-8编码 可先使用iconv转码 转成gbk

web_dir=/home/www/wwwroot/
iconv -f utf-8 -t gbk url.txt > url_gbk.txt

while read line1
do
    echo >> $web_dir$line1/data/flink.conf
    y=1

    while read line2
    do
        if [ $y -le 5 ];then
            let y++
        else
            sed -i '1,5d' url_gbk.txt
            break 1
        fi
            echo "$line2" >> $web_dir$line1/data/flink.conf
    done < url_gbk.txt

done < web.txt

echo > url.txt
rm -f url_gbk.txt


###### 03. 检测某一端口是否存活 ############
# 每1分钟检测一次800端口是否存活 自动重新启动vpn手机端服务
# 添加开启启动 echo "nohup /data/shell/shadowsocksr_status.sh &" >> /etc/rc.local
# 手动启动 nohup /data/shell/shadowsocksr_status.sh &
# 2020_12_14_T
while :
do
lsof -i:800 &>/dev/null
if [ $? -eq 0 ]; then
	echo -e "\e[1;35mshadowsocksr is open\e[0m"
else
	echo -e "\e[1;31mshadowsocksr is close\e[0m"
	{
	python /usr/local/shadowsocksr-3.2.1/server.py &
	} &
	echo -e "\e[1;33mshadowsocksr is rsetart\e[0m"
	
fi
sleep 60s
done


##### 04. ping01 脚本 ##########################
# 此脚本会 ping /data/shell/a.txt 里面的所有网址 会循环ping 多线程
# nohup /data/shell/ping.sh >/data/shell/nohup.out 2>&1 &    \\ 后台执行 ping.sh的打印信息会输出到指定目录下的nohup.out中
# 添加开机启动 /etc/profile.d/pings.sh  --> #!/bin/bash   nohup /data/shell/ping.sh >/data/shell/nohup.out 2>&1 &
# killall ping.sh 停止脚本 killall 的包是psmisc 可以 yum install psmisc安装
for ((n = 1;n >0;n++))
do
while read IP
do 
    { 
    ping $IP -c2 -s1 2>&1 1>>/dev/null && echo -e $IP is "\033[32;49;1malive\033[39;49;0m" || echo -e $IP is "\033[31;49;1mdown\033[39;49;0m" 
    } & 
done < /data/shell/a.txt
wait 
done


##### 05. ping02 脚本 ##############################
##### 检测局域网内所有的ip是否在线 多线程 ######
for i in $(seq 1 254)
do
    {
    IP="192.168.10.$i"
    ping $IP -c2 -s1 2>&1 1>>/dev/null && echo -e $IP is "\033[32;49;1malive\033[39;49;0m" || echo -e $IP is "\033[31;49;1mdown\033[39;49;0m" 
    } &
done

wait

echo "所有ip检测完毕"


##### 06. 检测nginx是否存活 重启nginx ###########
# 检测nginx 是否存活
# 运行脚本使用此命令后台 nohup /data/shell/nginx_status.sh &
# 添加开机启动 echo "nohup /data/shell/nginx_status.sh &" >> /etc/rc.local
# 2020_11_03_teo
while :
do
ss -tnl | grep ":80" &>/dev/null
if [ $? -eq 0 ]; then
	echo -e "\e[1;35mnginx is open\e[0m"
else
	echo -e "\e[1;31mnginx is close\e[0m"
	systemctl restart nginx
	echo -e "\e[1;33mnginx is rsetart\e[0m"
	
fi
sleep 10s
done


##### 07. 批量添加防火墙规则 #######################
# 批量添加删除防火墙规则 端口存放在 同目录下的ipt.txt内 
while read line
do
iptables -D IN_public_allow -p tcp --dport $line -m conntrack --ctstate  NEW,UNTRACKED -j ACCEPT
iptables -D IN_public_allow -p udp --dport $line -m conntrack --ctstate  NEW,UNTRACKED -j ACCEPT
done < ipt.txt


##### 08. 脚本跳板机 ###########################
# 脚本跳机

trap "" HUP INT OUIT TSTP
web1=192.168.10.10
web2=192.168.10.11
clear

while :
do
cat <<-EOF
+-----------------------------------+
|       This is a jumpserver        |
|       1. web1		     |
|       2. web2                     |
|       3. mysql1                   |
|       4. mysql2                   |
+-----------------------------------+
EOF

echo -en "\e[1;32minput number: \e[0m"
read num
case "$num" in
1)
	ssh jump@$web1
	;; 
2)
	ssh jump@$web2
	;;
3)
	ssh alice@$mysql1
	;;
4)
	ssh alice@$mysql2
	;;
"")
	;;
*)
	echo "error"
esac

done


##### 09. 禁止海外ip访问 添加白名单####################
#防火墙 添加ip白名单 可以限制国外的ip访问
#国外ip汇总在 ip段所有国家.zip 地址 https://www.ssyy99.com/download/ip段所有国家.zip
#先合并ip cat *.zone > white_ip.zone
iptables -F
iptables -X
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

while read ip
do
	
	iptables -A INPUT -s $ip -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
	echo $i
	let i++

done < white_ip.zone
wait

#iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited
echo "iptables 已添加所有要允许的ip"


##### 10. 批量下载脚本 #################################
#!/bin/bash
#批量下载脚本
# url格式为   即可 保存到 a.txt
# https://basicenglishspeaking.com/wp-content/uploads/2016/audio/ce/E-26.mp3
# https://basicenglishspeaking.com/wp-content/uploads/2016/audio/ce/E-27.mp3
mydir=`pwd`
while read line
do
{
    if [ -n "$line" ]
    then
        cd $mydir
        url=$(echo "$line" | tr -d '\r')
        picdir=$(echo $url | sed -r 's/http:\/\///g')
        picname=$(echo ${picdir##*/})
        picpath=$(echo ${picdir%/*})
        mkdir -p $picpath
        cd $picpath
        wget -O $picname `echo $url`
    fi
}
done < a.txt
exit 0


##### 11. 统计 单词出现的次数 #################################
#!/bin/sh
while read line 
do
    temp=`cat z.txt | grep -E "^$line "`
    if [ $? -eq 0 ];then
            i=`echo $temp | cut -d ' ' -f 2`
            let i++
            sed -i "/^$line/c$line $i" z.txt
    else
            echo $line 1 >> z.txt
    fi
done < a.txt

Teo

You must be logged in to post a comment