ปรับปรุงสคริปต์ให้สามารถบล๊อกโดยอัตโนมัติ
$ vi drupal_auto_blockuser.sh
#!/bin/bash # AUTO BLOCK DRUPAL USER SCRIPT, CHECK SPAM FROM google.co.th # FROM: https://drupal.org/node/118759 SITE=http://www.example.com # NO TRAILING SLASH NAME=drupal_admin_user PASS=drupal_admin_password UIDFILE="site_last_uid.txt" SPAMDATA="spamdata.txt" SLEEP=10 #REDUCE SERVER LOAD STATUS=0 #0:block,1:unblock OPLOGIN="Log%20in" OPSUBMIT="Save" TMP=/tmp/$RANDOM.txt COOKIES=/tmp/$RANDOM.txt GGCOOKIES=/tmp/$RANDOM.txt AGENT="Mozilla/5.0 (X11; Linux x86_64; rv:25.0) Gecko/20130712 Firefox/25.0" QURL="https://www.google.co.th/search?q=" # LOGIN wget -q -o /dev/null -O /dev/null \ --keep-session-cookies --save-cookies $COOKIES --load-cookies $COOKIES \ --post-data="name=${NAME}&pass=${PASS}&op=${OPLOGIN}&form_id=user_login" \ "${SITE}/?q=user/login" # PREVIOUS USER ID PREVUID=`cat $UIDFILE` let "STARTUID=${PREVUID}+1" # GET LAST USER ID wget -q -o /dev/null -O $TMP \ --keep-session-cookies --save-cookies $COOKIES --load-cookies $COOKIES \ "${SITE}/?q=admin/user/user" LASTUID=`grep -m1 'class="form-item" id="edit-accounts-' $TMP | awk -F'class="form-item" id="edit-accounts-' '{ print $2 }' | cut -d\- -f1` #DO CHECK for ID in `eval echo {$STARTUID..$LASTUID}`; do wget -q -o /dev/null -O $TMP \ --keep-session-cookies --save-cookies $COOKIES --load-cookies $COOKIES \ "${SITE}/?q=user/${ID}/edit" LINE0=`grep -n 'value="user_profile_form"' $TMP | cut -d: -f1` # PREVENT BLOCKED USER ERROR if ! [ "$LINE0" ]; then continue fi let "LINE=${LINE0}-1" TOKEN=`sed -n -e "${LINE}p" $TMP | awk -F'value="' '{ print $2 }' | cut -d\" -f1` USER1=`grep 'id="edit-name"' $TMP | awk -F'value="' '{ print $2 }' | cut -d\" -f1` EMAIL=`grep 'id="edit-mail"' $TMP | awk -F'value="' '{ print $2 }' | cut -d\" -f1` # QUERY FOR SPAM wget -q -o /dev/null -O $TMP \ --keep-session-cookies --save-cookies $GGCOOKIES --load-cookies $GGCOOKIES \ -U "$AGENT" "${QURL}${EMAIL}" ISSPAM=0 KEYWORD="" #*********************** ### BEWARE THIS WHILE LOOP, VARIABLE $ISSPAM IS LOCAL, USE REDIRECT INSTEAD OF PIPE ### #cat $SPAMDATA | while read TXT; do # if [ "$TXT" ]; then # if grep -m1 "$TXT" $TMP > /dev/null; then # ISSPAM=1 # break # fi # fi #done #*********************** while read TXT; do if [ "$TXT" ]; then if grep -m1 "$TXT" $TMP > /dev/null; then ISSPAM=1 KEYWORD=$TXT break fi fi done < <(cat $SPAMDATA ) if [ "$ISSPAM" != "0" ]; then #DO BLOCK wget -q -o /dev/null -O /dev/null \ --keep-session-cookies --save-cookies $COOKIES --load-cookies $COOKIES \ --post-data="status=${STATUS}&op=${OPSUBMIT}&name=${USER1}&mail=${EMAIL}&form_token=${TOKEN}&form_id=user_profile_form" \ "${SITE}/?q=user/${ID}/edit" echo "UID:\"$ID\", USER:\"$USER1\", EMAIL:\"$EMAIL\", KEYWORD:\"$KEYWORD\" --- Blocked." sleep $SLEEP else echo "--- UID:\"$ID\", USER:\"$USER1\", EMAIL:\"$EMAIL\", not found as spam." fi sleep $SLEEP done # SAVE LAST UID echo $LASTUID > $UIDFILE rm $TMP rm $COOKIES rm $GGCOOKIES
$ chmod 700 drupal_auto_blockuser.sh
ใส่ค่า spam keyword
$ vi spamdata.txt
did not match any documents Spam ไม่ตรงกับเอกสารใด
ใส่ค่า uid ของผู้ใช้คนสุดท้ายที่ไม่ต้องการตรวจ (สมมุติว่าเป็นผู้ใช้คนที่ 1000)
$ vi site_last_uid.txt
1000
$ ./drupal_auto_blockuser.sh
ครั้งต่อไปก็แค่สั่งรัน โดยไม่ต้องปรับแต่งอะไรอีก เว้นแต่มี spam keyword เพิ่มเติมก็ไปแก้ไฟล์ spamdata.txt