编写 hello world 脚本
1 2 3 4 5 #!/bin/bash echo "Hello World!"
通过位置变量创建 Linux 系统账户及密码
1 2 3 4 5 6 7 #!/bin/bash useradd "$1 " echo "$2 " | passwd ‐‐stdin "$1 "
备份日志
1 2 3 4 5 6 7 8 9 #!/bin/bash tar -czf log -`date +%Y%m%d`.tar.gz /var/log 00 03 * * 5 /root/logbak.sh
一键部署 LNMP(RPM 包版本)
1 2 3 4 5 6 7 8 9 10 #!/bin/bash yum ‐y install httpd yum ‐y install mariadb mariadb‐devel mariadb‐server yum ‐y install php php‐mysql systemctl start httpd mariadb systemctl enable httpd mariadb
监控内存和磁盘容量,小于给定值时报警
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash disk_size=$(df / | awk '/\//{print $4}' ) mem_size=$(free | awk '/Mem/{print $4}' ) while : do if [ $disk_size -le 512000 -a $mem_size -le 1024000 ] then mail ‐s "Warning" root <<EOF Insufficient resources,资源不足 EOF fi done
猜数字游戏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/bin/bash num=$[RANDOM%100+1] echo "$num " while : do read -p "计算机生成了一个 1‐100 的随机数,你猜: " cai if [ $cai -eq $num ] then echo "恭喜,猜对了" exit elif [ $cai -gt $num ] then echo "Oops,猜大了" else echo "Oops,猜小了" fi done
检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用字串对比版本)
1 2 3 4 5 6 7 8 9 10 #!/bin/bash if [ $USER == "root" ] then yum ‐y install vsftpd else echo "您不是管理员,没有权限安装软件" fi
检测本机当前用户是否为超级管理员,如果是管理员,则使用 yum 安装 vsftpd,如果不是,则提示您非管理员(使用 UID 数字对比版本)
1 2 3 4 5 6 7 8 9 #!/bin/bash if [ $UID -eq 0 ];then yum ‐y install vsftpd else echo "您不是管理员,没有权限安装软件" fi
编写脚本:提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码。如果用户不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默认的 123456 作为默认密码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #!/bin/bash read -p "请输入用户名: " user if [ -z $user ];then echo "您不需输入账户名" exit 2 fi stty -echo read -p "请输入密码: " pass stty echo pass=${pass:‐123456} useradd "$user " echo "$pass " | passwd ‐‐stdin "$user "
输入三个数并进行升序排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #!/bin/bash read -p "请输入一个整数:" num1 read -p "请输入一个整数:" num2 read -p "请输入一个整数:" num3 tmp=0 if [ $num1 -gt $num2 ];then tmp=$num1 num1=$num2 num2=$tmp fi if [ $num1 -gt $num3 ];then tmp=$num1 num1=$num3 num3=$tmp fi if [ $num2 -gt $num3 ];then tmp=$num2 num2=$num3 num3=$tmp fi echo "排序后数据(从小到大)为:$num1 ,$num2 ,$num3 "
石头、剪刀、布游戏
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #!/bin/bash game=(石头 剪刀 布) num=$[RANDOM%3] computer=${game[$num]} echo "请根据下列提示选择您的出拳手势" echo "1.石头" echo "2.剪刀" echo "3.布" read -p "请选择 1‐3:" person case $person in 1) if [ $num -eq 0 ] then echo "平局" elif [ $num -eq 1 ] then echo "你赢" else echo "计算机赢" fi ;; 2) if [ $num -eq 0 ] then echo "计算机赢" elif [ $num -eq 1 ] then echo "平局" else echo "你赢" fi ;; 3) if [ $num -eq 0 ] then echo "你赢" elif [ $num -eq 1 ] then echo "计算机赢" else echo "平局" fi ;; *) echo "必须输入 1‐3 的数字" esac
编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(for 版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash for i in {1..254} do ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.4.$i is up" else echo "192.168.4.$i is down" fi done
编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(while 版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash i=1 while [ $i -le 254 ] do ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.4.$i is up" else echo "192.168.4.$i is down" fi let i++ done
编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(多进程版)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash myping (){ ping ‐c 2 ‐i 0.3 ‐W 1 $1 &>/dev/null if [ $? -eq 0 ];then echo "$1 is up" else echo "$1 is down" fi } for i in {1..254} do myping 192.168.4.$i & done
编写脚本,显示进度条
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash jindu (){ while : do echo -n '#' sleep 0.2 done } jindu & cp -a $1 $2 killall $0 echo "拷贝完成"
进度条,动态时针版本;定义一个显示进度的函数,屏幕快速显示| / ‐ \
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #!/bin/bash rotate_line (){ INTERVAL=0.5 COUNT="0" while : do COUNT=`expr $COUNT + 1` case $COUNT in "1" ) echo -e '‐' "\b\c" sleep $INTERVAL ;; "2" ) echo -e '\\' "\b\c" sleep $INTERVAL ;; "3" ) echo -e "|\b\c" sleep $INTERVAL ;; "4" ) echo -e "/\b\c" sleep $INTERVAL ;; *) COUNT="0" ;; esac done } rotate_line
9*9 乘法表
1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash for i in `seq 9` do for j in `seq $i ` do echo -n "$j *$i =$[i*j] " done echo done
使用死循环实时显示 eth0 网卡发送的数据包流量
1 2 3 4 5 6 7 8 9 10 #!/bin/bash while : do echo '本地网卡 eth0 流量信息如下: ' ifconfig eth0 | grep "RX pack" | awk '{print $5}' ifconfig eth0 | grep "TX pack" | awk '{print $5}' sleep 1 done
使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码本脚本执行,需要提前准备一个 user.txt 文件,该文件中包含有若干用户名信息
1 2 3 4 5 6 7 8 9 #!/bin/bash for i in `cat user.txt` do useradd $i echo "123456" | passwd ‐‐stdin $i done
编写批量修改扩展名脚本
1 2 3 4 5 6 7 8 9 10 #!/bin/bash for i in `ls *.$1 ` do mv $i ${i%.*} .$2 done
使用 expect 工具自动交互密码远程其他主机安装 httpd 软件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash rm ‐rf ~/.ssh/known_hosts expect <<EOF spawn ssh 192.168.4.254 expect "yes/no" {send "yes\r"} # 根据自己的实际情况将密码修改为真实的密码字串 expect "password" {send "密码\r"} expect "#" {send "yum ‐y install httpd\r"} expect "#" {send "exit\r"} EOF
一键部署 LNMP(源码安装版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 #!/bin/bash menu () { clear echo " ##############‐‐‐‐Menu‐‐‐‐##############" echo "# 1. Install Nginx" echo "# 2. Install MySQL" echo "# 3. Install PHP" echo "# 4. Exit Program" echo " ########################################" } choice () { read -p "Please choice a menu[1‐9]:" select } install_nginx () { id nginx &>/dev/null if [ $? -ne 0 ];then useradd -s /sbin/nologin nginx fi if [ -f nginx‐1.8.0.tar.gz ];then tar -xf nginx‐1.8.0.tar.gz cd nginx‐1.8.0 yum -y install gcc pcre‐devel openssl‐devel zlib‐devel make ./configure ‐‐prefix=/usr/local/nginx ‐‐with‐http_ssl_module make make install ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ cd .. else echo "没有 Nginx 源码包" fi } install_mysql () { yum -y install gcc gcc‐c++ cmake ncurses‐devel perl id mysql &>/dev/null if [ $? -ne 0 ];then useradd -s /sbin/nologin mysql fi if [ -f mysql‐5.6.25.tar.gz ];then tar -xf mysql‐5.6.25.tar.gz cd mysql‐5.6.25 cmake . make make install /usr/local/mysql/scripts/mysql_install_db ‐‐user=mysql ‐‐datadir=/usr/local/mysql/data/ ‐‐basedir=/usr/local/mysql/ chown -R root.mysql /usr/local/mysql chown -R mysql /usr/local/mysql/data /bin/cp -f /usr/local/mysql/support‐files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld /bin/cp -f /usr/local/mysql/support‐files/my‐default.cnf /etc/my.cnf echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf ldconfig echo 'PATH=\$PATH:/usr/local/mysql/bin/' >> /etc/profile export PATH else echo "没有 mysql 源码包" exit fi } install_php () { yum -y install gcc libxml2‐devel if [ -f mhash‐0.9.9.9.tar.gz ];then tar -xf mhash‐0.9.9.9.tar.gz cd mhash‐0.9.9.9 ./configure make make install cd .. if [ ! ‐f /usr/lib/libmhash.so ];then ln -s /usr/local/lib/libmhash.so /usr/lib/ fi ldconfig else echo "没有 mhash 源码包文件" exit fi if [ -f libmcrypt‐2.5.8.tar.gz ];then tar -xf libmcrypt‐2.5.8.tar.gz cd libmcrypt‐2.5.8 ./configure make make install cd .. if [ ! -f /usr/lib/libmcrypt.so ];then ln -s /usr/local/lib/libmcrypt.so /usr/lib/ fi ldconfig else echo "没有 libmcrypt 源码包文件" exit fi if [ -f php‐5.4.24.tar.gz ];then tar -xf php‐5.4.24.tar.gz cd php‐5.4.24 ./configure ‐‐prefix=/usr/local/php5 ‐‐with‐mysql=/usr/local/mysql ‐‐enable ‐fpm ‐‐ enable ‐mbstring ‐‐with‐mcrypt ‐‐with‐mhash ‐‐with‐config‐file‐path=/usr/local/php5/etc ‐‐with‐ mysqli=/usr/local/mysql/bin/mysql_config make && make install /bin/cp -f php.ini‐production /usr/local/php5/etc/php.ini /bin/cp -f /usr/local/php5/etc/php‐fpm.conf.default /usr/local/php5/etc/php‐fpm.conf cd .. else echo "没有 php 源码包文件" exit fi } while : do menu choice case $select in 1) install_nginx ;; 2) install_mysql ;; 3) install_php ;; 4) exit ;; *) echo Sorry! esac done
编写脚本快速克隆 KVM 虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #!/bin/bash IMG_DIR=/var/lib/libvirt/images BASEVM=rh7_template read -p "Enter VM number: " VMNUM if [ $VMNUM -le 9 ];then VMNUM=0$VMNUM fi if [ -z "${VMNUM} " ]; then echo "You must input a number." exit 65 elif [[ ${VMNUM} =~ [a‐z] ]; then echo "You must input a number." exit 66 elif [ ${VMNUM} -lt 1 -o ${VMNUM} -gt 99 ]; then echo "Input out of range" exit 67 fi NEWVM=rh7_node${VMNUM} if [ -e $IMG_DIR /${NEWVM} .img ]; then echo "File exists." exit 68 fi echo -en "Creating Virtual Machine disk image......\t" qemu‐img create -f qcow2 ‐b $IMG_DIR /.${BASEVM} .img $IMG_DIR /${NEWVM} .img &> /dev/null echo -e "\e[32;1m[OK]\e[0m" cat /var/lib/libvirt/images/.rhel7.xml > /tmp/myvm.xml sed -i "/<name>${BASEVM} /s/${BASEVM} /${NEWVM} /" /tmp/myvm.xml sed -i "/uuid/s/<uuid>.*<\/uuid>/<uuid>$(uuidgen) <\/uuid>/" /tmp/myvm.xml sed -i "/${BASEVM} \.img/s/${BASEVM} /${NEWVM} /" /tmp/myvm.xml sed -i "/mac /s/a1/0c/" /tmp/myvm.xml echo -en "Defining new virtual machine......\t\t" virsh define /tmp/myvm.xml &> /dev/null echo -e "\e[32;1m[OK]\e[0m"
点名器脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash while : do line=`cat user.txt |wc ‐l` num=$[RANDOM%line+1] sed -n "${num} p" user.txt sleep 0.2 clear done
查看有多少远程的 IP 在连接本机
1 2 3 4 5 6 7 8 9 10 #!/bin/bash netstat -atn | awk '{print $5}' | awk '{print $1}' | sort -nr | uniq -c
对 100 以内的所有正整数相加求和(1+2+3+4…+100)
1 2 3 4 5 6 7 8 9 10 #!/bin/bash sum =0 for i in `seq 100` do sum =$[sum +i] done echo "总和是:$sum "
统计 13:30 到 14:30 所有访问 apache 服务器的请求有多少个
1 2 3 4 5 6 7 8 #!/bin/bash awk -F "[ /:]" '$7":"$8>="13:30" && $7":"$8<="14:30"' /var/log/httpd/access_log |wc -l
统计 13:30 到 14:30 所有访问本机 Aapche 服务器的远程 IP 地址是什么
1 2 3 4 5 6 7 #!/bin/bash awk -F "[ /:]" '$7":"$8>="13:30" && $7":"$8<="14:30"{print $1}' /var/log/httpd/access_log
打印国际象棋棋盘
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/bin/bash for i in {1..8} do for j in {1..8} do sum =$[i+j] if [ $[sum %2] -eq 0 ];then echo -ne "\033[46m \033[0m" else echo -ne "\033[47m \033[0m" fi done echo done
统计每个远程 IP 访问了本机 apache 几次?
1 2 3 4 #!/bin/bash awk '{ip[$1]++}END{for(i in ip){print ip[i],i}}' /var/log/httpd/access_log
统计当前 Linux 系统中可以登录计算机的账户有多少个
1 2 3 4 5 6 7 #!/bin/bash grep "bash$" /etc/passwd | wc -l awk -f: '/bash$/{x++}end{print x}' /etc/passwd
统计/var/log 有多少个文件,并显示这些文件名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash cd /var/log sum =0 for i in `ls -r *` do if [ -f $i ];then let sum ++ echo "文件名:$i " fi done echo "总文件数量为:$sum "
自动为其他脚本添加解释器信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Docker+K8s+Jenkins 主流技术全解视频资料【干货免费分享】 if ! grep -q "^#!" $1 ; then sed '1i #!/bin/bash' $1 sed '2i #Description: ' fi vim +2 $1
自动化部署 varnish 源码包软件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #!/bin/bash yum -y install gcc readline‐devel pcre‐devel useradd -s /sbin/nologin varnish tar -xf varnish‐3.0.6.tar.gz cd varnish‐3.0.6 ./configure ‐‐prefix=/usr/local/varnish make && make install cp redhat/varnish.initrc /etc/init.d/varnish cp redhat/varnish.sysconfig /etc/sysconfig/varnish cp redhat/varnish_reload_vcl /usr/bin/ ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/ ln -s /usr/local/varnish/bin/* /usr/bin mkdir /etc/varnish cp /usr/local/varnish/etc/varnish/default.vcl /etc/varnish/ uuidgen > /etc/varnish/secret
编写 nginx 启动脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 #!/bin/bash program=/usr/local/nginx/sbin/nginx pid=/usr/local/nginx/logs/nginx.pid start (){ if [ -f $pid ];then echo "nginx 服务已经处于开启状态" else $program fi stop (){ if [ -! -f $pid ];then echo "nginx 服务已经关闭" else $program -s stop echo "关闭服务 ok" fi } status (){ if [ -f $pid ];then echo "服务正在运行..." else echo "服务已经关闭" fi } case $1 in start) start;; stop) stop;; restart) stop sleep 1 start;; status) status;; *) echo "你输入的语法格式错误" esac
自动对磁盘分区、格式化、挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 fdisk /dev/vdb << EOF n p 1 wq EOF mkfs.xfs /dev/vdb1 if [ -e /data ]; then exit fi mkdir /data echo '/dev/vdb1 /data xfs defaults 1 2' >> /etc/fstab mount -a
自动优化 Linux 内核参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #!/bin/bash cat >> /usr/lib/sysctl.d/00‐system.conf <<EOF fs.file‐max=65535 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 5 net.ipv4.tcp_syn_retries = 5 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30 #net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 1024 65535 kernel.shmall = 2097152 kernel.shmmax = 2147483648 kernel.shmmni = 4096 kernel.sem = 5010 641280 5010 128 net.core.wmem_default=262144 net.core.wmem_max=262144 net.core.rmem_default=4194304 net.core.rmem_max=4194304 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_keepalive_time = 30 net.ipv4.tcp_window_scaling = 0 net.ipv4.tcp_sack = 0 EOF sysctl –p
切割 Nginx 日志文件(防止单个文件过大,后期处理很困难)
1 2 3 4 5 6 7 8 9 10 11 12 logs_path="/usr/local/nginx/logs/" mv ${logs_path} access.log ${logs_path} access_$(date -d "yesterday" +"%Y%m%d" ).log kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` 0 1 * * * /data/scripts/nginx_log.sh
检测 MySQL 数据库连接数量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash log_file=/var/log/mysql_count.log user=root passwd=123456 while : do sleep 2 count=`mysqladmin -u "$user " -p "$passwd " status | awk '{print $4}' ` echo "`date +%Y‐%m‐%d` 并发连接数为:$count " >> $log_file done
根据 md5 校验码,检测文件是否被修改
1 2 3 4 5 6 7 8 9 10 #!/bin/bash for i in $(ls /etc/*.conf) do md5sum "$i " >> /var/log/conf_file.log done
检测 MySQL 服务是否存活
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash host=192.168.51.198 user=root passwd=123456 mysqladmin -h '$host' -u '$user' -p'$passwd' ping &>/dev/null if [ $? -eq 0 ] then echo "MySQL is UP" else echo "MySQL is down" fi
备份 MySQL 的 shell 脚本(mysqldump版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash user=root passwd=123456 dbname=mysql date =$(date +%Y%m%d) [ ! -d /mysqlbackup ] && mkdir /mysqlbackup mysqldump -u "$user " -p "$passwd " "$dbname " > /mysqlbackup/"$dbname " -${date} .sql
将文件中所有的小写字母转换为大写字母
1 2 3 4 5 6 #!/bin/bash tr "[a‐z]" "[A‐Z]" < $1
非交互自动生成 SSH 密钥文件
1 2 3 4 5 6 #!/bin/bash rm -rf ~/.ssh/{known_hosts,id_rsa*} ssh‐keygen -t RSA -N '' -f ~/.ssh/id_rsa
检查特定的软件包是否已经安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash if [ $# -eq 0 ];then echo "你需要制定一个软件包名称作为脚本参数" echo "用法:$0 软件包名称 ..." fi for package in "$@ " do if rpm -q ${package} &>/dev/null ;then echo -e "${package} \033[32m 已经安装\033[0m" else echo -e "${package} \033[34;1m 未安装\033[0m" fi done
监控 HTTP 服务器的状态(测试返回码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #!/bin/bash url=http://http://183.232.231.172/index.html check_http () { status_code=$(curl -m 5 -s -o /dev/null -w %{http_code} $url ) } while : do check_http date =$(date +%Y%m%d‐%H:%M:%S) echo "当前时间为:$date $url 服务器异常,状态码为${status_code} . 请尽快排查异常." > /tmp/http$$.pid if [ $status_code -ne 200 ];then mail -s Warning root < /tmp/http$$.pid else echo "$url 连接正常" >> /var/log/http.log fi sleep 5 done
自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/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 $port do echo "Adding $i Port to firewall" firewall‐cmd --add-port=${i} /tcp done firewall‐cmd --runtime-to-permanent
使用脚本自动创建逻辑卷
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #!/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}
显示 CPU 厂商信息
1 2 3 4 #!/bin/bash awk '/vendor_id/{print $3}' /proc/cpuinfo | uniq
删除某个目录下大小为 0 的文件
1 2 3 4 5 6 7 #!/bin/bash dir ="/var/www/html" find $dir -type f -size 0 -exec rm -rf {} \;
查找 Linux 系统中的僵尸进程
1 2 3 4 5 #!/bin/bash ps aux | awk '{if($8 == "Z"){print $2,$11}}'
提示用户输入年份后判断该年是否为闰年
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/bin/bash read -p "请输入一个年份:" year if [ "$year " = "" ];then echo "没有输入年份" exit fi if [[ "$year " =~ [a‐Z] ]];then echo "你输入的不是数字" exit fi if [ $[year % 4] -eq 0 ] && [ $[year % 100] -ne 0 ];then echo "$year 年是闰年" elif [ $[year % 400] -eq 0 ];then echo "$year 年是闰年" else echo "$year 年不是闰年" fi
生成随机密码(urandom 版本)
1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash tr -dc '_A‐Za‐z0‐9' </dev/urandom | head -c 10
生成随机密码(字串截取版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash key="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" num=${#key} pass='' for i in {1..8} do index=$[RANDOM%num] pass=$pass${key:$index:1} done echo $pass
生成随机密码(UUID 版本,16 进制密码)
生成随机密码(进程 ID 版本,数字密码)
测试用户名与密码是否正确
1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash read -p "请输入用户名:" user read -p "请输入密码:" pass if [ "$user " == 'tom' -a "$pass " == '123456' ];then echo "Login successful" else echo "Login Failed" fi
循环测试用户名与密码是否正确
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash for i in {1..3} do read -p "请输入用户名:" user read -p "请输入密码:" pass if [ "$user " == 'tom' -a "$pass " == '123456' ];then echo "Login successful" exit fi done echo "Login Failed"
Shell 脚本的 fork 炸弹
1 2 3 4 5 6 7 8 #!/bin/bash .() { .|.& };.
批量下载有序文件(pdf、图片、视频等等)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash url="http://www.baidu.com/" echo "开始下载..." sleep 2 type =jpg for i in `seq 100` echo "正在下载$i .$type " curl $url /$i .$type -o /tmp/${i} $type sleep 1 done
显示当前计算机中所有账户的用户名称
1 2 3 4 5 6 7 8 9 10 11 12 13 awk -F: '{print $1}' /etc/passwd cut -d: -f1 /etc/passwd sed 's/:.*//' /etc/passwd
制定目录路径,脚本自动将该目录使用 tar 命令打包备份到/data目录
1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash [ ! -d /data ] && mkdir /data [ -z $1 ] && exit if [ -d $1 ];then tar -czf /data/$1 .-`date +%Y%m%d`.tar.gz $1 else echo "该目录不存在" fi
显示进度条(回旋镖版)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/bin/bash while : do clear for i in {1..20} do echo ‐e "\033[3;${i} H*" sleep 0.1 done clear for i in {20..1} do echo ‐e "\033[3;${i} H*" sleep 0.1 done clear done
安装 LAMP 环境(yum 版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 #!/bin/bash yum makecache &>/dev/null num=$(yum repolist | awk '/repolist/{print $2}' | sed 's/,//' ) if [ $num -lt 0 ];then yum -y install httpd yum -y install mariadb mariadb-server mariadb-devel yum -y install php php-mysql else echo "未配置 yum 源..." fi
循环关闭局域网中所有主机
1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash for i in {1..254} do [ $i -eq 100 ] && continue echo "正在关闭 192.168.4.$i ..." ssh 192.168.4.$i poweroff done
获取本机 MAC 地址
1 2 3 4 5 6 7 8 #!/bin/bash ip a s | awk 'BEGIN{print " 本 机 MAC 地 址 信 息 如 下 :"}/^[0‐9]/{print $2;getline;if($0~/link\/ether/){print $2}}' | grep -v lo:
自动配置 rsynd 服务器的配置文件 rsyncd.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #!/bin/bash [ ! -d /home/ftp ] && mkdir /home/ftp echo 'uid = nobody gid = nobody use chroot = yes max connections = 4 pid file = /var/run/rsyncd.pid exclude = lost+found/ transfer logging = yes timeout = 900 ignore nonreadable = yes dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 [ftp] path = /home/ftp comment = share' > /etc/rsyncd.conf
修改 Linux 系统的最大打开文件数量
1 2 3 4 5 6 7 8 9 #!/bin/bash cat >> /etc/security/limits.conf <<EOF * soft nofile 65536 * hard nofile 65536 EOF
设置 Python 支持自动命令补齐功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #!/bin/bash Needs import readline and rlcompleter module import readline import rlcompleter help (rlcompleter) display detail: readline.parse_and_bind('tab: complete' ) man python display detail: PYTHONSTARTUP variable if [ ! -f /usr/bin/tab.py ];then cat >> /usr/bin/tab.py <<EOF import readline import rlcompleter readline.parse_and_bind('tab: complete') EOF fi sed -i '$a export PYTHONSTARTUP=/usr/bin/tab.py' /etc/profile source /etc/profile
自动修改计划任务配置文件
1 2 3 4 5 6 7 8 9 10 11 12 Docker+K8s+Jenkins 主流技术全解视频资料【干货免费分享】 read -p "请输入分钟信息(00‐59):" min read -p "请输入小时信息(00‐24):" hour read -p "请输入日期信息(01‐31):" date read -p "请输入月份信息(01‐12):" month read -p "请输入星期信息(00‐06):" weak read -p "请输入计划任务需要执行的命令或脚本:" program echo "$min $hour $date $month $weak $program " >> /etc/crontab
使用脚本循环创建三位数字的文本文件(111-999 的文件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash for i in {1..9} do for j in {1..9} do for k in {1..9} do touch /tmp/$i$j$k .txt done done done
找出/etc/passwd 中能登录的用户,并将对应在/etc/shadow 中第二列密码提出处理
1 2 3 4 5 6 7 8 9 #!/bin/bash user=$(awk -F: '/bash$/{print $1}' /etc/passwd) for i in $user do awk -F: -v x=$i '$1==x{print $1,$2}' /etc/shadow done
统计/etc/passwd 中 root 出现的次数
1 2 3 4 5 6 #!/bin/bash awk -F: '{i=1;while(i<=NF){if($i~/root/){x++};i++}} END{print "root 出现次数为"x}' /etc/passwd
统计 Linux 进程相关数量信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #!/bin/bash running=0 sleeping=0 stoped=0 zombie=0 for pid in /proc/[1‐9]* do procs=$[procs+1] stat =$(awk '{print $3}' $pid /stat) case $stat in R) running=$[running+1] ;; T) stoped=$[stoped+1] ;; S) sleeping=$[sleeping+1] ;; Z) zombie=$[zombie+1] ;; esac done echo "进程统计信息如下" echo "总进程数量为:$procs " echo "Running 进程数为:$running " echo "Stoped 进程数为:$stoped " echo "Sleeping 进程数为:$sleeping " echo "Zombie 进程数为:$zombie "
从键盘读取一个论坛积分,判断论坛用户等级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #!/bin/bash read -p "请输入积分(0‐100):" JF if [ $JF -ge 90 ] ; then echo "$JF 分,神功绝世" elif [ $JF -ge 80 ] ; then echo "$JF 分,登峰造极" elif [ $JF -ge 70 ] ; then echo "$JF 分,炉火纯青" elif [ $JF -lt 60 ] ; then echo "$JF 分,略有小成" else echo "$JF 分,初学乍练" fi
判断用户输入的数据类型(字母、数字或其他)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash read -p "请输入一个字符:" KEY case "$KEY " in [a‐z]|[A‐Z]) echo "字母" ;; [0‐9]) echo "数字" ;; *) echo "空格、功能键或其他控制字符" esac
显示进度条(数字版)
1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash for i in {1..100} do echo -e "\033[6;8H[" echo -e "\033[6;9H$i %" echo -e "\033[6;13H]" sleep 0.1 done
打印斐波那契数列
1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash list=(0 1) for i in `seq 2 11` do list[$i ]=`expr ${list[‐1]} + ${list[‐2]} ` done echo ${list[@]}
判断用户输入的是 Yes 或 NO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash read -p "Are you sure?[y/n]:" sure case $sure in y|Y|Yes|YES) echo "you enter $a " ;; n|N|NO|no) echo "you enter $a " ;; *) echo "error" ;; esac
显示本机 Linux 系统上所有开放的端口列表
1 2 3 4 5 6 #!/bin/bash ss -nutlp | awk '{print $1,$5}' | awk -F"[: ]" '{print "协议:"$1,"端口号:"$NF}' | grep "[0‐9]" | uniq
将 Linux 系统中 UID 大于等于 1000 的普通用户都删除
1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash user=$(awk -F: '$3>=1000{print $1}' /etc/passwd) for i in $user do userdel -r $i done
使用脚本开启关闭虚拟机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #!/bin/bash case $1 in list) virsh list --all ;; start) virsh start $2 ;; stop) virsh destroy $2 ;; enable ) virsh autostart $2 ;; disable ) virsh autostart --disable $2 ;; *) echo "Usage:$0 list" echo "Usage:$0 [start|stop|enable|disable] VM_name" cat << EOF #list 显示虚拟机列表 #start 启动虚拟机 #stop 关闭虚拟机 #enable 设置虚拟机为开机自启 #disable 关闭虚拟机开机自启功能 EOF ;; esac
调整虚拟机内存参数的 shell 脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/bin/bash cat << EOF 1.调整虚拟机最大内存数值 2.调整实际分配给虚拟机的内存数值 EOF read -p "请选择[1‐2]:" select case $select in 1) read -p "请输入虚拟机名称" name read -p "请输入最大内存数值(单位:k):" size virsh setmaxmem $name --size $size --config ;; 2) read -p "请输入虚拟机名称" name read -p "请输入实际分配内存数值(单位:k):" size virsh setmem $name $size ;; *) echo "Error" ;; esac
查看 KVM 虚拟机中的网卡信息(不需要进入启动或进入虚拟机)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #!/bin/bash clear mountpoint="/media/virtimage" [ ! -d $mountpoint ] && mkdir $mountpoint read -p "输入虚拟机名称:" name echo "请稍后..." if mount | grep -q "$mountpoint " ;then umount $mountpoint fi guestmount -r -d $name -i $mountpoint echo echo "‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐" echo -e "\033[32m$name 虚拟机中网卡列表如下:\033[0m" dev=$(ls /media/virtimage/etc/sysconfig/network‐scripts/ifcfg-* |awk -F"[/‐]" '{print $9}' ) echo $dev echo "‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐" echo echo echo "+++++++++++++++++++++++++++++++++++++++++++" echo -e "\033[32m 网卡 IP 地址信息如下:\033[0m" for i in $dev do echo -n "$i :" grep -q "IPADDR" /media/virtimage/etc/sysconfig/network‐scripts/ifcfg-$i || echo "未配置 IP地址" awk -F= '/IPADDR/{print $2}' /media/virtimage/etc/sysconfig/network-scripts/ifcfg-$i done echo "+++++++++++++++++++++++++++++++++++++++++++"
不登陆虚拟机,修改虚拟机网卡 IP 地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #!/bin/bash read -p "请输入虚拟机名称:" name if virsh domstate $name | grep -q running ;then echo "修改虚拟机网卡数据,需要关闭虚拟机" virsh destroy $name fi mountpoint="/media/virtimage" [ ! -d $mountpoint ] && mkdir $mountpoint echo "请稍后..." if mount | grep -q "$mountpoint " ;then umount $mountpoint fi guestmount -d $name -i $mountpoint read -p "请输入需要修改的网卡名称:" dev read -p "请输入 IP 地址:" addr if grep -q "IPADDR" $mountpoint /etc/sysconfig/network‐scripts/ifcfg‐$dev ;then sed -i "/IPADDR/s/=.*/=$addr /" $mountpoint /etc/sysconfig/network‐scripts/ifcfg‐$dev else echo "IPADDR=$addr " >> $mountpoint /etc/sysconfig/network‐scripts/ifcfg‐$dev fi awk -F= -v x=$addr '$2==x{print "完成..."}' $mountpoint /etc/sysconfig/network‐scripts/ifcfg-$dev
破解虚拟机密码,无密码登陆虚拟机系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash read -p "请输入虚拟机名称:" name if virsh domstate $name | grep -q running ;then echo "破解,需要关闭虚拟机" virsh destroy $name fi mountpoint="/media/virtimage" [ ! -d $mountpoint ] && mkdir $mountpoint echo "请稍后..." if mount | grep -q "$mountpoint " ;then umount $mountpoint fi guestmount -d $name -i $mountpoint sed -i "/^root/s/x//" $mountpoint /etc/passwd
Shell 脚本对信号的处理,执行脚本后,按键盘 Ctrl+C 无法终止的脚本
1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash trap 'echo "暂停 10s";sleep 10' 2 while : do echo "go go go" done
一键部署 memcached
1 2 3 4 5 6 7 8 9 10 11 12 #!/bin/bash wget http://www.memcached.org/files/memcached-1.5.1.tar.gz yum -y install gcc tar -xf memcached‐1.5.1.tar.gz cd memcached‐1.5.1 ./configure make make install
一键配置 VNC 远程桌面服务器(无密码版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 #!/bin/bash rpm --quiet -q tigervnc‐server if [ $? -ne 0 ];then yum -y tigervnc‐server fi x0vncserver AcceptKeyEvents=0 AlwaysShared=1 \ AcceptPointerEvents=0 SecurityTypes=None rfbport=5908
关闭 SELinux
1 2 3 4 5 6 #!/bin/bash sed -i '/^SELINUX/s/=.*/=disabled/' /etc/selinux/config setenforce 0
查看所有虚拟机磁盘使用量以及CPU使用量信息
1 2 3 4 5 6 7 #!/bin/bash virt‐df read -n1 "按任意键继续" key virt‐top
使用 shell 脚本打印图形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #!/bin/bash clear for (( i=1; i<=9; i++ )) do for (( j=1; j<=i; j++ )) do echo -n "$i " done echo "" done read -n1 "按任意键继续" key clear for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " |" done echo "_ " done read -n1 "按任意键继续" key clear for (( i=1; i<=5; i++ )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo "" done for (( i=5; i>=1; i‐‐ )) do for (( j=1; j<=i; j++ )) do echo -n " *" done echo "" done
根据计算机当前时间,返回问候语,可以将该脚本设置为开机启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #!/bin/bash tm=$(date +%H) if [ $tm -le 12 ];then msg="Good Morning $USER " elif [ $tm -gt 12 -a $tm -le 18 ];then msg="Good Afternoon $USER " else msg="Good Night $USER " fi echo "当前时间是:$(date +"%Y‐%m‐%d %H:%M:%S" ) " echo -e "\033[34m$msg \033[0m"
读取用户输入的账户名称,将账户名写入到数组保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/bin/bash i=0 while : do read -p "请输入账户名,输入 over 结束:" key if [ $key == "over" ];then break else name[$i ]=$key let i++ fi done echo "总账户名数量:${#name[*]} " echo "${name[@]} "
判断文件或目录是否存在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash if [ $# -eq 0 ] ;then echo "未输入任何参数,请输入参数" echo "用法:$0 [文件名|目录名]" fi if [ -f $1 ];then echo "该文件,存在" ls -l $1 else echo "没有该文件" fi if [ -d $1 ];then echo "该目录,存在" ls -ld $2 else echo "没有该目录" fi
打印各种格式的时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #!/bin/bash echo "显示星期简称(如:Sun)" date +%a echo "显示星期全称(如:Sunday)" date +%A echo "显示月份简称(如:Jan)" date +%b echo "显示月份全称(如:January)" date +%B echo "显示数字月份(如:12)" date +%m echo "显示数字日期(如:01 号)" date +%d echo "显示数字年(如:01 号)" date +%Y echo "显示年‐月‐日" date +%F echo "显示小时(24 小时制)" date +%H echo "显示分钟(00..59)" date +%M echo "显示秒" date +%S echo "显示纳秒" date +%N echo "组合显示" date +"%Y%m%d %H:%M:%S"
使用 egrep 过滤 MAC 地址
1 2 3 4 5 6 7 #!/bin/bash egrep "[0‐9a‐fA‐F]{2}(:[0‐9a‐fA‐F]{2}){5}" $1
统计双色球各个数字的中奖概率
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash awk '{print $1"\n"$2"\n"$3"\n"$4"\n"$5"\n"$6}' 1.txt | sort | uniq -c | sort awk '{print $7}' 1.txt | sort | uniq -c | sort
生成签名私钥和证书
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/bin/bash read -p "请输入存放证书的目录:" dir if [ ! -d $dir ];then echo "该目录不存在" exit fi read -p "请输入密钥名称:" name openssl genrsa -out ${dir} /${name} .key openssl req -new -x509 -key ${dir} /${name} .key -subj "/CN=common" -out ${dir} /${name} .crt
使用 awk 编写的 wc 程序
1 2 3 4 5 6 7 8 9 #!/bin/bash awk '{chars+=length($0)+1;words+=NF} END{print NR,words,chars}' $1
原文:blog.csdn.net/yugemengjing/article/details/82469785