不怕忘記!讓電腦每天自動登入 PTT 累積上站次數

ptt 自動登入

本文適用於想累積登入次數,卻常忘記每天登入 PTT 的使用者。

 


 

確認電腦內有安裝 expect

$ which expect 
/user/bin/expect 

 

建立 .sh 腳本

$ vi AutoPtt.sh

#!/usr/bin/expect
spawn ssh -oBatchMode=no -oStrictHostKeyChecking=no bbsu@ptt.cc
set BBS_ID "YOUR_ID"
set BBS_PW "YOUR_PW"
expect {
        "請輸入代號" { send "$BBS_ID\r" ; exp_continue }
        "請輸入您的密碼" { send "$BBS_PW\r" ; exp_continue }
        "您想刪除其他重複登入的連線嗎" { send "N\r" ; exp_continue }
        "您要刪除以上錯誤嘗試的記錄嗎" { send "N\r" ; exp_continue }
        "任意鍵繼續" { send "q\r" ; exp_continue }
        "密碼不對喔" { exit } #xi virus
        "裡沒有這個人啦" { exit }
        "請勿頻繁登入以免造成系統過度負荷" { send "\r" ; exp_continue }
        "請按任意鍵繼續" { send "\r" ; exp_continue }
        "oodbye" { interact }
        }
exit

 

更改檔案權限成為執行檔

$ chmod a+x AutoPtt.sh

 

 

利用 crontab 指令讓 AutoPtt.sh 定時自動執行

在 crontab 文件開頭加入 expect 宣告,或者用 expect -f 來啟動程式

$ crontab -e
#!/usr/bin/expect
0 0 */1 * * /home/user/AutoPtt.sh
0 0 */1 * * expect -f /home/user/AutoPtt.sh

 

 

星號由左至右分別代表「分、時、日、月、周」,如果想每天執行的話可以這樣寫:

0 0 */1 * * # 每一天執行一次
0 0 * * *   # 每天 0 點執行

 


Crontab 時間排程與開機自動啟動程式


用 anacron 讓系統補充執行超過時間的程式

如果擔心因斷電或假日關機導致程式沒執行到的話,可以把執行檔或連結檔丟入 /etc/cron.daily 內,讓系統根據 /etc/anacrontab 的設定來補充執行漏掉的程式。


如此一來,等到下次電腦開機後,若系統發現 daily 資料夾內的執行檔超過時間沒被執行的話,就會在五分鐘後自動補充執行。

 

sudo nano /etc/anacrontab                                                               

# These replace cron's entries
1       5       cron.daily      run-parts --report /etc/cron.daily
7       10      cron.weekly     run-parts --report /etc/cron.weekly
@monthly        15      cron.monthly    run-parts --report /etc/cron.monthly


/etc/cron.daily 
/etc/cron.weekly 

 

另外,丟入 daily 資料夾的執行檔或腳本,必須是可執行檔 (a+x) 並去除 .sh 副檔名才能正確執行。完成後,可以用下面這個指令來檢測是否能正常運作:


run-parts --test /etc/cron.daily



詳細用法請參考鳥哥的 Linux 教學。但帳號密碼直接以明碼寫在檔案內其實不太安全,因此除了檔案權限以外,也要注意電腦的安全管理。

 

 

參考

腳本作者為 PTT 使用者: kenduest (小州) 

鳥哥的 Linux 教學: 第十五章、例行性工作排程(crontab)

留言

TadiYvo表示…
抱歉站長 請問一下該怎下指令 才能每天執行一次?
還有 如果我的電腦不小心今天沒開機 那跳過的這天能補嗎?
匿名表示…
./Autoptt.sh: 2: ./Autoptt.sh: spawn: not found
./Autoptt.sh: 5: ./Autoptt.sh: expect: not found
./Autoptt.sh: 6: ./Autoptt.sh: 請輸入代號: not found
./Autoptt.sh: 6: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 7: ./Autoptt.sh: 請輸入您的密碼: not found
./Autoptt.sh: 7: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 8: ./Autoptt.sh: 您想刪除其他重複登入的連線嗎: not found
./Autoptt.sh: 8: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 9: ./Autoptt.sh: 您要刪除以上錯誤嘗試的記錄嗎: not found
./Autoptt.sh: 9: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 10: ./Autoptt.sh: 任意鍵繼續: not found
./Autoptt.sh: 10: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 11: ./Autoptt.sh: 密碼不對喔: not found
./Autoptt.sh: 12: ./Autoptt.sh: 裡沒有這個人啦: not found
./Autoptt.sh: 13: ./Autoptt.sh: 請勿頻繁登入以免造成系統過度負荷: not found
./Autoptt.sh: 13: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 14: ./Autoptt.sh: 請按任意鍵繼續: not found
./Autoptt.sh: 14: ./Autoptt.sh: exp_continue: not found
./Autoptt.sh: 15: ./Autoptt.sh: goodbye: not found

請問出現這些訊息並且沒有登入成功是什麼原因呢?
匿名表示…
這個會不會盜密碼?
此腳本是透過 expect 這支程式與 Linux shell 來進行自動登入的,程式碼也已經開放給大家檢視了。至於要不要使用,則是個人選擇了,本站不做任何擔保。
因為您的 Linux 內沒有裝 expect 這支程式吧?