Linux Shell 运维脚本功底积累

1、删除Linux远程用户连接会话

[root@tradx24 logs]# w
 10:45:28 up 15 days, 16:23,  4 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Sun21    4days  0.00s  0.00s -bash
root     pts/0    192.168.1.2    09:11    0.00s  0.07s  0.00s w
root     pts/2    192.168.1.2    09:45   30:53   0.07s  0.07s -bash
root     pts/3    192.168.1.8    10:20   22:05   0.02s  0.00s vi operation.log
[root@tradx24 logs]# skill -KILL -v pts/3
pts/3    root     21217 vi              
pts/3    root     24331 bash            

2、通过ssh远程执行命令

[root@tradx24 bin]# iip="192.168.1.10"
[root@tradx24 bin]# ssh $iip w
 10:54:13 up 125 days, 20:17,  1 user,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                21Mar14  4days  0.09s  0.09s -bash

3、通过ssh远程批量执行MySQL脚本

#ip
list1=(`cat ../tools/dblist | grep -v \# | awk {print $1} | sed "1,1d"`)
#user
list2=(`cat ../tools/dblist | grep -v \# | awk {print $2} | sed "1,1d"`)
#password
list3=(`cat ../tools/dblist | grep -v \# | awk {print $3} | sed "1,1d"`)
for((i=0;i<=${#list1[@]};i++));
do
        iip=`echo ${list1[i]} | awk -F "." {print $4}`
        ssh ${list1[i]}  mkdir /home/sqlScrip/
        ssh ${list1[i]}  mkdir /home/sqlScrip/$iip
        clear
        scp ../sqlScrip/$iip/* ${list1[i]}:/home/sqlScrip/$iip
        clear
        #path
        list4=(`ls ../sqlScrip/$iip`)

        for((w=0;w<=${#list4[@]};w++));
        do
                if [ "117" == "$iip" ]
                then
                        ssh ${list1[i]}  "mysql -h ${list1[i]} -u ${list2[i]} -p${list3[i]} < /home/sqlScrip/${iip}/${list4[w]};"
                        clear
                fi
        done
        ssh ${list1[i]} ‘rm -rf /home/sqlScrip‘
done

4、通过svn下载war包

#list第五行为下载地址
list=(`cat ../tools/list  | awk {print $5} | sed "1,1d"`)
for((i=0;i<${#list[@]};i++));
do
        curl -u wangyong:Abcd1234 -O ${list[i]}

        war=`echo ${list[i]} | awk -F "/" {print $NF}`
        mv $war ../workapp/
done

5、解压war包到指定目录

ssh ${ip} unzip ${tomcat目录}/webapps/${war包名称}.war -d  ${tomcat目录}/webapps/${指定目录}

6、多Linux服务器建立信任关系

ssh-keygen -t rsa
cd ~/.ssh
scp -r id_rsa.pub $ip:/root/.ssh/$ip
ssh $ip touch ~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys
ssh $ip "cat ~/.ssh/$ip >> ~/.ssh/authorized_keys"

7、删除某目录下空文件

find ../new/$serve/properties -type f -size 0 -exec rm -f {} \;

8、删除某目录下小于10b的文件

#Delete the files smaller than 10B
        for i in `ls -l ../new/$serve/properties/ | sed -e 1d | awk $5 < 10 {print$NF}`
        do
                rm -rf ../new/$serve/properties/$i
        done

9、Linux通过alias设置快捷命令

command(){
list1=(`cat ~/.bashrc | grep custom`)
for((i=0;i<=${#list1[@]};i++));
do
        if [[ "${list1[i]}" = "#custom" ]]
        then
                echo "[warn] The command has already been initialized,there‘s no need to repeat the operation, thank you!"
                break
        else
                path=`pwd`
                #初始命令
                #Initialization commands.
                echo "#custom" >> ~/.bashrc
                echo "alias l=‘ll -al‘" >> ~/.bashrc
                echo "alias conf=‘cd `echo $path`/../conf‘" >> ~/.bashrc
                echo "alias bin=‘cd `echo $path`/../bin‘" >> ~/.bashrc
                echo "alias new=‘cd `echo $path`/../new‘" >> ~/.bashrc
                echo "alias too=‘cd `echo $path`/../tools‘" >> ~/.bashrc
                echo "alias hostlist=‘cat `echo $path`/../tools/.hostlist‘" >> ~/.bashrc
                echo "alias un=‘cd `echo $path`/../‘" >> ~/.bashrc
                echo "alias bak=‘cd `echo $path`/../bak‘" >> ~/.bashrc
                echo "alias app=‘cd `echo $path`/../workapp‘" >> ~/.bashrc
                echo "alias ..=‘cd ../‘" >> ~/.bashrc
                source ~/.bashrc
        fi
done
}

10、引用其他shell脚本方法

## ====================================================##
## auth:wonter                                         ##
## date:2014-06-12                                     ##
## path:javame.cnblogs.com                             ##
## ====================================================##
#!/bin/sh
#引用tool.sh脚本中方法
. ../tools/tool.sh
#Parameters
par
#Capture ip
#Capture path
config

11、获取脚本自身名称

#Automatically obtain configuration files
name=`echo $0`
uname=`echo $name | awk -F "/" {print $2} | awk -F "." {print $1}`
#Uniform configuration
serve="$uname"

12、检查是否存在文件夹,没有则新建文件夹

bak_mk="../bak/$serve"
new_mk="../new/$serve"
if [ ! -d $bak_mk ]; then
mkdir $bak_mk
fi
if [ ! -d $new_mk ]; then
mkdir $new_mk
fi

13、统计脚本执行时间

     st=`date +"%Y-%m-%d %H:%M:%S"`
   #执行方法
start en=`date +"%Y-%m-%d %H:%M:%S"` st_c=`date -d "$st" +%s` en_c=`date -d "$en" +%s` interval=`expr $en_c - $st_c` echo "Start start-up time :${st}" echo "Start end time :${en}" echo "total consuming time :${interval} 秒"

14、shell定义集合

#name
list1=(`cat ../tools/list | grep -v \# | awk {print $1} | sed "1,1d"`)
#ip
list2=(`cat ../tools/list | grep -v \# | awk {print $2} | sed "1,1d"`)
#serve
list3=(`cat ../tools/list | grep -v \# | awk {print $3} | sed "1,1d"`)
for((i=0;i<=${#list1[@]};i++));
do
        cp ../conf/.model ../conf/${list1[i]}.conf
        cp ../bin/.model.wy ../bin/${list1[i]}.wy
        cp ../bin/.model.yw ../bin/${list1[i]}.yw
done

15、记录脚本执行者用户与IP

#Perform operation
echo "operator:"
read me
a=`tty | awk -F "/dev/" {print $2}`
date "+%Y-%m-%d %H:%M:%S" >> ../logs/operation.log
echo `w | grep $a | awk {print $3}` "${me} > Perform operation: one" >> ../logs/operation.log

16、shell中的正则语句

for i in `cat ../conf/$confs  | grep start | awk -F ">>" {print $2} | awk -F "_" {print $1}`
do
        if [[ "${i}" == "shiro" ]]
        then
                sed -n -e />>shiro/,/>>shiro/p  $confs |grep -v >>shiro > $pro_shi
                cat $pro_shi | col -b > ../temp/m1
                cat ../temp/m1 > $pro_shi
        elif [[ "${i}" == "jdbc" ]]
        then
                sed -n -e />>jdbc/,/>>jdbc/p  $confs |grep -v >>jdbc > $pro_jdbc
                cat $pro_jdbc | col -b > ../temp/m1
                cat ../temp/m1 > $pro_jdbc
        fi
done

17、shell写个定时进程(不推荐我这个写法,应该有更好!)

## ====================================================##
## auth:wonter                                         ##
## date:2014-06-12                                     ##
## to  :timing.sh                                      ##
## ====================================================##
#!/bin/sh
echo "请输入定时阀值(格式:`date +"%Y-%m-%d %H:%M"` )"
        read timing
echo "定时任务设置完毕,请安心等待..."
ti(){
while true
do
        time=`date +"%Y-%m-%d %H:%M"`

        if [[ "${time}" == "${timing}" ]]
        then
                echo "定时任务:"
                #定时清理日志
                rm -rf ../logs/*
                #定时备份日志
                #定时执行脚本
                break
        fi
done
}
ti&

18、通过参数方式执行脚本 tools.sh

#!/bin/sh
trust(){
#建立信任
echo "请输入信任机ip:(提示:需输入三次密码!)"
read ip
trusts
}
command(){
#初始化命令
command
}
case "$*" in
        trust)
        trust
        ;;
        command)
        command
        ;;
        *)
        echo "----------------------------------------"
        echo "pls : http://javame.cnblogs.com  welcome"
        echo "----------------------------------------"
        echo "  主机信任     | or | 初始化命令(初始)  "
        echo "tools.sh trust | or | tools.sh command"
        echo "----------------------------------------"
        ;;
esac

19、终端发送回会话给其他用户 限root用户

echo "输入您想说的话吧:"
read spk echo $spk
>/dev/stdin >/dev/pts/2

20、某目录下所有文件统一批量修改ip

sed -i  "s/10.0.0.1/10.0.1.2/g" `grep "10.0.0.1" -rl /opt/uer/wy`

21、链接数与进程数

#链接数
netstat -n | awk /^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}
#统计httpd协议连接数进程数
ps -ef|grep httpd|wc -l
ps aux|grep httpd|wc -l
#句柄数
lsof -n|awk {print $2}|sort|uniq -c|sort -nr|more 
#查看进程占用内存
ps aux |awk ($1 ~apache) && ($6>50) {print $0}

22、Linux查看线程的三种方法

1、top -H
手册中说:-H : Threads toggle
加上这个选项启动top,top一行显示一个线程。否则,它一行显示一个进程。
2、ps xH
手册中说:H Show threads as if they were processes
这样可以查看所有存在的线程。
3、ps -mp <PID>
手册中说:m Show threads after processes
这样可以查看一个进程起的线程数。

23、查看连接某服务端口最多的的IP地址

netstat -nat | grep "192.168.1.20:8443" |awk {print $5}|awk -F: {print $4}|sort|uniq -c|sort -nr|head -20

24、常见压缩解压命名

.tar 
解包:tar xvf FileName.tar
打包:tar cvf FileName.tar DirName
(注:tar是打包,不是压缩!)

.gz
解压1:gunzip FileName.gz
解压2:gzip -d FileName.gz
压缩:gzip FileName
.tar.gz 和 .tgz
解压:tar zxvf FileName.tar.gz
压缩:tar zcvf FileName.tar.gz DirName

.bz2
解压1:bzip2 -d FileName.bz2
解压2:bunzip2 FileName.bz2
压缩: bzip2 -z FileName
.tar.bz2
解压:tar jxvf FileName.tar.bz2
压缩:tar jcvf FileName.tar.bz2 DirName

.bz
解压1:bzip2 -d FileName.bz
解压2:bunzip2 FileName.bz
压缩:未知
.tar.bz
解压:tar jxvf FileName.tar.bz
压缩:未知

.Z
解压:uncompress FileName.Z
压缩:compress FileName
.tar.Z
解压:tar Zxvf FileName.tar.Z
压缩:tar Zcvf FileName.tar.Z DirName
.zip
解压:unzip FileName.zip
压缩:zip FileName.zip DirName

.rar
解压:rar x FileName.rar
压缩:rar a FileName.rar DirName 

25、厚积薄发,持续更新积累,请关注javame.cnblogs.com

 

注:转载请添加出处

Linux Shell 运维脚本功底积累,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。