示例:收集系统的CPU、内存、磁盘等信息,并输出到文件。
#!/bin/bash
# 获取CPU信息
cpu_info=$(cat /proc/cpuinfo)
# 获取内存信息
memory_info=$(free -m)
# 获取磁盘信息
disk_info=$(df -h)
# 输出到文件
echo "$cpu_info" > system_info.txt
echo "$memory_info" >> system_info.txt
echo "$disk_info" >> system_info.txt
echo "System information collected and saved to system_info.txt"
#!/bin/bash
echo "System Information"
# CPU信息
cpu_info=$(cat /proc/cpuinfo | grep "model name" | head -n 1 | awk -F ': ' '{print $2}')
echo "CPU: $cpu_info"
# 内存信息
memory_info=$(free -h | awk 'NR==2{print $2}')
echo "Memory: $memory_info"
# 磁盘信息
disk_info=$(df -h | awk '/^/dev/{print $1, $2}')
echo "Disk: $disk_info"
示例:分析日志文件中的错误信息,并统计出现次数。
#!/bin/bash
# 日志文件路径
log_file="/var/log/app.log"
# 分析错误信息
error_count=$(grep -c "ERROR" "$log_file")
echo "Error count in $log_file: $error_count"
#!/bin/bash
echo "Error Log Analysis"
# 日志文件路径
log_file="/var/log/nginx/error.log"
# 分析错误日志
error_count=$(grep -c "error" "$log_file")
echo "Error Count: $error_count"
# 获取最近的错误记录
recent_errors=$(grep "error" "$log_file" | tail -n 5)
echo "Recent Errors:"
echo "$recent_errors"
示例:备份和恢复配置文件。
#!/bin/bash
# 配置文件路径
config_file="/etc/app.conf"
# 备份配置文件
backup_file="/tmp/app.conf.bak"
# 备份配置文件
cp "$config_file" "$backup_file"
echo "Configuration file backed up to $backup_file"
# 恢复配置文件
cp "$backup_file" "$config_file"
echo "Configuration file restored from $backup_file"
#!/bin/bash
echo "Configuration Management"
# 配置文件路径
config_file="/etc/nginx/nginx.conf"
# 备份配置文件
backup_file="/var/backup/nginx.conf.$(date +%Y%m%d%H%M%S)"
cp "$config_file" "$backup_file"
echo "Config file backed up to: $backup_file"
# 恢复配置文件
restore_file="/var/backup/nginx.conf.20211201090000"
cp "$restore_file" "$config_file"
echo "Config file restored from: $restore_file"
示例:启动、停止和重启系统服务。
#!/bin/bash
# 启动服务
start_command="systemctl start myservice"
$start_command
# 停止服务
stop_command="systemctl stop myservice"
$stop_command
# 重启服务
restart_command="systemctl restart myservice"
$restart_command
echo "Service management completed"
#!/bin/bash
echo "Service Management"
# 启动服务
start_service() {
local service="$1"
systemctl start "$service"
echo "Started service: $service"
}
# 停止服务
stop_service() {
local service="$1"
systemctl stop "$service"
echo "Stopped service: $service"
}
# 重启服务
restart_service() {
local service="$1"
systemctl restart "$service"
echo "Restarted service: $service"
}
# 主程序
service_name="nginx"
start_service "$service_name"
sleep 5
stop_service "$service_name"
sleep 2
restart_service "$service_name"
示例:添加和删除定时任务。
#!/bin/bash
# 添加定时任务
add_task="crontab -l > mycron"
$add_task
echo "*/5 * * * * /path/to/script.sh" >> mycron
crontab mycron
rm mycron
echo "Cron task added"
# 删除定时任务
remove_task="crontab -l | grep -v '/path/to/script.sh' | crontab"
$remove_task
echo "Cron task removed"
#!/bin/bash
echo "Cron Task Management"
# 添加定时任务
add_cron_task() {
local task="$1"
(crontab -l ; echo "$task") | crontab -
echo "Cron task added: $task"
}
# 删除定时任务
remove_cron_task() {
local task="$1"
(crontab -l | grep -v "$task") | crontab -
echo "Cron task removed: $task"
}
# 主程序
cron_task="*/5 * * * * /path/to/script.sh"
add_cron_task "$cron_task"
sleep 10
remove_cron_task "$cron_task"
示例:通过SSH连接到远程服务器并执行命令。
#!/bin/bash
# 远程服务器信息
remote_server="user@remote-server"
# 执行远程命令
remote_command="ls /path/to/files"
ssh "$remote_server" "$remote_command"
#!/bin/bash
# 远程服务器信息
remote_user="username"
remote_host="example.com"
remote_command="ls -l"
# SSH连接并执行命令
ssh -t "$remote_user@$remote_host" "$remote_command"
在这个示例中,我们定义了远程服务器的用户名(remote_user)、主机名(remote_host)和要执行的命令(remote_command)。然后,使用ssh命令进行远程连接,并使用-t选项为SSH分配一个伪终端,以便执行命令。
你可以根据需要修改示例中的远程服务器信息和命令,以满足你的具体需求。还可以在脚本中添加更多的SSH命令,如上传文件、下载文件等。
请注意,为了能够成功连接到远程服务器,你需要确保已经配置了SSH密钥认证或提供了远程服务器的密码。另外,也要确保远程服务器已经允许SSH连接。
示例:检测远程服务器的网络连通性。
#!/bin/bash
# 远程服务器地址
remote_server="remote-server"
# 检测网络连通性
ping -c 3 "$remote_server"
echo "Network connectivity test completed"
示例:修改系统用户的密码策略。
#!/bin/bash
# 修改密码策略
password_policy_file="/etc/login.defs"
sed -i 's/PASS_MAX_DAYS 99999/PASS_MAX_DAYS 90/' "$password_policy_file"
sed -i 's/PASS_MIN_DAYS 0/PASS_MIN_DAYS 7/' "$password_policy_file"
sed -i 's/PASS_WARN_AGE 7/PASS_WARN_AGE 14/' "$password_policy_file"
echo "Password policy updated"
页面更新:2024-05-19
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号