shell 脚本

shell 清理log ,

#/bin/bash

find /var/log/hadoop/ocdp/ -mtime +2 -name "hdfs-audit.log.*" -exec rm -rf {} \;

find . -name "*.log.*" -exec du -k {} \; |awk '{sum+=$1}END{print sum}'


————————————————————————————————————————————————

ssh 登录跳转脚本,不需要每次去登录了, 直接执行./tiao.sh 就可以登录到目标主机上了。

cat tiao.sh            // 跳了两次

#!/usr/bin/expect 

set timeout 3

spawn ssh [email protected]

expect "*password:"

send "23pBZHjg4y\r"

#interact

expect "]*"

send "ssh [email protected] \r"

expect "*password:"

send "8Wfy_zwy\r"

expect "]*"

send "su - ocdp \r"

interact





#!/usr/bin/expect           实现自动su , 自动跳转

spawn su - aisware

expect "Password:"

send "qW&Pa5gW3gnJ0#cw\r"

#interact

send "ssh aisware@hohhot031 \r"

expect "*password:"

send "qW&Pa5gW3gnJ0#cw\r"

interact


SSH 互信 ,脚本实现yes, 密码 输入。 脚本执行完成后,会更改 PS1 变量。 需执行  PS1='[\u@\h \w]\$ ' 

#!/bin/bash

cat /home/ocdp/hostliu.txt |while read hostline

do

/usr/bin/expect << EOF

set timeout 3

spawn ssh-copy-id -i /home/ocdp/.ssh/id_rsa.pub ocdp@$hostline

expect {

    "*yes/no" { send "yes\r"; exp_continue }

    "*password:" { send "Asia%2019\r" }

    }

#  expect "*$"

#  send "hostname -f\r"

#    expect "*$"

#    send "exit\r"

#interact

expect eof

EOF

done



check_alive.sh
#!/bin/bash

if [ ! -f /tmp/check.txt ];then

  touch /tmp/check.txt

else

  >/tmp/check.txt

fi

ip=172.20.54.

for i in `seq 21 140`

do

 ping -w 1 -c 1 ${ip}${i}  >/dev/null

   if [ $? -eq 0 ];then

     echo "${ip}${i} .........is live" >>/tmp/check.txt

   else

      echo "${ip}${i} .........is unlive" >>/tmp/check.txt

   fi

done



mysqlbak.sh
#!/bin/sh

MYUSER=root

MYPASS=Test%0328

SOCKET=/usr/local/mysql/mysql.sock

MYLOGIN="mysql -u$MYUSER -p$MYPASS -S $SOCKET "

MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S$SOCKET -B"

DATABASE="$($MYLOGIN -e "show databases;" |egrep -vi "Data|_schema|mysql|sys")"

echo $DATABASE

for dbname in $DATABASE

  do

  MYDIR=/server/backup/$dbname

  [ ! -d $MYDIR ] && mkdir -p $MYDIR

  $MYDUMP $dbname|gzip >$MYDIR/${dbname}_$(date +%F).sql.gz

done



实时网速统计:
#!/bin/sh

if [ "$1" = "" ];then  #判断后面是否有跟参数

  echo -e "\n  use interface_name after the script,like \"script eth0\"...\n"

  exit -1

fi

echo -e "\n  start monitoring the $1,press \"ctrl+c\" to stop"

echo ----------------------------------------------------------

file=/proc/net/dev  #内核网卡信息文件

while true

  do

  RX_bytes=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $2}'`  #这里sed这一步为了同时兼容centos6和7

  TX_bytes=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $10}'`

  sleep 10

  RX_bytes_later=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $2}'`

  TX_bytes_later=`cat $file|grep $1|sed 's/^ *//g'|awk -F'[ :]+' '{print $10}'`

  #B*8/1024/1024=Mb

  speed_RX=`echo "scale=2;($RX_bytes_later - $RX_bytes)/1024/1024"|bc`

  speed_TX=`echo "scale=2;($TX_bytes_later - $TX_bytes)/1024/1024"|bc`

  printf "%-3s %-3.1f %-10s %-4s %-3.1f %-4s\n" IN: $speed_RX Mb/s OUT: $speed_TX Mb/s

done



#!/bin/bash

for i in {1..9}

do

  umount /data0$i

done

umount /data10

#格式化硬盘

PIDARRAY=()

for i in b c d e f g h i j k

do

  parted -s /dev/sd$i mklabel gpt

  parted -s /dev/sd$i mkpart primary 0% 100%

  mkfs.ext4 -T largfile /dev/sd$i"1" &

  PIDARRAY+=("$!")

  echo "sd$i"1" is formatting.....,please wait!!!!!"

done

wait ${PIDARRAY[@]}

if [ $? -eq 0 ]; then

  echo " disk has been formatted successful"

else

  exit 110

fi

#修改自动挂载

sed -i s/UUID/#UUID/g /etc/fstab

echo "/dev/sdb1 /hdfs/data1 ext4 defaults 1 2" >> /etc/fstab

echo "/dev/sdc1 /hdfs/data2 ext4 defaults 1 2" >> /etc/fstab

echo "/dev/sdd1 /hdfs/data3 ext4 defaults 1 2" >> /etc/fstab

echo "/dev/sde1 /hdfs/data4 ext4 defaults 1 2" >> /etc/fstab

echo "/dev/sdf1 /hdfs/data5 ext4 defaults 1 2" >> /etc/fstab


mount /dev/sdb1 /hdfs/data1

mount /dev/sdc1 /hdfs/data2

mount /dev/sdd1 /hdfs/data3

mount /dev/sde1 /hdfs/data4

mount /dev/sdf1 /hdfs/data5


ambari 元数据备份
#!/bin/sh

now=`date "+%y%m%d"`

export PATH=/usr/bin:/usr/local/bin:$PATH

cd /home/ocdc/mysqlbak_ambari

mysqldump -h10.299.31.250 -uroot -pAsia%222 --set-gtid-purged=OFF --databases ambari --triggers --routines --events >mysql_ambari_${now}.sql

echo "==mysqldata dump Completed !=="

#backup to remote server

tar -zcvf mysql_ambari_${now}.tgz  mysql_ambari_${now}.sql --remove-files

scp /home/ocdc/mysqlbak_ambari/mysql_ambari_${now}.tgz 10.299.31.242:/home/ocdc/mysqlbak_ambari/

scp /home/ocdc/mysqlbak_ambari/mysql_ambari_${now}.tgz 10.299.31.130:/data02/mysqlbak_ambari/

#rm 7 days ago

find /home/ocdc/mysqlbak_ambari/  -mtime +7 -name 'mysql_ambari_*.tgz' -exec rm {} \;

ssh 10.299.31.242 "find /home/ocdc/mysqlbak_ambari/  -mtime +7 -name 'mysql_ambari_*.tgz' -exec rm {} \;"

ssh 10.299.31.130 "find /data02/mysqlbak_ambari/  -mtime +7 -name 'mysql_ambari_*.tgz' -exec rm {} \;"

exit 0




tomcat  定时重启

#!/bin/bash

#. /etc/profile

tomcatPath="/usr/local/apache-tomcat-9.0.19"

binPath="$tomcatPath/bin"

echo "[info][$(date +'%F %H:%M:%S')]正在监控tomcat,路径:$tomcatPath"

pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`

if [ -n "$pid" ]; then

echo "[info][$(date +'%F %H:%M:%S')]tomcat进程为:$pid"

echo "[info][$(date +'%F %H:%M:%S')]tomcat已经启动,准备使用shutdown命令关闭..."

$binPath"/catalina.sh" stop

sleep 2

pid=`ps -ef | grep tomcat | grep -w $tomcatPath | grep -v 'grep' | awk '{print $2}'`

if [ -n "$pid" ]; then

echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭失败,准备kill进程..."

kill -9 $pid

echo "[info][$(date +'%F %H:%M:%S')]kill进程完毕!"

sleep 1

else

echo "[info][$(date +'%F %H:%M:%S')]使用shutdown命令关闭成功!"

fi

else

echo "[info][$(date +'%F %H:%M:%S')]tomcat未启动!"

fi

echo "[info][$(date +'%F %H:%M:%S')]准备启动tomcat..."

$binPath"/catalina.sh" start



0 0 * * * nmon -s10 -c8640 -f -m /home/ocdc/app/nmon_monitor >/dev/null 2>&1

你可能感兴趣的:(shell 脚本)