Shell基础-5
Tutorial: Shell基础
Category: Shell
Published: 2026-04-07 13:58:25
Views: 23
Likes: 0
Comments: 0
Shell 基础-5
- 生成秘钥文件
#!/bin/bash
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh-keygen -t RSA -N '' -f ~/.ssh/id_rsa
- 检查软件包是否安装
#!/bin/bash
if [ $# -eq 0 ]; then
echo "你需要制定一个软件包名称作为脚本参数"
echo "用法:$0 软件包名称 ..."
fi
for package in "$@"; do
if rpm -q ${package} &>/dev/null; then
echo -e "${package}\e[32m 已经安装\e[0m"
else
echo -e "${package}\e[34;1m 未安装\e[0m"
fi
done
- 使用 curl 监控 http 返回码
#!/bin/bash
url=http://192.168.4.5/index.html
check_http() {
status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url)
}
while :; do
check_http
if [ $status_code -ne 200 ]; then
date=$(date +%Y%m%d-%H:%M:%S)
echo "当前时间为:$date $url 服务器异常,状态码为${status_code}.请尽快排查异常." >/tmp/http$.pid
mail -s Warning root </tmp/http$.pid
else
echo "$url 连接正常" >>/var/log/http.log
fi
sleep 5
done
- 防火墙简单操作
#!/bin/bash
service="nfs http ssh"
port="80 22 8080"
for i in $service; do
echo "Adding $i service to firewall"
firewall-cmd --add-service=${i}
done
for i in $service; do
echo "Adding $i Port to firewall"
firewall-cmd --add-port=${i}/tcp
done
firewall-cmd --runtime-to-permanent
- 自动创建逻辑卷
#!/bin/bash
clear
echo -e "\033[32m!!!!!!警告(Warning)!!!!!!\033[0m"
echo
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
echo "脚本会将整个磁盘转换为 PV,并删除磁盘上所有数据!!!"
echo "This Script will destroy all data on the Disk"
echo "+++++++++++++++++++++++++++++++++++++++++++++++++"
echo
read -p "请问是否继续 y/n?:" sure
[ $sure != y ] && exit
read -p "请输入磁盘名称,如/dev/vdb:" disk
[ -z $disk ] && echo "没有输入磁盘名称" && exit
read -p "请输入卷组名称:" vg_name
[ -z $vg_name ] && echo "没有输入卷组名称" && exit
read -p "请输入逻辑卷名称:" lv_name
[ -z $lv_name ] && echo "没有输入逻辑卷名称" && exit
read -p "请输入逻辑卷大小:" lv_size
[ -z $lv_size ] && echo "没有输入逻辑卷大小" && exit
pvcreate $disk
vgcreate $vg_name $disk
lvcreate -L ${lv_size}M -n ${lv_name} ${vg_name}