目录
查看局网内主机存活
#!/usr/bin/bash
# check host status
#v1
for i in {1..254}
do
{
ip=192.168.8.$i
ping -c 2 -W 1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is online" | tee -a /tmp/host_online.txt
else
# echo "$ip is offline" | tee -a /tmp/host_offline.txt
echo "$ip is offline" &>/dev/null
fi
}&
done
wait
批量自定义创建用户
#!/usr/bin
# create user
# 功能:自定义用户名前缀和尾部数字,判断 输入的是不是数字和非空的字符集
#
read -p "请输入尾号:" num
while true
do
if [[ "$num" =~ ^[0-9]+$ ]];then
break
else
read -p "请再次输入尾号:" num
fi
doneread -p "请输入用户名前缀:" prefix
while true
do
if [ -n "$prefix" ];then
break
else
read -p "请再次输入用户名前缀:" prefix
fi
donefor i in `seq -w $num` #-w 等位补全
do
user=$prefix$i
useradd $user
echo "123" | passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created"
fi
done
命名管道控制并发创建
#/usr/bin/bash
#
#
num=100
tmp_file=/tmp/file1.fifomkfifo ${tmp_file}
exec 8<> ${tmp_file}
rm -f ${tmp_file}
for i in `seq $num`
do
echo >&8
donefor j in {1..1000}
do
read -u 8
{
user=user$j
useradd $user
if [ $? -eq 0 ];then
echo "$user is created"
fi
echo >&8
echo $$
}&
donewait
echo $$
从文件中读取用户名密码进行批量创建
#!/bin/bash
file=/root/shell/passwd.txt
while read line
do
user=`echo "$line" | awk '{print $1}'`
pass=`echo "$line" | awk '{print $2}'`
if [[ `cat $file | wc -l` -eq 0 ]];then
echo "文件是空的"
else
echo "$user"
echo "$pass"
useradd $user
echo "$pass" | passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "user is create"
fi
fi
done<$file
自动应答分发免密登录
#!/usr/bin/bash
# ssh keygen
# auth ren>ip_ok.txt
>ip_false.txt
user=root
passwd=123456rpm -qa | grep expect &>/dev/null
if [ $? -ne 0 ];then
echo "expect is not install"
yum -y install expect
fiif [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fifor i in {15..30}
do
{
ip=192.168.8."$i"
ping -c 1 -W1 "$ip"
if [ $? -eq 0 ];then
echo "$ip" >> ip_ok.txt
/usr/bin/expect <<-EOF
spawn ssh-copy-id $user@$ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$passwd\r" };
}
expect eof
EOF
else
echo "$ip" >>ip_false.txt
fi
}&
done
wait
echo "finish"
简单的系统工具
#!/bin/bash
# system tools
# v1
menu() {
cat <<-EOF
+-----------------------------------+
H:help of menu
F:display disk partition
D:filesystem mount
M:memory info
U:system load
I: check to see if all hosts
Q:exit
+-----------------------------------+
EOF
}
mem_use() {
mem_used=`free -m | grep "^Mem" | awk '{print $3}'`
mem_total=`free -m | grep "^Mem" | awk '{print $2}'`
mem_percent=$((mem_used*100/mem_total))
echo "${mem_percent}%"
}ip_check() {
for i in {2..20}
do
ip=192.168.8.$i
ping -c 1 -w1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip" >>/tmp/ip_online.txt
else
echo "$ip" >>/tmp/ip_notonline.txt
fi
done
}while :
do
menu
read -p "please choose:" actioncase "$action" in
h|H)
menu
;;
f|F)
fdisk -l
disk_info=`df -h | grep "/$" | awk '{print $(NF-1)}'`
echo "磁盘剩余空间为:$disk_info"
;;
d|D)
mount
;;
m|M)
free -m
mem=$(mem_use)
echo "剩余内存百分比为:$mem"
;;
u|U)
uptime
;;
q|Q)
break
;;
I|i)
ip_check
if [ $? -eq 0 ];then
echo "check finish"
else
echo "eheck fiald"
fi
;;
"")
echo "请重新输入!!"
;;
*)
menu
esac
done
远程配置基础环境
#/bin/bash
#change config
#v1
for i in `cat /root/shell/remote_host_list.txt`
do
ping -c 1 $i &> /dev/null
if [ $? -eq 0 ];then
ssh "$i" "sed -ri '/^#UseDNS/cUseDNS no' /etc/ssh/sshd_config"
ssh "$i" "sed -ri '/SELINUX=enforcing/cSELINUX=disabled' /etc/selinux/config"
ssh "$i" "systemctl stop fireawlld; systemctl disable firewalld"
ssh "$i" "reboot"
fi
done
echo "finish"
菜单式自定义批量创建用户
#!/bin/bash
# create user
# v4
while true
do
read -p "请输入前缀,密码,及数量 & num[e.g.: car beckham 5]:" prefix pass num
printf "USER INFOMATION:
-------------------------------------------------------
user prefix: $prefix
user password: $pass
user number: $num
-------------------------------------------------------
"
read -p "Are you ok?[y|n]:" action
if [ "$action" = "y" ];then
break
fidone
for i in `seq -w 0$num`
do
user=$prefix$i
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "该用户已经存在"
else
useradd $user
echo "$pass" | passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "user is created"
fi
fi
done
监控内存脚本
#!/bin/bash
#memory use
#v1
mem_war_file=/tmp/mem_war.txt
mem_use=`free -m | grep Mem | awk '{print $3}'`
mem_total=`free -m | grep Mem | awk '{print $2}'`
mem_percent=$((mem_use*100/mem_total))
# echo "$mem_percent"%
if (($mem_percent > 80));then
echo "`date +%F-%H-%M` mem: ${mem_percent}%" >$mem_war_file
echo "`date +%F-%H-%M` mem: ${mem_percent}%" | mail -s "mem warning" root
fi
跳板机脚本
#!/usr/bin/bash
#jumpserver
#v3#ps1=`ps | grep bash | awk '{print $1}'` #退出远程终端第一种方法
trap "" HUP INT QUIT TSTP
web1=192.168.8.15
mysql=192.168.8.16clear
while :
do
cat <<-EOF
+--------------------------+
| JumpServer |
| 1.web1 |
| 2.msql |
| Q.quit |
+--------------------------+
EOFecho -en "\e[1;32minput number: \e[0m" #添加颜色并不换行
read numcase "$num" in
1)
ssh beckham@$web1
;;
2)
ssh beckham@$mysql
;;
Q|q)
#kill -9 $ps1 #第一种方法的选择
ps | grep bash |awk '{print "kill -9 "$1}' |bash
;;
*)
echo "输入有误请重新输入"
esac
done
监控磁盘脚本
#!/usr/bin/bash
# disk use
#v1
mail_user=root
disk_use=`df -Th | grep "/$" |awk '{print $(NF-1)}' | awk -F '%' '{print $1}'`if [ "$disk_use" -ge 80 ];then
echo "`date +%F-%H-%M` disk: ${disk_use}%" | mail -s "disk warning" $mail_user
fi
代码上线脚本
#!/bin/bash
# code online
# author: ren
PROJT_DIR=/usr/local/nginx/html
OLD_DIR=/usr/local/nginx/html/web1
PROJT=web1
BACKUP_DIR=/data/backup
DATA_CHMOD=www
DATE=`date +%F`
NEW_DIR=/data/web1
# 关闭nginx
function stop_nginx() {
/usr/bin/systemctl stop nginx
if [ $? -eq 0 ];then
echo "nginx is stopd"
else
echo "nginx is not stop please check..."
exit 1
fi
}#2 备份原有数据
function backup_data() {
if [ -d $BACKUP_DIR/$DATE'-'$PROJT ];then
echo "DIR $BACKUP_DIR/$DATE'-'$PROJT is exist"
exit 2
else
mv $OLD_DIR $BACKUP_DIR/$DATE'-'$PROJT
fi
}# 3移动新的代码 项目目录 注:此代码目录需手动上传解压
function new_code() {
if [ -d $NEW_DIR ];then
mv $NEW_DIR $PROJT_DIR
else
echo "NEW_DIR is not exist"
exit 3
fi
}# 4 修改权限
function chmod_news() {
chown -R $DATA_CHMOD.$DATA_CHMOD $OLD_DIR
}# 5 启动服务
function start_nginx() {
/usr/bin/systemctl start nginx
if [ $? -eq 0 ];then
echo "nginx start ok"
else
echo "ngin is not start,please check..."
fi
}stop_nginx
backup_data
new_code
chmod_news
start_nginx
redis 启动脚本
#!/bin/bash
# the script usage to start and stop redis
# chkconfig 100 90
. /etc/init.d/functions
conf_path=/usr/local/redis/redis.conf
if [ ! -f $conf_path ];then
exit 6
fi
ser_path=/usr/local/bin/redis-server
cli_path=/usr/local/bin/redis-cli
redis_start()
{
if [ -f "/var/run/redis.pid" ];then
echo "/var/run/redis.pid does exists, process is already running or crashed"
else
echo "starting Redis server..."
$ser_path $conf_path
fi
if [ $? -eq 0 ];then
action "redis-server startd" /bin/true
else
action "redis-server startd" /bin/false
echo "Please check the configuration parameters, see the log for specific information."
fi
}
redis_stop()
{
if [ ! -f "/var/run/redis.pid" ];then
echo "/var/run/redis.pid does not exist,process is not running"
else
$cli_path shutdown
action "redis-server stopd" /bin/true
fi
}
case $1 in
start)
redis_start
;;
stop)
redis_stop
;;
restart)
redis_stop
redis_start
;;
*)
echo "usage $0 [start|stop|restart]"
esac
MySQL Xtrabackup备份脚本
#!/bin/bash
# chkconfig: 345 99 10
#This script is used to backup mysql
#Choose to run the script by week
#The script will automatically choose the backup level
time=$(date +%A)
mysql=$(ps aux | grep mysql | grep -v grep | wc -l)
log=/tmp/xtrabackup.log
full_back(){
/usr/bin/innobackupex --defaults-file=/etc/my.cnf --user=root --password='123456' /backup/full &>>$log
/usr/bin/find /backup/full/* -mtime +7 -type d | xargs rm -rf
}
add_back(){
innobackupex --defaults-file=/etc/my.cnf --user=root --password='123456' --incremental /backup/add/ --incremental-basedir=/backup/full/ &>>$log
/usr/bin/find /backup/add/* -mtime +7 -type d | xargs rm -rf
}
case "$time" in
'Monday')
if [ $mysql -ne 0 ];then
full_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
'Tuesday')
if [ $mysql -ne 0 ];then
add_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
'Wednesday')
if [ $mysql -ne 0 ];then
add_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
'Thursday')
if [ $mysql -ne 0 ];then
full_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
'Friday')
if [ $mysql -ne 0 ];then
add_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
'Saturday')
if [ $mysql -ne 0 ];then
add_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
'Sunday')
if [ $mysql -ne 0 ];then
add_back
else
/usr/bin/date "+%Y-%m-%d %H:%M:%S" >> $log
echo "Mysqld is not running" >> $log
fi
;;
esac
定时任务
00 2 * * * /root/xtrabackup.sh