[root@localhost mnt]#!/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! ;;esac2.執行:
[root@localhost mnt]user1user2user3[root@localhost mnt]123456789[root@localhost mnt]Please input the operation (create or delete ): hello //輸入錯誤動作Eorror![root@localhost mnt]Please input the operation (create or delete ): createPlease input the userfile : user //輸入錯誤文件user is not exist [root@localhost mnt]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]Please input the operation (create or delete ): delete //刪除用戶Please input the userfile :userfile[root@localhost mnt]id: user1: no such user實驗二編寫腳本auto_connect.exp滿足要求:執行:/mnt/auto_connect.exp IP password 時 密碼正確,則通過 ssh 連接到該 IP 主機,並保持登陸。1.編寫腳本:[root@foundation71 mnt]#!/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][root@foundation71 mnt]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"done2.執行:##注意此時會出現一個覆蓋問題[root@foundation71 mnt] 172.25.254.226 172.25.254.227 [root@foundation71 mnt]+ 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.2273.我們將腳本做一個調整
#!/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換為空。done4.此時執行
[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 }done2.執行
[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.sh2.腳本執行後部署好論壇,並設定 apache 的網絡接口為 80801.編寫腳本:真機中執行腳本#!/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推薦:一個從不寫廢話的公眾號
覺得不錯點個「贊」、「在看」哦