Linux—shell腳本實例進階篇
VSole2021-09-23 22:23:47
實驗一
利用case語句編寫腳本,滿足下列要求
1.執行create時根據userfile和passfile建立用戶
2.執行delete時根據userfile刪除用戶
1.編寫腳本:
[root@localhost mnt]# vim user_ctrl.sh#!/bin/bashread -p "Please input the operation (create or delete ): " OPERATION //輸入你要執行的動作case $OPERATION in create) //第一種情況:create read -p "Please input the userfile : " USERFILE //提示輸入文件 [ -e $USERFILE ] || { //判斷是否存在 echo "$USERFILE is not exist " exit 1 } read -p "Please input the passwdfile :" PASSFILE [ -e $PASSFILE ] || { echo "$PASSFILE is not exist " exit 1 } USERLINE=`awk 'BEGIN{N=0}{N++}END{print N}' $USERFILE` //計算userfile文件行數 for LINE_NUM in `seq 1 $USERLINE` //利用循環建立 do USERNAME=`sed -n "${LINE_NUM}p" $USERFILE` //截取userfile文件第一行內容 PASSWORD=`sed -n "${LINE_NUM}p" $PASSFILE` //截取passfile文件第一行內容 useradd $USERNAME //建立用戶 echo $PASSWORD | passwd --stdin $USERNAME done ;; delete) //第二種情況:delete read -p "Please input the userfile :" USERFILE [ -e $USERFILE ] || { echo "$USERFILE is not exist " exit 1 } USERLINE=`awk 'BEGIN{N=0}{N++}END{print N}' $USERFILE` for LINE_NUM in `seq 1 $USERLINE` do USERNAME=`sed -n "${LINE_NUM}p" $USERFILE` userdel -r $USERNAME done ;; *) //第三種情況:其余各種情況 echo Eorror! ;;esac

2.執行:
[root@localhost mnt]# cat userfile user1user2user3[root@localhost mnt]# cat passfile 123456789[root@localhost mnt]# sh user_ctrl.sh user Please input the operation (create or delete ): hello //輸入錯誤動作Eorror![root@localhost mnt]# sh user_ctrl.sh user Please input the operation (create or delete ): createPlease input the userfile : user //輸入錯誤文件user is not exist [root@localhost mnt]# sh user_ctrl.sh user Please input the operation (create or delete ): createPlease input the userfile : userfilePlease input the passwdfile :passfile //建立用戶Changing password for user user1.passwd: all authentication tokens updated successfully.Changing password for user user2.passwd: all authentication tokens updated successfully.Changing password for user user3.passwd: all authentication tokens updated successfully.[root@localhost mnt]# sh user_ctrl.sh user Please input the operation (create or delete ): delete //刪除用戶Please input the userfile :userfile[root@localhost mnt]# id user1id: user1: no such user


實驗二
編寫腳本auto_connect.exp滿足要求:
執行:/mnt/auto_connect.exp IP password 時 密碼正確,則通過 ssh 連接到該 IP 主機,并保持登陸。
1.編寫腳本:
[root@foundation71 mnt]# vim auto_connect.sh #!/bin/bash/usr/bin/expect << EOF //切換到expect環境spawn ssh root@$1 //ssh連接IPexpect { "yes/no" { send "yes\r";exp_continue } //確認連接 "password" { send "$2\r" } //輸入密碼}Interact //保留EOF //退出

2.執行:
[root@foundation71 mnt]# chmod +x auto_connect.sh //添加可執行權限[root@foundation71 mnt]# sh auto_connect.sh 172.25.254.226 redhat //添加IP和密碼spawn ssh root@172.25.254.226The authenticity of host '172.25.254.226 (172.25.254.226)' can't be established.ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.25.254.226' (ECDSA) to the list of known hosts.root@172.25.254.226's password: [root@foundation71 mnt]# //自動化連接

實驗三
編寫腳本,當執行腳本host_ping.sh時,ping各個主機IP ,ping通,顯示該IP的 hostname以及IP ,不能 ping 通,報錯并顯示 IP。
1.編寫腳本:
#!/bin/bashAuto_Connect(){/usr/bin/expect << EOFset timeout 5spawn ssh root@172.25.254.$IP_NUM hostnameexpect { "yes/no" { send "yes\r";exp_continue } "password:" { send "westos\r" }}expect eofEOF}for IP_NUM in {71..72}do ping -c1 -w1 172.25.254.$IP_NUM &> /dev/null && { Host_Name=`Auto_Connect | grep -E "authenticity|fingerprint|connecting|password|spawn|Warning" -v` //過濾掉警告等語句 } echo " $Host_Name 172.25.254.$IP_NUM"done

2.執行:
##注意此時會出現一個覆蓋問題
[root@foundation71 mnt]# sh host_ping.sh //執行未顯示主機名 172.25.254.226 172.25.254.227 [root@foundation71 mnt]# sh -x host_ping.sh + for IP_NUM in '{226..227}'+ ping -c1 -w1 172.25.254.226++ Auto_Connect++ grep -E 'authenticity|fingerprint|connecting|password|spawn|Warning' -v++ /usr/bin/expect+ Host_Name=$'localhost\r' //此時這里有主機名,但是后面自動加了/r,這是因為expect是unix工具,在unix系統中/r是換行,但在linux中成了回車,會覆蓋之前內容。 172.25.254.226 ' 172.25.254.226 + for IP_NUM in '{226..227}'+ ping -c1 -w1 172.25.254.227 172.25.254.227 ' 172.25.254.227

3.我們將腳本做一個調整
#!/bin/bashAuto_Connect(){/usr/bin/expect << EOFset timeout 5spawn ssh root@172.25.254.$IP_NUM hostnameexpect { yes/no { send "yes\r";exp_continue } password { send "redhat\r" }}expect eofEOF}for IP_NUM in {226..227}do ping -c1 -w1 172.25.254.$IP_NUM &> /dev/null && { Host_Name=`Auto_Connect | grep -E "authenticity|fingerprint|connecting|password|spawn|Warning" -v` } echo "$Host_Name 172.25.254.$IP_NUM " | sed 's/\r//g' //將全文的/r換為空。done

4.此時執行
[root@foundation71 mnt]# sh host_ping.sh //成功localhost 172.25.254.226 localhost 172.25.254.227

實驗四
利用case語句備份數據庫,要求
1.執行db_dump.sh westos (數據庫密碼)
2.腳本執行后會備份數據庫中的所有數據到/mnt/mysqldump目錄下
3.備份文件名稱為“庫名稱.sql”,當此文件存在是跳過,并詢問動作
4.輸入”s“跳過備份,輸入”b“備份“庫名稱.sql”文件為”庫名稱_backup.sql”,輸入”o“覆蓋原文件
1.編寫腳本:
#!/bin/bashDATABASE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v`mkdir -p /mnt/mysqldump //建立目錄for DATABASE_NAME in $DATABASEdo [ -e "/mnt/mysqldump/${DATABASE_NAME}.sql" ] || { mysqldump -uroot $DATABASE_NAME > /mnt/mysqldump/${DATABASE_NAME}.sql echo -e "${DATABASE_NAME}.sql is backup!!" //文件不存在,備份 } && { //文件存在時詢問動作 read -p "[S]kip [B]ackup [O]verwrite Please input action: " ACTION ACTION=`echo $ACTION | tr 'A-Z' 'a-z'` case $ACTION in s) //直接跳過 ;; b) //更新名字備份 mysqldump -uroot $DATABASE_NAME > /mnt/mysqldump/${DATABASE_NAME}_backup.sql echo "${DATABASE_NAME}_backup.sql is backup!!" ;; o) //覆蓋備份 mysqldump -uroot $DATABASE_NAME > /mnt/mysqldump/${DATABASE_NAME}.sql echo "${DATABASE_NAME}.sql is overwrite!!" ;; exit) //退出 echo "bye" exit 0 ;; *) //其他顯示錯誤 echo error esac }done

2.執行
[root@localhost mnt]# sh db_dump.sh linux.sql is backup!![S]kip [B]ackup [O]verwrite Please input action: smysql.sql is backup!![S]kip [B]ackup [O]verwrite Please input action: exitbye[root@localhost mnt]# sh db_dump.sh [S]kip [B]ackup [O]verwrite Please input action: blinux_backup.sql is backup!![S]kip [B]ackup [O]verwrite Please input action: exitbye[root@localhost mnt]# sh db_dump.sh [S]kip [B]ackup [O]verwrite Please input action: olinux.sql is overwrite!![S]kip [B]ackup [O]verwrite Please input action: exitbye

實驗五
腳本要求:
1執行腳本 lamp.sh
2.腳本執行后部署好論壇,并設定 apache 的網絡接口為 8080
1.編寫腳本:
真機中執行腳本#!/bin/bashAuto_Discuz(){/usr/bin/expect << EOFset timeout 30spawn ssh root@$1expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "westos\r" }}expect "]#" { send "yum install httpd -y\r" }expect "]#" { send "yum install mariadb-server -y\r"}expect "]#" { send "yum install php-mysql.x86_64 -y\r"}expect "]#" { send "systemctl start httpd\r" }expect "]#" { send "systemctl start mariadb\r" }expect eofEOF}Auto_Connect(){/usr/bin/expect << EOFset timeout 30spawn ssh root@$1expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "westos\r" }}expect "]#" { send "cd /var/www/html/\r" }expect "]#" { send "unzip /var/www/html/Discuz_X3.2_SC_UTF8.zip >> /dev/null \r" }expect "]#" { send "chmod 777 /var/www/html/upload/ -R\r" }expect "]#" { send "systemctl restart httpd\r" }expect eofEOF}Auto_Httpd(){/usr/bin/expect << EOFset timeout 30spawn ssh root@$1expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "westos\r" }}expect "]#" { send "sed "/^Listen/cListen 8080" -i /etc/httpd/conf/httpd.conf\r" }expect "]#" { send "yum restart httpd -y\r" }expect eofEOF}yum install expect -yAuto_Discuz $1scp /home/kiosk/Downloads/Discuz_X3.2_SC_UTF8.zip root@$1:/var/www/htmlAuto_Connect $1firefox -new-tab $1/upload/installAuto_Httpd $1
VSole
網絡安全專家