[工具] Shell 开放所有正处于监听状态的端口策略 (firewalld 版)

介绍

基本信息

作者:朱明宇
名称:开放所有正处于监听状态的端口策略
作用:开放所有正处于监听状态的端口策略

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本

脚本

#!/bin/bash

systemctl start firewalld
systemctl enable firewalld

for k in `ss -ntulap |grep 0.0.0.0 | grep LISTEN | awk '{print $5}' | awk -F':' '{print $2}'`
do 
        firewall-cmd --add-port=$k/tcp --permanent
done

firewall-cmd --reload

[工具] Shell 批量对多个 IP 地址开发多个端口策略 (firewalld 版)

介绍

基本信息

作者:朱明宇
名称:批量对多个 IP 地址开发多个端口策略
作用:批量对多个 IP 地址开发多个端口策略

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

1.ips=”192.168.2.1 192.168.1.0/24″ #要开放端口的 IP 地址
2.ports”22 3306 8080-8090″ #要开放的端口

脚本

#!/bin/bash

####################### Separator ########################

ips="192.168.2.1 192.168.1.0/24"
ports"22 3306 8080-8090"

####################### Separator ########################

systemctl start firewalld
systemctl enable firewalld

for i in $ips
do
        for j in $ports
        do
                firewall-cmd --add-rich-rule="rule family="ipv4" source address="$i" port protocol="tcp" port="$j" accept" --permanent
        done
        echo $i done
        echo
done

firewall-cmd --reload

[工具] Shell 批量检测某一个软件包的安装情况 (通过读取和生成 CSV 文件实现)

介绍

使用方法

1. 将此脚本和 patch.cs 文件放在同一目录下
2. patch.cs 里每个 IP 地址和每个 RPM 包名称占用一行
3. 给此脚本添加执行权限
4. 执行此脚本
5. 此脚本执行完成后,会将运行结果写入当前目录下的 checkout.csv 里

注意

此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器

补充

patch.cs 示例如下:

192.168.100.101,kernel-4.18.0-80.el8.x86_64,other0
192.168.100.102,kernel-4.18.0-80.el8.x86_64,other1
192.168.100.103,kernel-4.11.0-80.el8.x86_64,other2
192.168.100.104,kernel-4.18.0-80.el8.x86_64,other3
192.168.100.105,kernel-4.18.0-80.el8.x86_64,other4

脚本

#!/bin/bash

for i in `seq 1 $(cat patch.csv | wc -l)`
do

        servername=`sed -n "$[i]p" patch.csv | cut -d ',' -f 1`
        software=`sed -n "$[i]p" patch.csv | cut -d ',' -f 2`

        ssh $servername "rpm -qa | grep $software" &> /dev/null

        if [ $? -eq 0 ];then
                echo "$servername,$software,have not patched"
        else
                echo "$servername,$software,have patched"
        fi

done

[工具] Shell 显示使用 swap 的进程

介绍

基本信息

作者:朱明宇
名称:显示使用 swap 的进程
作用:显示使用 swap 的进程

使用方法

1. 给此脚本添加执行权限
2. 执行此脚本
3. 执行结果会输出到脚本同目录下的 swapcheck.txt 目录中

脚本

#!/bin/bash

echo > swapcheck.txt

for pid in `ls /proc/ | egrep ^[0-9] | awk '$0 > 100'`
do 

        ls /proc/$pid &> /dev/null
        
        if [ $? -ne 0 ];then
                continue
        fi

        size=`awk '/Swap:/{a=a+$2}END{print a}' /proc/$pid/smaps` 
        name=`ps -aux | egrep $pid`

        if [ -z $size ];then
                continue
        fi

        if [ $size -eq 0 ];then
                continue
        fi

        echo "$[size]k $pid \"$name\"" >> swapcheck.txt
        echo  >> swapcheck.txt

done

[工具] Shell 在 MariaDB & MySQL 的配置文件里设置部分安全策略

介绍:

作者:朱明宇
名称:在 MariaDB & MySQL 的配置文件里设置部分安全策略
作用:在 MariaDB & MySQL 的配置文件里设置部分安全策略

使用方法:
1. 确认 MariaDB & MySQL 已提前装好
2. 在此脚本的分割线内写入相应的内容
3. 给此脚本增加执行权限
4. 执行此脚本

脚本:

#!/bin/bash

#At the system level, start the database as a MySQL user
chown -R mysql /var/lib/mysql
sed -i '/^user=/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i '/^datadir/a user=mysql' /etc/my.cnf.d/mariadb-server.cnf

#Disable client local data reading at the system level
sed -i '/^local-infile=/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i '/^datadir/a local-infile=0' /etc/my.cnf.d/mariadb-server.cnf

#At the system level, remote login of database is prohibited
sed -i '/^bind-address=/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i '/^datadir/a bind-address=127.0.0.1' /etc/my.cnf.d/mariadb-server.cnf

#Restart database
systemctl restart mariadb ; systemctl restart mysql