Common Shell Instances

Source: Internet
Author: User
Tags file upload script disk usage

1. Exclude portions of files from disk usage statistics

Du--exclude "*.iso" dir

2. Find the maximum of 10 files in a specified directory

Find-type f-exec du-k {} \ |sort-nrk 1 |head-n 10

3. List the maximum 10 commands for the current system

Cat. Bash_history | awk ' {print '} ' | Sort | uniq-c | Sort-nr | Head-10

4. Count the amount of memory that is occupied by all processes

Ps-eo RSS | The memory for awk ' BEGIN {print ' is: '}/^ *[0-9]/{total + = $} END {print all ' KiB '} '

Instance:

Example 1: Collect system Load data, and analyze system hotspot by gnuplot drawing.

/opt/cpuload.sh

#!/bin/bash

# Create by WxH 2015-07-05

# version:v0.1


# Collect CPU load data in a certain format and store it in the/tmp/mydata file.

Uptime | awk ' {print $, $ (NF-2), $ (NF-1), $NF} ' | Sed ' s/,//g ' >>/tmp/mydata


# Call the Gnuplot software to draw on the collected data.

Gnuplot <<eof


Set terminal png tiny font '/USR/SHARE/FONTS/LIBERATION/LIBERATIONSANS-REGULAR.TTF '

Set Output '/var/www/html/loadavg.png '

Set XData time

Set timefmt '%h:%m:%s '

Set Xlabel ' time '

Set format x '%h:%m '

Set Xtics rotate

Set Ylabel ' Load average '


Plot '/tmp/mydata ' u 1:2 t ' 1-min ' with lines, '/tmp/mydata ' u 1:3 t ' 5-min ' with

Lines, '/tmp/mydata ' u 1:4 t ' 15-min ' with lines


Eof


# define cycle tasks, execute scripts once per minute.

Crontab-e

* * * * * */opt/cpuload.sh


# Publish gnuplot-drawn graphics through the HTTP service for easy viewing by users.

Cat/var/www/html/gnuplot.html




<a href= "/loadavg.png" >load average</a>



Example 2: Writing an interactive script


/opt/check_system.sh

#!/bin/bash

# Create by WxH 2015-07-05

# version:v0.1


# while loop displays interactive interface

While True

Do

# Show Interactive Content

Echo-e "

\033[31m A \033[0m Display host IP address

\033[32m B \033[0m Show Disk Idle

\033[33m C \033[0m Display system run time

\033[34m Q \033[0m Exit

"

# Read user input from Terminal read-p "Please enter your choice:" num

# Set multi-task branch case $num in

a| A

Ifconfig Br0 | grep "inet Addr:" | awk ' {print $ ': ' $4} ' | Awk-f: ' {print $ '/' $4} ';;

b| B

df-h | grep/$ | awk ' {print $} ';;

c| C

Uptime | awk ' {print $} ' | Tr-d ', ' | Awk-f: ' {print ' system has been running ' $ ', ' $ ' $ ' min '} '

;;

q| Q) exit;

*)

echo "Please don't be capricious, need to choose [a| b| C] "

Esac

Done


Example 3: Writing Nginx service scripts

/etc/ini.t/nginx

#!/bin/bash

# Create by WxH 2015-07-05

# version:v0.1

. /etc/rc.d/init.d/functions

Nginx=/usr/local/nginx/sbin/nginx

Prog=nginx

Pidfile=/usr/local/nginx/logs/nginx.pid

Retval=0

Start () {

Echo-n $ "Starting $prog:"

$nginx-T &>/dev/null

If [$?-eq 0];then

$nginx &>/dev/null && Success | | Failure

Fi

Echo

Retval=$?

Return $RETVAL

}

Stop () {

Echo-n $ "Stopping $prog:"

$nginx-S Stop &>/dev/null && Success | | Failure

Echo

Retval=$?

}

Reload () {

Echo-n $ "Reloading $prog:"

$nginx-S Reload &>/dev/null && Success | | Failure

Echo

}

# See how we were called.

Case "$" in

Start

Start

;;

Stop

Stop

;;

Restart

Stop

Start

;;

Reload

Reload

;;

Status

Status-p ${pidfile} $nginx

Retval=$?

;;

Help

$nginx-H

Retval=$?

;;

*)

echo $ "Usage: $prog {start|stop|restart|reload|help|status}"

Retval=1

Esac

Exit $RETVAL


Example 4: Production Environment batch detection Web URL, mail, alarm.


/opt/check_weburl.sh

#!/bin/bash

# Create by WxH 2015-07-08

# version:v0.1

#应用系统提供的脚本, the methods in this script are used, such as action.

. /etc/init.d/functions

Retval=0 #定义默认返回值为0

address=1206160700 #定义好友ID

Content= "/tmp/checkurl.txt"

function GetStatus () {#定义函数

Failcount=0 #定义FAILCOUNT变量的默认值

#使用wget循环检测3次指定url的状态, if the detection fails Failconut value +1

For ((i=1;i<=3;i++))

Do

Wget-t5-t1--spider http://$1 &>/dev/null

[$?-ne 0] && let failcount+=1

Done #判断FAILCOUNT的值如果大于1的话就报警.

If [$FAILCOUNT-gt 1];then

Retval=1 #检测失败后返回值为1

Ntime= ' date + '%y-%m-%d%h:%m:%s ' #定义时间格式

echo "Http://$1 is down, ${ntime}." > $CONTENT #定义报警内容

php/usr/local/weixin-private-api/test.php $ADDRESS &>/dev/null

#调用接口报警

Else

Retval=0 #检测成功返回值依然为0

Fi

return $RETVAL #返回值

}

#判断urllist A. txt file exists

if [!-f/opt/urllist.txt];then

echo Urllist.txt not found

Exit 1

Fi


#调用上面定义的函数getstatus () Loop detection URL list

For URL in ' Cat/opt/urllist.txt '

Do

Echo-n "Checking $URL:"

GetStatus $URL && Action Health/bin/true | | Action hangs off the/bin/false

Done


At the end of the script content, the following is the contents of the Urllist.txt file called in the script, one line at a:

# Cat/opt/urllist.txt

www.westos.org

192.168.0.145


Example 5: IP blocking password attempts to fail too many times


/opt/check_secure.sh

#!/bin/bash

# Create by WxH 2015-07-08

# version:v0.1


#过滤认证失败的主机IP和失败次数, and store data to file

Cat/var/log/secure|awk '/failed/{print $ (NF-3)} ' |sort|uniq-c|awk ' {print $ "=" $;} ' >/tmp/black.txt


#设定变量, exceeding this value will be masked by the script

Baseline= "20"


#循环检测过滤到的数据文件

For i in ' cat/tmp/black.txt '

Do

Ip= ' echo $i |awk-f= ' {print '} ' #取出主机ip

Num= ' echo $i |awk-f= ' {print $} ' #取出失败次数


If [$NUM-gt $BASELINE];then

#在屏蔽IP之前先确认是否此IP是否已经存在

grep $IP/etc/hosts.deny >/dev/null

If [$?-gt 0];then

echo "sshd: $IP" >>/etc/hosts.deny

Fi

Fi

Done


You can put the script into the cycle task and automatically detect it regularly.


Expect usage:

#!/usr/bin/expect

Tell the operating system that the code in the script uses that Shell to execute.

Set Timeout 30

Sets the time-out, in seconds, timeout-1 to never time out.


Spawn Ssh-l User 192.168.0.1


Spawn is a expect internal command that can be executed after entering the expect environment, and its main function is to add a shell to the SSH running process to pass the interactive instructions.


Expect "Password:"

Determine if the last output contains the string "Password:", if any, return immediately, otherwise wait for a period of time to return, where the waiting time is the previous set of 30 seconds.


Send "ispass\r"

This performs an interactive action that is equivalent to the action of manually entering a password. Do not forget to add "\ r" to the end of the command string.


Interact

After the completion of the implementation of the interactive State, the control to the console, this time can be manually operated.


$ARGV parameter array

The expect script can accept parameters passed from bash. can be obtained using [lindex $argv n], n starting from 0, representing the first, second, third .... Parameters


Instance 6:expect automatic Interactive scripting


/opt/auto1.sh

#!/usr/bin/expect

Spawn ssh 192.168.0.120

Expect "(yes/no)?" {

Send "yes\r"

Expect "Password:"

Send "westos\r"

} "Password:" {send "westos\r"} "*host" {Exit 1}

Expect "#"

Send "df-h\n"

Send "exit\n"

Expect EOF


Example 7: Automatic file Upload script


/opt/upload.sh

#!/usr/bin/expect

Set DIR [lindex $argv 0]

Spawn Lftp 192.168.2.251

Expect "~>"

Send "CD pub\r"

Expect "pub>"

Send "Mput $DIR/*\r"

Send "exit\r"

Expect EOF


Example 8: Bulk host operations (same password)


/opt/auto2.sh

#!/usr/bin/expect

foreach IP {

172.25.0.1

172.25.0.2

} {

Spawn Ssh-l Root $ip

Expect "*password:"

Sleep 1

Send "westos\r"

Expect "root"

Send "SCP [email protected]:/shell/check.sh/mnt\r"

# # #注意第一次连接会提示yes/no

Expect "Pasword:"

Send "123456\r"

Expect EOF

}


Example 9: Bulk host operations (different passwords)


# Cat Passwd.txt

192.168.2.60=westos

192.168.2.70=redhat


# Cat Hello.sh

#!/bin/bash

#for i in ' Cat passwd.txt '

#do

# ip= ' echo $i | awk-f= ' {print '} '

# pass= ' echo $i | Awk-f= ' {print $} '

#done


For IP in ' awk-f= ' {print \ Passwd.txt '

Do

Pass= ' awk-f=-v i= "$ip" ' {if (i==$1) print $ ' passwd.txt '

Expect Login.exp $ip $pass


Done


# Cat Login.exp

#!/usr/bin/expect

Set IP [lindex $argv 0]

Set PASS [lindex $argv 1]


Spawn ssh [email protected] $IP

Expect "(yes/no)?" {

Send "yes\r"

Expect "Password:"

Send "$PASS \ r"

} "Password:" {send "$PASS \ r"} "* Host" {Exit 1}

Expect "$"

Send "hostname;exit\r"

Expect EOF

Common Shell Instances

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.