LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1242|回复: 5

Shell/Web 下發送 MSN Alert/Message(ZT)

[复制链接]
发表于 2005-7-25 10:16:31 | 显示全部楼层 |阅读模式
MSN 通常是用?斫徽劦, Liunx 下最有名的 MSN Client 當屬 gaim,
不過因為它是 GUI 的環境,要以 shell ?響脤?在不容易
(如果你能用 shell 控制 mouse 點在那裏就可以做到,但你能嗎 ? ()

1. 安裝 tmsnc (http://tmsnc.sourceforge.net/download.html)
我的例子是用 0.1.1 版本,你可以找別的版本自己開發,或參考我的例子也可以
代码:

  1. $>wget [url]http://unc.dl.sourceforge.net/sourceforge/tmsnc/tmsnc-0.1.1.tar.gz[/url]
  2. $>tar -zxvf tmsnc-0.1.1.tar.gz
  3. $>cd tmsnc-0.1.1
  4. # 看一下 Makefile , 裏面的 openssl 路?绞欠穹夏愕沫h境
  5. $>make
  6. $>make install
  7. $>tmsnc   (啟動的樣子)
  8.      .........
  9.    .'------.' |
  10.   | .-----. | |
  11.   | |TMSNC| | |;.
  12.   |*`-----'.|.' `;
  13.   `---------' .;'
  14. .''''////////;'
  15. / ######### /;/    The
  16. / ######### //   textbased
  17. `-----------'   alternative!
复制代码


Login: abel@twnic.net.tw
Your login-address is abel@twnic.net.tw
Enter password:

2.TMSNC 的指令說明
代码:

# tmsnc 的 command 說明 (Readme),如果你要完善你的功能,就要詳讀任何的說明
Availible commands in TMSNC are:
* help                      show a list of availible commands
* clear                     clear the screen
* nick <name>               set your nickname to <name>
* status <string>           where <string> is online, busy, brb,
*                            away, phone, lunch or hidden
* quit                      logout from server and quit TMSNC

* add <address>             add <address> to the contact-list
* block <address>           prevent <address> from obtaining your status
* unblock <address>         reverse the block
* remove <address>          remove <address> from contact-list
* window <n> or TAB         change current window to <n>
* window close or F7        close current window and conversation
* chat <address> or Enter   start a new conversation with <address>

由以上我們可以知道, tmsnc 我們巳經裝好了,所以就可以?碜 shell 發送 msn 的工作了!
用 shell ?戆l,最好的工具是 expect , 這個工具的說明,你可以在 man page 中找到詳細
解說

3.
3.1
範例一(簡單版):
代码:


  1. #!/usr/bin/expect
  2. # filename=send.exp
  3. log_file ./exp.log
  4. spawn tmsnc -l 你申請的MSN帳號
  5. expect "Enter password:"
  6. send "你的密碼\r"
  7. expect -re ".* can't control me*"
  8. sleep 5

  9. send "/chat [lindex $argv 0]\n"
  10. expect -re ".*!.*"
  11. sleep 1
  12. send "Now: [exec date]\n"
  13. sleep 1
  14. send "[lindex $argv 1]\n"
  15. sleep 3
  16. send "/window close\r"
  17. send "/quit\r"
复制代码


用法:
代码:

#請注意, MSN?息需為 UTF-8 encoding
$>./send.exp MSN連絡人 MSN?息


3.1
範例二(稍複雜版):

代码:


  1. #!/usr/bin/expect
  2. # log file , for debug
  3. log_file ./exp.log
  4. #設定?息路經
  5. set workdir "/www/htdocs/html/msn/msg"
  6. # -l 指定 MSN contact, 你可以 tmsnc -h ?砹私鈪
  7. spawn tmsnc -l 你申請的MSN帳號
  8. sleep 1
  9. # 出現 password 時,送出密碼
  10. expect "Enter password:"
  11. send "你的密碼\r"
  12. sleep 1
  13. # 出現 control me 時,表示 tmsnc 載入完成
  14. expect -re ".* can't control me*"
  15. sleep 3
  16. # 無限迴圈,讀取?息檔
  17. while {1} {
  18.        sleep 1
  19.    #取得 ?息檔中第一行,我個人的定義是 MSN addr
  20.        set msn_addr [ exec cat $workdir/msg.utf8 |  head -1 ]
  21.    #如果?]有第一行,則回到 loop
  22.        if {$msn_addr!=""} {
  23.       #對誰發送說明,指令請看 Readme
  24.                send "/chat $msn_addr\n"
  25.                sleep 1
  26.       #出現對話視窗後
  27.                expect -re ".*!.*"
  28.       #發送現在時間
  29.                send "Now: [exec date ]\n"
  30.                sleep 1
  31.       #發送該檔案的第二行起的?息,請注意,?息需為 UTF-8 的 encoding ,您可使用 iconv ?磙D換
  32.                send "[exec cat $workdir/msg.utf8 | sed -e 1d ] \n"
  33.                sleep 1
  34.       #關?]對話
  35.                send "/window close\n"
  36.                sleep 1
  37.       #清除?息檔
  38.                set cmd [exec echo -n "" > $workdir/msg.utf8]
  39.        }
  40. }
  41. send "/quit\r"
复制代码


現在,我們可以?韺懸粋網頁程式,?碜錾侠械 msg.utf8 的處理
代码:

[PHP]
<?php
while ( list($key,$val)=each($_GET))
       $$key=$val;
if (isset($smt1)) {
       $msn=iconv("GB2312","UTF-8",$msg);
       $fp=fopen("/www/htdocs/html/msn/msg/msg.utf8",'w');
       fwrite($fp,"$txt1\n$msn");
       fflush($fp);
       fclose($fp);
       system("cat ./msg/msg.utf8");
       echo "<BR>\n";
}
?>
<html><body>您要加我才能送?息給你, 請新增 MSN 連絡人: abel@twnic.net.tw<BR>
測試完後再把我刪了即可, 非常感謝!<BR>
<form method=GET>
我要傳送給 msn:<input type=text value="<?php echo $txt1;?>" name=txt1>
<input type=text name=msg value="<?php echo $msg;?>" maxlength=255>?息
<input type=submit value=send name=smt1>
</form> </body></html>
[/PHP]

嗯... Good, 如此,你就可以用
代码:


  1. $>wget [url]http://你的url/msn.php?smt1=send&txt1=MSN連絡人&msg=MSN?息[/url]
复制代码


?戆l放?息了,?際的例子,您可參考
http://211.72.210.251/msn/msn.php

5.
?用範例(mrtg)
代码:


  1. WorkDir: /www/htdocs/html/mrtg
  2. Refresh: 300
  3. Interval: 5
  4. Language: Big5
  5. Options[_]: growright, noinfo, gauge
  6. #LogFormat: rrdtool
  7. #PathAdd: /usr/bin/
  8. #LibAdd: /usr/lib/perl/

  9. #-----------------------Target Begin------------------------------------
  10. Target[localhost-mysql]:`check_mysqlsh localhost `
  11. MaxBytes1[localhost-mysql]:200
  12. MaxBytes2[localhost-mysql]: 100
  13. Title[localhost-mysql]: 211.72.210.251 MYSQL Threads/Qps Stat
  14. Legend1[localhost-mysql]: 行程數
  15. Legend2[localhost-mysql]: 秒查詢量
  16. LegendI[localhost-mysql]: Threads
  17. LegendO[localhost-mysql]: Qps
  18. YLegend[localhost-mysql]: Threads/Qps
  19. PageTop[localhost-mysql]: <h1>211.72.210.251 MYSQL Threads/Qps Stat</h1>

  20. ThreshMaxI[procmail-mysql]: 30
  21. ThreshMinI[procmail-mysql]: 1
  22. ThreshMaxO[procmail-mysql]: 50
  23. ThreshProgI[procmail-mysql]:/www/htdocs/html/mrtg/alert/mysql_alert.sh
  24. ThreshProgO[procmail-mysql]:/www/htdocs/html/mrtg/alert/mysql_alert.sh
复制代码


以上的例子是我在監控 mysql 的行程式及查詢量,若行程數超過 30 個,或<=1 個時,或是mysql 每秒
查詢超過 50 個 query, 啟動 mysql_alrtt.sh 程式,而 mysql_alert.sh 程式如下


代码:


  1. #!/bin/sh
  2. # mrtg 會傳送三個參數給 ProgI/ProgO , 以利 alert 程式處理
  3. # 分於為 Target name/所設定的警告值/現在的值
  4. target=$1
  5. alert_val=$2
  6. curr_val=$3
  7. wget "http://211.72.210.251/msn/msn.php?smt1=&txt1=MSN連絡人&msg=MRTG 所監測的\
  8. $target,發生問?},您所設的現在值 $curr_val 超過您所設的 $alert_val\
  9. \n請您參考 http://你的網頁/$target.html" -O /tmp/msn.log
复制代码


4. 結語
我個人目前系統的 Alert 都是透過 MSN ?碜黾磿r發放的,你可以寫一個檢查系統的程式, check 到有
alert 狀況時,就可以用 wget .... ?硭统鲇?息給很多相關人員,只要您想送什麼,就可以送什麼,透過
shell 放送,更增您的方便性(當然,也可以送病毒)


註:如果您充份了解 expect 的字串處理,可以反過?,讓 tmsnc + expect 幫您做事哦, 例如和他交談
"exec ls -la /tmp" , 這個 exec 是 prefix, 你只要能拆解出 "ls -la /tmp", 就可以在 msn 中看
到 /tmp 下的檔案列表了 ! 這絕對是真的, 就看你的能力囉
(以上所出現的 email 或 msn address 皆是假的,不要寄信?,不然你會被列入 RBL)


原文请看这里:http://bbs.chinaunix.net/forum/viewtopic.php?t=575520
发表于 2005-7-25 12:18:46 | 显示全部楼层
不错,非常好的shell, expect应用。
回复 支持 反对

使用道具 举报

发表于 2005-7-25 12:30:47 | 显示全部楼层
不错,非常好的shell, expect应用。
回复 支持 反对

使用道具 举报

发表于 2005-7-25 12:57:06 | 显示全部楼层
有电乱,有code包起来会好一点
回复 支持 反对

使用道具 举报

发表于 2005-7-26 01:08:16 | 显示全部楼层
Post by genv
有电乱,有code包起来会好一点

收到, 已帮楼主做了些装修工作。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-7-27 09:13:35 | 显示全部楼层
谢谢,我不太熟悉如何排版。加上是转发
谢谢yongjian
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表