码迷,mamicode.com
首页 > 系统相关 > 详细

Shell百宝箱(后续会不断更新)

时间:2020-06-30 22:26:01      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:rom   ssl   pen   url   restart   exist   connect   init   数字   

获取随机字符串或数字

随机获取8位字符串
# 方法一
echo $RANDOM |md5sum |cut -c 1-8
d2614e90

# 方法二
openssl rand -base64 4
6kLKvQ==

# 方法3
cat /proc/sys/kernel/random/uuid |cut -c 1-8
获取随机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

监控CPU,内存和硬盘利用率

CPU
#!/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
Memory
#!/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
Disk
#!/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])

sshpass

远程登录其他机器执行命令,之前一直用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

Shell百宝箱(后续会不断更新)

标签:rom   ssl   pen   url   restart   exist   connect   init   数字   

原文地址:https://www.cnblogs.com/you-men/p/13216283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!