天天看點

shell 腳本基礎

!/bin/bash

判斷172.40.51.0網段有多少是開關機狀态

x=0

y=0

for i in <code>seq 254</code>

do

ping -c1 -i0.1 -W1 172.40.51.$i

if [ $? -eq 0 ];then

echo "172.40.51.$i is up"

let x++

else

echo "172.40.51.$i is down"

let y++

fi 

done

echo "開機的有$x個,關機的有$y個。"

1加到100的和

tmp=0

for i in {1..100} 

let tmp=$i+$tmp

echo "總和:$tmp"

随機猜獎

u=0

num=$[ RANDOM%100+1 ]

while :

read -p "[1-100]随即猜:" ca

if [ $ca -eq $num ];then

echo “恭喜中獎”

exit

elif [ $ca -gt $num ];then

echo “猜大了”

echo “猜小了”

fi

let u++

echo "你猜了$u次"

閹割版if

case "$1" in

-n)

touch $2;;

-e)

vim $2;;

-c)

cat $2;;

-r)

rm -rf $2;;

*)

echo "usage:$0 (-n|-e|-c|-r) 檔案名"

esac

加法計算機

read -p "請輸入一個數[0結束]:" shu

let tmp+=$shu

[ $shu -eq 0 ] &amp;&amp; break

echo "總和$tmp"

99乘法表

for i in {1..9}

for j in <code>seq $i</code>

echo -n " $i$j=$[ij]"

棋盤

for i in {1..10}

do 

for j in {1..10}

he=$[i+j]

if [ $[$he%2] -eq 0 ];then

echo -en "\033[45m \033[0m" 

echo -en "\033[47m \033[0m"

使用者建立

read -p "請建立使用者:" user

echo "必須建立使用者"

if [ -z "$user" ];then

[ $? -ne 0 ] &amp;&amp; exit

[ -n "$user" ]&amp;&amp; break

stty -echo

read -p "請建立密碼:" pass

stty echo

pass=${pass:-123456}

useradd "$user"

echo "$pass" |passwd --stdin "$user"

rm /root/.ssh/known_host

i=172.25.0.10

expect &lt;&lt; EOF

set timeout 30

spawn ssh $i

expext yes {send "yes\n"}

expect password {send "redhat\n"}

expect # {send "touch /qq\n"}

expect # {send "ls\n"} 最後一條不執行

EOF

i=<code>uptime |awk -F: '{print $5}'</code>

echo "CPU 1-15分鐘的負載的值是:$i"

y=<code>ifconfig eth0 |awk '/RX packets/{print $6,$7}'</code>

##echo "RX 接受的資料流量是:$y"

u=<code>ifconfig eth0 |awk '/TX packets/{print $6,$7}'</code>

echo "TX 發送的資料流量是$u"

p=<code>free |awk '/Mem:/{print $4}'</code>

echo " 記憶體剩餘:"$p"K"

q=<code>df|awk '/\//{print $4}'</code>

echo "根分區的剩餘容量:"$q"K"

w=<code>who |awk '{print $1}'</code>

echo "已登陸的使用者:$w"

a=<code>ps aux |wc -l</code>

echo "程序數量:$a"

s=<code>rpm -qa |wc -l</code>

echo "已裝包數量:$s"

ls / |awk '/nginx.*.gz/'

tar -xf nginx-1.8.0.tar.gz

echo "沒有nginx-1.8.0.tar.gz包"

cd nginx-1.8.0

yum clean&gt;/dev/null

yum repolist

yum -y install gcc pcre-deve openssl-devel&gt;/dev/null

xi(){

echo -ne '\033[42m \033[0m'

sleep 1

}

xi &amp;

./configure &gt;/dev/null

make&gt;/dev/null

make install&gt;/dev/null

cd ..

kill $!

i=<code>ls /usr/local/nginx/</code>

if [ $? -eq 0 ];then 

echo OK

echo 失敗 

nginx的啟動服務

case $1 in

start)

/usr/local/nginx/sbin/nginx;;

stop)

/usr/local/nginx/sbin/nginx -s stop;;

restart)

/usr/local/nginx/sbin/nginx -s stop

status)

netstat -ntulp |grep nginx;;

echo "請正确輸入";;

awk '/Failed/{print $11}' /var/log/secure &gt;&gt; ip.txt

if [ -d ip.txt ];then

echo ip.txt沒有内容

u=<code>cat ip.txt</code>

for i in $u

iptables -I INPUT -s 172.25.0.$i -j DROP 

     本文轉自夜流璃雨 51CTO部落格,原文連結:http://blog.51cto.com/13399294/2055319,如需轉載請自行聯系原作者

繼續閱讀