2020年4月8日 星期三

PTT 自動登入 (auto PTT login through ssh)

[Linux]:
[ref]
1. https://gist.github.com/M157q/ad375e227ec0f1ba450915df65433473
2. http://tcl-eval.blogspot.com/2018/11/tclexpect-auto-login-to-ptt-bbs-using.html
3. https://www.cnblogs.com/lzrabbit/p/4298794.html

@CentOS8
>> dnf install tcl expect
>> vi ssh2PTT.sh

script:
>>
#!/usr/bin/env expect
# Login into PTT via SSH.
# Combine crontab with this script so you don't forget to login PTT everyday.
# Usage: /bin/bash -c ${FILE_PATH_OF_THIS_SCRIPT}
# Example in crontab:
# ptt auto login, every day at 12:00 PM
# 0 12 * * * /bin/bash -c ${ABSOLUTE_FILE_PATH_OF_THIS_SCRIPT}

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


[Windows 10 X64]:
[ref]
1. https://www.ptt.cc/bbs/Linux/M.1285695860.A.B95.html
2. https://bobjin.com/blog/view/3568ea7f829d9609b31806beab906e97.html
3. https://dreamtails.pixnet.net/blog/post/28197814
4. https://www.jianshu.com/p/94827688e093
5. https://blog.51cto.com/superleedo/2119076
6. https://www.cnblogs.com/zy6103/p/10222996.html
7. https://pypi.org/project/wexpect/
PS. winpexpect stop maintaining

Method 1. Python + pexpect + openssh/plink ... bad
1. download & install anaconda
https://www.anaconda.com/distribution/#download-section
note: check 'path' option would be easier
2. C:\> pip install wexpect
Comment: bad solution
1. winpexpect is obsolete and stop maintaining
2. pexpect doesn't work on Windows
3. wexpect works only on python 2.7 and will be obsolete soon

Method 2: cygwin + expect + openssh
wait for trying

沒有留言:

張貼留言