<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    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
    
    root權限shell腳本實例
    本作品采用《CC 協議》,轉載必須注明作者和本文鏈接
    實驗一利用case語句編寫腳本,滿足下列要求1.執行create時根據userfile和passfile建立用戶
    建議組織密切關注可疑的容器活動、執行動態鏡像分析,并定期掃描環境。
    攻擊團伙情報 APT37組織使用Konni RAT攻擊歐盟目標 近期APT32(海蓮花)組織攻擊活動樣本分析 透明部落以“清潔運動”為主題對印度國防部下屬企業發起釣魚攻擊 疑似EvilNum針對歐洲金融實體
    linux命令一大堆,都記不住呀!別忙,這里總結常用20個,大家要多多練習!運行它就是列出文件夾里的內容,可能是文件也可能是文件夾。注意:用戶可以使用官方提供的和md5sum生成簽名信息匹對以此檢測文件是否改變。Md5sum沒有sha1sum安全,這點我們稍后討論。可以用來轉換和復制文件,大多數時間是用來復制iso文件到一個usb設備中去,所以可以用來制作USB啟動器。
    本篇文章是WebLogic中間件漏洞復現,記錄了近幾年來爆出的WebLogic中間件漏洞主要分為六個部分:WebLogic簡介、WebLogic安裝、WebLogic漏洞復現、WebLogic SSRF聯動Redis、WebLogic實戰和WebLogic防御措施。
    黑客攻擊變得一年比一年高級和復雜,因此現在追蹤了解安全漏洞比以往任何時候都來得重要。
    在最近的一個案例中,威脅分子利用這個未打補丁的漏洞來部署 SmokeLoader 惡意軟件,以便投放其他惡意軟件,比如 TrickBot。這兩個漏洞于 2022 年 9 月首次被披露,據稱被黑客利用了數月。微軟證實,黑客們利用 ProxyNotShell 漏洞,在被攻擊的 Exchange 服務器上部署了 China Chopper web shell 惡意腳本
    在平時進行紅藍攻防演練的時候,經常會碰到目標資產在云服務機器上的情況,新的技術也會帶來新的風險,本文將以 AWS 的 EC2(Elastic Compute Cloud)彈性計算服務為例,主要談談在面對云服務器場景下的一些攻防手法。
    VSole
    網絡安全專家
      亚洲 欧美 自拍 唯美 另类