腳本

$variable 什麼括號都不加時後面只能接變量

${variable} $大括號裏面括的是變量

$(command) $小括號裏面括的是命令。

$[1+2] $中括號表示算術運算

` ` 引起來的是嵌套的命令

‘’強引用,裏面所有的值都是其字符字面值

“”弱引用,如果引號裏面有$ ` \ ! 雙引號雙引號管不住他們。

shell的變量名是大小寫敏感的

clip_image002

find /etc -type f -mtime -1 找出/etc下面類型爲file 修改時間小於1天的 文件,+1代表大於1天,1代表等於1天。

[root@server11 ~]# which find

/usr/bin/find

which 看命令對應的文件在哪裏。和whereis差不多。後面只能跟命令。

(())裏面是進行的算術運算。

clip_image004

clip_image006

實現自動備份數據庫:

#!/bin/bash

dir=/backupdb

if [ -d $dir ];then

echo "目錄已經存在"

else

mkdir $dir

echo "$dir 目錄創建成功"

fi

# [ -d $dir] || mkdir $dir

for DB in $(mysql -e "show databases;" -E -N | grep -v '^*' | grep -v 'schema$')

do

echo "backing up $DB..."

dbname=${DB}_$(date +%Y-%m-%d)

mysqldump $DB > $dir/$dbname

size=$(stat --print "%s\n" $dir/$dbname)

echo "$dbname $size"

done

test命令可用於評估bash腳本中的表達式。它評估其參數所指定的表達式,如果表達式

爲true,返回零退出狀態,如果表達式爲false,則返回非零退出狀態。test具有替代語

法,使用方括號"[]"將表達式括起來,這樣更易於閱讀。

語法:test EXPRESSION 或 [EXPRESSION]

* 字符串之間的比較:

[root@server11 ~]# [ -n abc ] && echo 0 || echo -1 判斷是否是非空字符串

0

[root@server11 ~]# [ -z abc ] && echo 0 || echo -1 判斷是否是空字符串

-1

[root@server11 ~]# test bb = cc && echo 0 || echo -1 判斷兩個字符串是否相等

-1

[root@server11 ~]# test bb != cc && echo 0 || echo -1 是否不等

0

[root@server11 ~]# test bb!=cc && echo 0 || echo -1 注意:運算符和操作數之間一定要有空格》

0

[root@server11 ~]# test bb=cc && echo 0 || echo -1

0

clip_image008

clip_image010

clip_image012

if 語句:

判斷當前使用量是否小於50%

clip_image014
$(df | grep '/$' |awk '{print $5}'| sed s/%//g)這個也能取出想要的數

echo $(df | grep '/$' |awk '{print $5}'| awk -F '%' '{print $1}')

echo $(df | grep '/$' |awk '{print $5}'| cut -f1 -d%)

clip_image016

用if語句設置http虛擬主機:

clip_image018

clip_image020

case語句:

clip_image022

expect語句

clip_image024

#!/usr/bin/expect

set ip [lindex $argv 0]

set name [lindex $argv 1]

spawn ssh root@$ip

expect "(yes/no)?" {

send "yes\r"

expect "password:"

send "redhat\r"

} "password:" { 《不能和}』換行》

send "redhat\r"

} "* host" {

exit 1

}

expect "#"

send "scp [email protected]:/ect/passwd /root/ \r"

expect "(yes/no)?" {

send "yes\r"

expect "password:"

send "redhat\r"

} "password:" {

send "redhat\r"

}

foreach語句

[root@foundation11 ~]# cat ftpput.sh

#!/usr/bin/expect

set DIR [lindex $argv 0]

foreach ip {

172.25.11.10

172.25.11.11

} {

spawn ssh root@$ip

expect "(yes/no)?" {

send "yes\r"

expect "password"

#send "redhat\r"

} "password" {

send "redhat\r"

} " host" {exit 1}

expect "#"

send "chown ftp /var/ftp/pub \r"

send "setsebool ftpd_anon_write on \r"

send "chcon -R -t public_content_rw_t /var/ftp/pub \r"

send "echo anon_upload_enable=YES >> /etc/vsftpd/vsftpd.conf \r"

send "systemctl restart vsftpd \r"

send "firewall-cmd --add-service=ftp\r"

send "exit \r"

expect eof

}

foreach ip {

172.25.11.10

172.25.11.11

} {

spawn lftp $ip

expect ">"

send "cd /pub \r"

send "lcd /mnt/bai \r"

send "mput * \r"

send "exit \r"

expect eof

}

shell裏面調用expect語句:

第一種方法是把shell和expect寫在兩個文件裏面:

[root@foundation11 test]# cat passwd.txt

172.25.11.10 redhat

172.25.11.11 westos

[root@foundation11 test]# cat ssh.exp

#!/usr/bin/expect

set ip [lindex $argv 0]

set passwd [lindex $argv 1]

spawn ssh root@$ip

expect "(yes/no)?" {

send "yes\r"

expect "password"

send "$passwd\r"<$passwd 和 \r 之間沒有空格,比如$passwd \r 會被解析成redhat空格>

} "password" {

send "$passwd\r"

} " host" {exit 1}

expect "#"

send " hostname;exit \r"

expect eof

[root@foundation11 test]# cat ssh.sh

#!/bin/bash

for ip in `awk '{print $1}' passwd.txt`

do

pass=`awk -v i="$ip" '{if(i==$1) print $2}' passwd.txt`

echo "ip: $ip pass: $pass"

expect ssh.exp $ip $pass

done

第二種方法,把expect語句寫到shell裏面:

clip_image026

一個防惡意登陸的例子。

vim /etc/rsyslog.conf

clip_image028

有些壞人嘗試多次登陸desktop來破解desktop的密碼。上面這個配置文件裏面設置了認證相關的日誌記錄在/var/log/secure裏面。在壞人多次嘗試後,這個文件的內容如下:

clip_image030

我們要計算出嘗試登陸次數大於10次而且沒有成功的ip。然後把這個ip 加入/etc/hosts.deny裏面。

下面是我寫的腳本

clip_image032

看,執行結果:

clip_image034

下面是/etc/hosts.deny裏面的內容:

clip_image036自動多了下面這兩行

下面讓這個腳本自動定期執行:

[root@desktop11 ~]# crontab -e

clip_image038

clip_image040

部分代碼轉自白雪嬌的文檔   如有侵權,請聯繫作者

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章