日常脚本分享(持续更新中...)

centos7/8系统初始化脚本(懒人福音)

#! /bin/bash

VERSION_ID=$(grep -oP 'VERSION_ID="\K\d+' /etc/os-release)
DIR=/etc/yum.repos.d
file_path=/etc/chrony.conf


update_yum() {
    cd $DIR && rm -rf * && yum clean all
    if [ "$VERSION_ID" -eq 7 ]; then
        curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    else
        curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
    fi
    echo "阿里云yum源配置成功!!!" && sleep 2s
}

# 关闭防火墙和SELINUX
firewalld() {
    systemctl stop firewalld && systemctl disable firewalld &> /dev/null
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    echo "SElinux已禁用,重新启动后才可生效" && sleep 2s
}

# 修改主机名称
set_ps1() {
    echo "PS1='\[\e[35m\][\u@\h \W]#\[\e[m\] '" >> /root/.bashrc
    echo "提示符已修改成功,请重新登录生效" && sleep 2s
}


#设置时间同步,为确保某些机器无法通过正常修改时区实现时间同步所以这里直接修改配置文件

datetime() {
    if [ -e "$file_path" ]; then
        echo "chrony服务已经安装,正在配置"
    else
        yum -y install chrony &> /dev/null
        echo "chrony 已成功安装" && sleep 2s
    fi
    cat <<EOF > /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html)
# 国家服务器
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
# 阿里
server ntp.aliyun.com
# 腾讯
server time1.cloud.tencent.com
server time2.cloud.tencent.com
server time3.cloud.tencent.com
server time4.cloud.tencent.com
server time5.cloud.tencent.com
# if its offset is larger than 1 second.
makestep 1.0 3
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
# Specify directory for log files.
logdir /var/log/chrony 
EOF
    timedatectl set-timezone Asia/Shanghai && systemctl restart chronyd
    echo "时间同步已同步完成" && sleep 2s
}

# 修改epel源
update_epel() {
    yum clean all &> /dev/null
    sudo sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
    -e 's!https\?://download\.example/pub/epel!https://mirrors.tuna.tsinghua.edu.cn/epel!g' \
    -i /etc/yum.repos.d/epel*.repo
    echo "epel源配置完毕" && sleep 2s
}

yum_epel() {
    if [ -e /etc/yum.repos.d/epel*.repo ]; then
        update_epel
    else
        yum remove -y epel-release &> /dev/null && yum -y install epel-release &> /dev/null
        update_epel
    fi
}

# 安装一些常用软件
yum_install(){
    yum update &> /dev/null
    echo "正在安装常用软件..."
    yum -y install vim wget tree net-tools lrzsz zip lsof tcpdump  &> /dev/null
    echo "常用软件安装完成,所有配置已就绪,系统将在5秒后重启..." && sleep 5s && reboot
}

start(){
    update_yum
    firewalld
    set_ps1
    datetime
    yum_epel
    yum_install
}

start

生产案例:将指定目录下的文件所有文件的后缀改名为bak后缀

#!/bin/bash
DIR=/data/test
cd $DIR || {    echo 无法进入   $DIR;exit 1; }
for FILE in * ;do
    PRE=`echo $FILE | grep -Eo ".*\."`
    mv $FILE ${PRE}bak
    PRE=`echo $FILE|rev|cut -d. -f 2-|rev`
    PRE=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\1/p'`
    SUFFIX=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\2/p'`
    mv $FILE $PRE.bak
done

批量创建用户并设置随机密码

#!/bin/bash
for i in {1..10};do
    useradd user$i
    PASS=`cat /dev/urandom | tr -dc '[:alnum:]' | head -c12`
    echo $pass | passwd --stdin user$i &> /dev/null
    echo user$i:$PASS >> /data/user.log
    echo "user$i is created"
done

配置虚拟机代理的脚本,执行一次开启代理,再次执行关闭代理

配置虚拟机代理访问

#!/bin/bash

file="/etc/profile.d/proxy.sh"

# 检查文件是否存在,如果不存在则创建
if [ ! -f "$file" ]; then
    sudo touch "$file"
    echo "代理文件不存在,已创建。"
fi

# 检查文件是否为空
if [ ! -s "$file" ]; then
    # 文件为空,引导用户输入物理机 IP
    echo "代理文件为空,请输入您的物理机 IP 地址:"
    read IP

    # 写入变量到文件
    sudo tee "$file" >/dev/null <<EOF
export http_proxy="http://$IP:7891"
export https_proxy="http://$IP:7891"
export no_proxy="localhost,192.168.1.0/24,192.168.31.0/24,127.0.0.1"
EOF

    # 使文件中的变量生效
    source "$file"
    echo -e "\e[32m已开启代理服务           [OK]\e[0m"
else
    # 文件不为空,清空文件并使其生效
    sudo echo -n > "$file"

    # 使文件为空生效
    source "$file"
    echo -e "\e[31m已关闭代理服务          [STOP]\e[0m"
fi

测试虚拟机代理访问是否开启成功

#!/bin/bash


#通过curl命令来判断虚拟机是否可以正常与Google通信
existing_http_proxy=$http_proxy

curl --proxy $existing_http_proxy --output /dev/null --silent --head --fail http://www.google.com

if [ $? -eq 0 ]; then
    echo -e "\e[32m代理配置成功,可以访问Google      [successful]\e[0m"
else
    echo -e "\e[31m代理配置可能存在问题,无法访问Google      [ERROR]\e[0m"
fi

循环访问nginx端口

#!/bin/bash

# NGINX服务器的地址和端口
nginx_url="http://example.com:80"

# 要执行的循环次数
num_requests=10

for ((i=1; i<=num_requests; i++)); do
    # 发送HTTP请求并获取响应状态码
    response_code=$(curl -s -o /dev/null -w "%{http_code}" $nginx_url)
    if [ $response_code -eq 200 ]; then
        # 如果响应状态码为200,则请求成功
        echo "Request $i: Success"
    else
        # 如果响应状态码不为200,则请求失败
        echo "Request $i: Failed (Status Code: $response_code)"
    fi
done

ubuntu系统将DHCP地址改为静态

#!/bin/bash

# 获取当前的网络接口名称
INTERFACE=$(ip -o -4 route show to default | awk '{print $5}')

# 获取当前的IP地址
IP_ADDRESS=$(ip -o -4 addr show $INTERFACE | awk '{print $4}' | cut -d/ -f1)
NETMASK=$(ifconfig $INTERFACE | grep 'netmask' | awk '{print $4}')
GATEWAY=$(ip -4 route show default | awk '{print $3}')
DNS=$(systemd-resolve --status | grep 'DNS Servers' | awk '{print $3}')

# 打印获取到的信息
echo "Interface: $INTERFACE"
echo "IP Address: $IP_ADDRESS"
echo "Netmask: $NETMASK"
echo "Gateway: $GATEWAY"
echo "DNS: $DNS"

# 备份现有的Netplan配置文件
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak

# 创建新的Netplan配置文件,将DHCP改为静态IP
sudo tee /etc/netplan/01-netcfg.yaml > /dev/null << EOF
network:
  version: 2
  ethernets:
    $INTERFACE:
      addresses:
        - $IP_ADDRESS/24
      gateway4: $GATEWAY
      nameservers:
        addresses:
          - $DNS
EOF

# 应用新的Netplan配置
sudo netplan apply

echo "Network configuration has been changed to static IP."

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值