不怕忘記!讓電腦每天自動登入 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 -e
* */3 * * * /home/user/AutoPtt.sh
星號由左至右各自代表「分、時、日、月、周」,如果想要一天執行一次可以這樣寫:
* * */1 * * # 每一天執行一次
* 0 * * * * # 每天 0 點執行
如果因為某些原因,導致超過時間沒有執行到這項工作 (如斷電或關機),可以把執行檔或連結檔丟入 /etc/cron.daily 內。根據 /etc/anacrontab 內的設定,若資料夾內的執行檔超過時間沒被執行,就會自動啟動該項任務。
/etc/cron.daily
/etc/cron.weekly
詳細用法請參考鳥哥的 Linux 教學。但帳號密碼直接以明碼寫在檔案內其實不太安全,因此除了檔案權限以外,也要注意電腦的安全管理。
參考
腳本作者為 PTT 使用者: kenduest (小州)
鳥哥的 Linux 教學: 第十五章、例行性工作排程(crontab)
抱歉站長 請問一下該怎下指令 才能每天執行一次?
回覆刪除還有 如果我的電腦不小心今天沒開機 那跳過的這天能補嗎?
已增加說明。
刪除./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
請問出現這些訊息並且沒有登入成功是什麼原因呢?
因為您的 Linux 內沒有裝 expect 這支程式吧?
刪除這個會不會盜密碼?
回覆刪除此腳本是透過 expect 這支程式與 Linux shell 來進行自動登入的,程式碼也已經開放給大家檢視了。至於要不要使用,則是個人選擇了,本站不做任何擔保。
刪除