# 方法一
echo $RANDOM |md5sum |cut -c 1-8
d2614e90
# 方法二
openssl rand -base64 4
6kLKvQ==
# 方法3
cat /proc/sys/kernel/random/uuid |cut -c 1-8
# 方法1:
echo $RANDOM |cksum |cut -c 1-8
62936468
# 方法2:
openssl rand -base64 4 |cksum |cut -c 1-8
17393369
# 方法3:
date +%N |cut -c 1-8
56937858
#!/bin/bash
. /etc/init.d/functions
array=(
http://www.baidu.com
http://www.zcj.net.cn
http://www.taobao.com
)
for((i=0;i<${#array[*]};i++))
do
ret=`curl -I -s ${array[i]}|egrep "200|302"|wc -l`
if [ $ret -eq 1 ];then
action "`echo ${array[i]}|awk -F "/" '{print $3}'` is ok" /bin/true
else
action "`echo ${array[i]}|awk -F "/" '{print $3}'` is not ok" /bin/false
echo `echo ${array[i]} is not ok|mail -s "$(date +%F-%S)warning" 18621048481@163.com`
fi
done
# 执行脚本验证
bash test.sh
www.baidu.com is ok [ OK ]
www.zcj.net.cn is ok [ OK ]
www.taobao.com is not ok [FAILED]
#!/bin/sh
[ -f /etc/init.d/functions ]&& source /etc/init.d/functions
[ $UID -ne 0 ]&&{
echo "Ples sudo su - root"
exit 1
}
for user in youmen{01..10}
do
word=$(grep "\b$user\b" /etc/passwd|wc -l)
if [ $word -eq 1 ];then
action "Useradd $user already exists" /bin/false
continue
fi
pass=$(echo $RANDOM|md5sum|cut -c 1-8)
useradd $user && \
echo "$pass"|passwd --stdin $user &>/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Useradd $user IS OK" /bin/true
fi
echo -e "$user\t$pass" >>/tmp/user.txt
done
#!/bin/bash
if rpm -q sysstat &>/dev/null; then
echo "sysstat is already installed."
else
echo "sysstat is not installed!"
fi
#!/usr/bin/env bash
read -p "please input you pass key IP:[192.168.25]" ip
for i in `seq 2 254`
do
{
ping -c1 $ip.$i &> /dev/null
if [ $? -eq 0 ];then
echo "$ip.$i" >> ip.txt
fi
}&
done
wait
#!/usr/bin/env bash
read -p "please input you pass key IP:[192.168.25]" ip
read -p "please input you pass keyIP password:" youpasswd
if [ ! -f /usr/bin/expect ];then
yum -y install expect
fi
sed -i 's/# *StrictHostKeyChecking *ask/StrictHostKeyChecking no/g' /etc/ssh/ssh_config
systemctl restart sshd
if [ ! -f /root/.ssh/id_rsa.pub ];then
cd /root/.ssh/
ssh-keygen -t rsa -N '' -f id_rsa -q
fi
for i in `seq 2 254`
do
{
ping -c1 $ip.$i &> /dev/null
if [ $? -eq 0 ];then
echo "$ip.$i" >> ip.txt
/usr/bin/expect <<-EOF
set timeout 10
spawn ssh-copy-id $ip.$i
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$youpasswd\r"}
}
expect eof
EOF
fi
}&
done
wait
#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=`ip addr | grep "inet" | grep -v "127.0.0.1" | grep -v "inet6" | awk -F/ '{print $1}' | awk '{print $2}' `
MAIL="example@mail.com"
if ! which vmstat &>/dev/null; then
echo "vmstat command no found, Please install procps package."
exit 1
fi
US=$(vmstat |awk 'NR==3{print $13}')
SY=$(vmstat |awk 'NR==3{print $14}')
IDLE=$(vmstat |awk 'NR==3{print $15}')
WAIT=$(vmstat |awk 'NR==3{print $16}')
USE=$(($US+$SY))
echo $US $SY $IDLE $WAIT $USE
if [ $USE -ge 50 ]; then
echo "
Date: $DATE
Host: $IP
Problem: CPU utilization $USE
" | mail -s "CPU Monitor" $MAIL
fi
#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=`ip addr | grep "inet" | grep -v "127.0.0.1" | grep -v "inet6" | awk -F/ '{print $1}' | awk '{print $2}' `
MAIL="example@mail.com"
TOTAL=$(free -m |awk '/Mem/{print $2}')
USE=$(free -m |awk '/Mem/{print $3-$6-$7}')
FREE=$(($TOTAL-$USE))
# 内存小于1G发送报警邮件
if [ $FREE -lt 1024 ]; then
echo "
Date: $DATE
Host: $IP
Problem: Total=$TOTAL,Use=$USE,Free=$FREE
" | mail -s "Memory Monitor" $MAIL
fi
#!/bin/bash
DATE=$(date +%F" "%H:%M)
IP=`ip addr | grep "inet" | grep -v "127.0.0.1" | grep -v "inet6" | awk -F/ '{print $1}' | awk '{print $2}' `
MAIL="example@mail.com"
TOTAL=$(fdisk -l |awk -F'[: ]+' 'BEGIN{OFS="="}/^Disk \/dev/{printf "%s=%sG,",$2,$3}')
PART_USE=$(df -h |awk 'BEGIN{OFS="="}/^\/dev/{print $1,int($5),$6}')
echo $TOTAL
echo $PART_USE
for i in $PART_USE; do
PART=$(echo $i |cut -d"=" -f1)
USE=$(echo $i |cut -d"=" -f2)
MOUNT=$(echo $i |cut -d"=" -f3)
if [ $USE -gt 80 ]; then
echo "
Date: $DATE
Host: $IP
Total: $TOTAL
Problem: $PART=$USE($MOUNT)
" | mail -s "Disk Monitor" $MAIL
fi
done
# 批量主机磁盘利用率监控
#!/bin/bash
HOST_INFO=host.info
for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do
USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO)
PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO)
TMP_FILE=/tmp/disk.tmp
ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE
USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^\/dev/{print $1,int($5)}' $TMP_FILE)
for USE_RATE in $USE_RATE_LIST; do
PART_NAME=${USE_RATE%=*}
USE_RATE=${USE_RATE#*=}
if [ $USE_RATE -ge 80 ]; then
echo "Warning: $PART_NAME Partition usage $USE_RATE%!"
fi
done
done
python实现发邮件
#!/bin/bash
email='18621048481@163.com'
hostip=$(/usr/sbin/ifconfig eth0 |grep "inet"| cut -f 2 -d ":" |awk '{print $2}')
disk_use=`df -h |grep -w "/" |awk -F'%' '{print $1}'|awk '{print $NF}'| uniq`
if [ $disk_use -gt 10 ]
then
/data/SendEmail.py $email "$hostip" "$disk_use"
fi
# SendEmail.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
import sys
mail_host = 'smtp.163.com'
mail_user = '18621048481@163.com'
mail_pass = '*******'
def send_mail(to_list, subject, content):
me = mail_user
msg = MIMEText(content, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['to'] = to_list
try:
server = smtplib.SMTP_SSL(mail_host, 465)
server.login(mail_user, mail_pass)
server.sendmail(me, to_list, msg.as_string())
return True
except Exception as e:
print(e)
return False
if __name__ == "__main__":
send_mail(sys.argv[1], sys.argv[2], sys.argv[3])
# 删除30天前的文件
find ./logs/ -mtime +30 -exec rm -rf {} \;
远程登录其他机器执行命令,之前一直用export 脚本的方式。现在感觉用这个更加方便一点,但是不安全,不建议在生产环境中使用此命令
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install sshpass -y
# 源码安装
wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz
tar xf sshpass.tar.gz
cd sshpass-1.06/
./configure
make && make install
sshpass -p 'password' ssh username@ip 'cmd'
# ssshpas -p '远程机器密码' 使用ssh 用户名@远程机器ip '要执行的命令'
# Example
[root@prometheus_106 opt]# sshpass -p 'youmen' ssh root@192.168.43.18 'ls /root'
memcached-1.5.9
memcached-1.5.9.tar.gz
memcached_exporter-0.6.0.linux-amd64
memcached_exporter-0.6.0.linux-amd64.tar.gz
# 但是注意,linux系统连接陌生机器会有一个秘钥认证,让你输入一个yes,会导致你使用sshpass返回上一次执行的结果,可以通过下面命令关掉
sed -i 's/# *StrictHostKeyChecking *ask/StrictHostKeyChecking no/g' /etc/ssh/ssh_config
systemctl restart sshd
# 自动登录
[root@prometheus_106 opt]# cat ip.txt
192.168.43.18:root:youmen
192.168.43.84:root:youmen
192.168.43.134:root:youmen
192.168.43.144:root:youmen
# -f指定密码文件
# -o 忽略密码提示
sshpass -f ip.txt ssh root@192.168.43.18 'hostnamectl'
# 批量主机操作
cat ip.txt
192.168.43.18
192.168.43.134
192.168.43.189
192.168.43.251
192.168.43.243
192.168.43.144
192.168.43.213
cat sshpass.sh
#!/bin/bash
username="root"
passwd="密码"
port="22"
timeout=3
cmd="hostname"
for host in `cat ip.txt`
do
result=""
result=`sshpass -p "$passwd" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd`
echo $result >> result.txt
done
[root@prometheus_106 opt]# bash sshpass.sh
[root@prometheus_106 opt]# cat result.txt
memcached
redis
mysql-101
rabbitmq-2
nginx-104
prometheus_106
rabbitmq-1
expect是一种能够按照脚本内容里面设定的方式与交互式程序进行“会话”的程序。根据脚本内容,Expect可以知道程序会提示或反馈什么内容以及 什么是正确的应答。它是一种可以提供“分支和嵌套结构”来引导程序流程的解释型脚本语言。 shell功能很强大,但是不能实现有交互功能的多机器之前的操作,例如ssh和ftp.而expect可以帮助我们来实现.
yum -y install expect
#!/usr/bin/env bash
read -p "please input you pass key IP:[192.168.25]" ip
read -p "please input you pass keyIP password:" youpasswd
if [ ! -f /usr/bin/expect ];then
yum -y install expect
fi
sed -i 's/# *StrictHostKeyChecking *ask/StrictHostKeyChecking no/g' /etc/ssh/ssh_config
systemctl restart sshd
if [ ! -f /root/.ssh/id_rsa.pub ];then
cd /root/.ssh/
ssh-keygen -t rsa -N '' -f id_rsa -q
fi
for i in `seq 2 254`
do
{
ping -c1 $ip.$i &> /dev/null
if [ $? -eq 0 ];then
echo "$ip.$i" >> ip.txt
/usr/bin/expect <<-EOF
set timeout 10 # 设置超时时间
spawn ssh-copy-id $ip.$i # 发送ssh
expect { # 返回信息匹配
"yes/no" { send "yes\r"; exp_continue } # 第一次ssh连接会提示yes/no,继续
"password:" { send "$youpasswd\r"} # 出现密码提示,发送密码
}
expect eof
EOF
fi
}&
done
wait
chmod u+w /etc/sudoers &&echo "appmanager ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && chmod u-w /etc/sudoers
# 查看当前公网IP
youmen@youmendeMacBook-Pro ~ % curl cip.cc
IP : 221.217.106.253
地址 : 中国 北京
运营商 : 联通
数据二 : 北京市房山区 | 联通
数据三 :
URL : http://www.cip.cc/221.217.106.253
#! /usr/bin/env bash
# Author: ZhouJian
# Mail: 18621048481@163.com
# Time: 2019-9-3
# Describe: CentOS 7 Install Nginx Source Code Script
version="nginx-1.14.2.tar.gz"
user="nginx"
nginx=${version%.tar*}
path=/usr/local/src/$nginx
echo $path
if ! ping -c2 www.baidu.com &>/dev/null
then
echo "网络不通,无法安装"
exit
fi
yum install -y gcc gcc-c++ openssl-devel pcre-devel make zlib-devel wget psmisc
if [ ! -e $version ];then
wget http://nginx.org/download/$version
fi
if ! id $user &>/dev/null
then
useradd $user -M -s /sbin/nologin
fi
if [ ! -d /var/tmp/nginx ];then
mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
fi
tar xf $version -C /usr/local/src
cd $path
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_realip_module \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--with-file-aio \
--with-http_secure_link_module && make && make install
if [ $? -ne 0 ];then
echo "nginx未安装成功"
exit
fi
killall nginx
/usr/local/nginx/sbin/nginx
#echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
#chmod +x /etc/rc.local
#systemctl start rc-local
#systemctl enable rc-local
ss -antp |grep nginx
# 去掉文件所以以#开头的行
sed -i '/^ *#/d' 配置文件路径
# 去掉文件空行
sed -ri '/^[[:space:]]*(#|$)/d' 配置文件路径
# 查看指定端口程序打开的文件路径
ss -antlp |grep 5555
ll /proc/6010 |grep cwd