• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            不會(huì)飛的鳥

            2010年12月10日 ... 不鳥他們!!! 我要用自己開(kāi)發(fā)的分布式文件系統(tǒng)、分布式調(diào)度系統(tǒng)、分布式檢索系統(tǒng), 做自己的搜索引擎!!!大魚有大志!!! ---楊書童

            #

            [轉(zhuǎn)]TF-IDF與余弦相似性的應(yīng)用(二):找出相似文章

            今天,我們?cè)賮?lái)研究另一個(gè)相關(guān)的問(wèn)題。有些時(shí)候,除了找到關(guān)鍵詞,我們還希望找到與原文章相似的其他文章。比如,"Google新聞"在主新聞下方,還提供多條相似的新聞。

            為了找出相似的文章,需要用到"余弦相似性"(cosine similiarity)。下面,我舉一個(gè)例子來(lái)說(shuō)明,什么是"余弦相似性"。

            為了簡(jiǎn)單起見(jiàn),我們先從句子著手。

              句子A:我喜歡看電視,不喜歡看電影。

              句子B:我不喜歡看電視,也不喜歡看電影。

            請(qǐng)問(wèn)怎樣才能計(jì)算上面兩句話的相似程度?

            基本思路是:如果這兩句話的用詞越相似,它們的內(nèi)容就應(yīng)該越相似。因此,可以從詞頻入手,計(jì)算它們的相似程度。

            第一步,分詞。

              句子A:我/喜歡/看/電視,不/喜歡/看/電影。

              句子B:我/不/喜歡/看/電視,也/不/喜歡/看/電影。

            第二步,列出所有的詞。

              我,喜歡,看,電視,電影,不,也。

            第三步,計(jì)算詞頻。

              句子A:我 1,喜歡 2,看 2,電視 1,電影 1,不 1,也 0。

              句子B:我 1,喜歡 2,看 2,電視 1,電影 1,不 2,也 1。

            第四步,寫出詞頻向量。

              句子A:[1, 2, 2, 1, 1, 1, 0]

              句子B:[1, 2, 2, 1, 1, 2, 1]

            到這里,問(wèn)題就變成了如何計(jì)算這兩個(gè)向量的相似程度。

            我們可以把它們想象成空間中的兩條線段,都是從原點(diǎn)([0, 0, ...])出發(fā),指向不同的方向。兩條線段之間形成一個(gè)夾角,如果夾角為0度,意味著方向相同、線段重合;如果夾角為90度,意味著形成直角,方向完全不相似;如果夾角為180度,意味著方向正好相反。因此,我們可以通過(guò)夾角的大小,來(lái)判斷向量的相似程度。夾角越小,就代表越相似。

            以二維空間為例,上圖的a和b是兩個(gè)向量,我們要計(jì)算它們的夾角θ。余弦定理告訴我們,可以用下面的公式求得:

            假定a向量是[x1, y1],b向量是[x2, y2],那么可以將余弦定理改寫成下面的形式:

            數(shù)學(xué)家已經(jīng)證明,余弦的這種計(jì)算方法對(duì)n維向量也成立。假定A和B是兩個(gè)n維向量,A是 [A1, A2, ..., An] ,B是 [B1, B2, ..., Bn] ,則A與B的夾角θ的余弦等于:

            使用這個(gè)公式,我們就可以得到,句子A與句子B的夾角的余弦。

            余弦值越接近1,就表明夾角越接近0度,也就是兩個(gè)向量越相似,這就叫"余弦相似性"。所以,上面的句子A和句子B是很相似的,事實(shí)上它們的夾角大約為20.3度。

            由此,我們就得到了"找出相似文章"的一種算法:

              (1)使用TF-IDF算法,找出兩篇文章的關(guān)鍵詞;

              (2)每篇文章各取出若干個(gè)關(guān)鍵詞(比如20個(gè)),合并成一個(gè)集合,計(jì)算每篇文章對(duì)于這個(gè)集合中的詞的詞頻(為了避免文章長(zhǎng)度的差異,可以使用相對(duì)詞頻);

              (3)生成兩篇文章各自的詞頻向量;

              (4)計(jì)算兩個(gè)向量的余弦相似度,值越大就表示越相似。

            "余弦相似度"是一種非常有用的算法,只要是計(jì)算兩個(gè)向量的相似程度,都可以采用它。

            posted @ 2014-03-06 21:36 不會(huì)飛的鳥 閱讀(250) | 評(píng)論 (0)編輯 收藏

            [轉(zhuǎn)]TF-IDF與余弦相似性的應(yīng)用(一):自動(dòng)提取關(guān)鍵詞

            這個(gè)標(biāo)題看上去好像很復(fù)雜,其實(shí)我要談的是一個(gè)很簡(jiǎn)單的問(wèn)題。

            有一篇很長(zhǎng)的文章,我要用計(jì)算機(jī)提取它的關(guān)鍵詞(Automatic Keyphrase extraction),完全不加以人工干預(yù),請(qǐng)問(wèn)怎樣才能正確做到?

            這個(gè)問(wèn)題涉及到數(shù)據(jù)挖掘、文本處理、信息檢索等很多計(jì)算機(jī)前沿領(lǐng)域,但是出乎意料的是,有一個(gè)非常簡(jiǎn)單的經(jīng)典算法,可以給出令人相當(dāng)滿意的結(jié)果。它簡(jiǎn)單到都不需要高等數(shù)學(xué),普通人只用10分鐘就可以理解,這就是我今天想要介紹的TF-IDF算法。

            讓我們從一個(gè)實(shí)例開(kāi)始講起。假定現(xiàn)在有一篇長(zhǎng)文《中國(guó)的蜜蜂養(yǎng)殖》,我們準(zhǔn)備用計(jì)算機(jī)提取它的關(guān)鍵詞。

            一個(gè)容易想到的思路,就是找到出現(xiàn)次數(shù)最多的詞。如果某個(gè)詞很重要,它應(yīng)該在這篇文章中多次出現(xiàn)。于是,我們進(jìn)行"詞頻"(Term Frequency,縮寫為TF)統(tǒng)計(jì)。

            結(jié)果你肯定猜到了,出現(xiàn)次數(shù)最多的詞是----"的"、"是"、"在"----這一類最常用的詞。它們叫做"停用詞"(stop words),表示對(duì)找到結(jié)果毫無(wú)幫助、必須過(guò)濾掉的詞。

            假設(shè)我們把它們都過(guò)濾掉了,只考慮剩下的有實(shí)際意義的詞。這樣又會(huì)遇到了另一個(gè)問(wèn)題,我們可能發(fā)現(xiàn)"中國(guó)"、"蜜蜂"、"養(yǎng)殖"這三個(gè)詞的出現(xiàn)次數(shù)一樣多。這是不是意味著,作為關(guān)鍵詞,它們的重要性是一樣的?

            顯然不是這樣。因?yàn)?中國(guó)"是很常見(jiàn)的詞,相對(duì)而言,"蜜蜂"和"養(yǎng)殖"不那么常見(jiàn)。如果這三個(gè)詞在一篇文章的出現(xiàn)次數(shù)一樣多,有理由認(rèn)為,"蜜蜂"和"養(yǎng)殖"的重要程度要大于"中國(guó)",也就是說(shuō),在關(guān)鍵詞排序上面,"蜜蜂"和"養(yǎng)殖"應(yīng)該排在"中國(guó)"的前面。

            所以,我們需要一個(gè)重要性調(diào)整系數(shù),衡量一個(gè)詞是不是常見(jiàn)詞。如果某個(gè)詞比較少見(jiàn),但是它在這篇文章中多次出現(xiàn),那么它很可能就反映了這篇文章的特性,正是我們所需要的關(guān)鍵詞。

            用統(tǒng)計(jì)學(xué)語(yǔ)言表達(dá),就是在詞頻的基礎(chǔ)上,要對(duì)每個(gè)詞分配一個(gè)"重要性"權(quán)重。最常見(jiàn)的詞("的"、"是"、"在")給予最小的權(quán)重,較常見(jiàn)的詞("中國(guó)")給予較小的權(quán)重,較少見(jiàn)的詞("蜜蜂"、"養(yǎng)殖")給予較大的權(quán)重。這個(gè)權(quán)重叫做"逆文檔頻率"(Inverse Document Frequency,縮寫為IDF),它的大小與一個(gè)詞的常見(jiàn)程度成反比。

            知道了"詞頻"(TF)和"逆文檔頻率"(IDF)以后,將這兩個(gè)值相乘,就得到了一個(gè)詞的TF-IDF值。某個(gè)詞對(duì)文章的重要性越高,它的TF-IDF值就越大。所以,排在最前面的幾個(gè)詞,就是這篇文章的關(guān)鍵詞

            下面就是這個(gè)算法的細(xì)節(jié)。

            第一步,計(jì)算詞頻。

            考慮到文章有長(zhǎng)短之分,為了便于不同文章的比較,進(jìn)行"詞頻"標(biāo)準(zhǔn)化。

            或者

            第二步,計(jì)算逆文檔頻率。

            這時(shí),需要一個(gè)語(yǔ)料庫(kù)(corpus),用來(lái)模擬語(yǔ)言的使用環(huán)境。

            如果一個(gè)詞越常見(jiàn),那么分母就越大,逆文檔頻率就越小越接近0。分母之所以要加1,是為了避免分母為0(即所有文檔都不包含該詞)。log表示對(duì)得到的值取對(duì)數(shù)。

            第三步,計(jì)算TF-IDF。

            可以看到,TF-IDF與一個(gè)詞在文檔中的出現(xiàn)次數(shù)成正比,與該詞在整個(gè)語(yǔ)言中的出現(xiàn)次數(shù)成反比。所以,自動(dòng)提取關(guān)鍵詞的算法就很清楚了,就是計(jì)算出文檔的每個(gè)詞的TF-IDF值,然后按降序排列,取排在最前面的幾個(gè)詞。

            還是以《中國(guó)的蜜蜂養(yǎng)殖》為例,假定該文長(zhǎng)度為1000個(gè)詞,"中國(guó)"、"蜜蜂"、"養(yǎng)殖"各出現(xiàn)20次,則這三個(gè)詞的"詞頻"(TF)都為0.02。然后,搜索Google發(fā)現(xiàn),包含"的"字的網(wǎng)頁(yè)共有250億張,假定這就是中文網(wǎng)頁(yè)總數(shù)。包含"中國(guó)"的網(wǎng)頁(yè)共有62.3億張,包含"蜜蜂"的網(wǎng)頁(yè)為0.484億張,包含"養(yǎng)殖"的網(wǎng)頁(yè)為0.973億張。則它們的逆文檔頻率(IDF)和TF-IDF如下:

            從上表可見(jiàn),"蜜蜂"的TF-IDF值最高,"養(yǎng)殖"其次,"中國(guó)"最低。(如果還計(jì)算"的"字的TF-IDF,那將是一個(gè)極其接近0的值。)所以,如果只選擇一個(gè)詞,"蜜蜂"就是這篇文章的關(guān)鍵詞。

            除了自動(dòng)提取關(guān)鍵詞,TF-IDF算法還可以用于許多別的地方。比如,信息檢索時(shí),對(duì)于每個(gè)文檔,都可以分別計(jì)算一組搜索詞("中國(guó)"、"蜜蜂"、"養(yǎng)殖")的TF-IDF,將它們相加,就可以得到整個(gè)文檔的TF-IDF。這個(gè)值最高的文檔就是與搜索詞最相關(guān)的文檔。

            TF-IDF算法的優(yōu)點(diǎn)是簡(jiǎn)單快速,結(jié)果比較符合實(shí)際情況。缺點(diǎn)是,單純以"詞頻"衡量一個(gè)詞的重要性,不夠全面,有時(shí)重要的詞可能出現(xiàn)次數(shù)并不多。而且,這種算法無(wú)法體現(xiàn)詞的位置信息,出現(xiàn)位置靠前的詞與出現(xiàn)位置靠后的詞,都被視為重要性相同,這是不正確的。(一種解決方法是,對(duì)全文的第一段和每一段的第一句話,給予較大的權(quán)重。)

            posted @ 2014-03-06 21:35 不會(huì)飛的鳥 閱讀(239) | 評(píng)論 (0)編輯 收藏

            關(guān)于pthread_detach( )

            man pthread_detach


            pthread_t  

            類型定義: typedef unsigned long int pthread_t;   //come from /usr/include/bits/pthread.h

            用途:pthread_t用于聲明線程ID。  sizeof (pthread_t) =4;

            linux線程執(zhí)行和windows不同,pthread有兩種狀態(tài)joinable狀態(tài)和unjoinable狀態(tài)

            一個(gè)線程默認(rèn)的狀態(tài)是joinable,如果線程是joinable狀態(tài),當(dāng)線程函數(shù)自己返回退出時(shí)或pthread_exit時(shí)都不會(huì)釋放線程所占用堆棧和線程描述符(總計(jì)8K多)。只有當(dāng)你調(diào)用了pthread_join之后這些資源才會(huì)被釋放。
            若是unjoinable狀態(tài)的線程,這些資源在線程函數(shù)退出時(shí)或pthread_exit時(shí)自動(dòng)會(huì)被釋放。

            unjoinable屬性可以在pthread_create時(shí)指定,或在線程創(chuàng)建后在線程中pthread_detach自己, 如:pthread_detach(pthread_self()),將狀態(tài)改為unjoinable狀態(tài),確保資源的釋放。如果線程狀態(tài)為 joinable,需要在之后適時(shí)調(diào)用pthread_join.

            pthread_self()函數(shù)用來(lái)獲取當(dāng)前調(diào)用該函數(shù)的線程的線程ID
            NAME
            pthread_self - get the calling thread ID

            SYNOPSIS
            #include <pthread.h>

            pthread_t pthread_self(void);

            DESCRIPTION
            The pthread_self() function shall return the thread ID of the calling thread.

            RETURN VALUE
            Refer to the DESCRIPTION.

            ERRORS
            No errors are defined.

            The pthread_self() function shall not return an error code of [EINTR].

            The following sections are informative.

            /*example:test.c*/

            #include<pthread.h>
            #include<stdio.h>
            #include<unistd.h>

            void print_message( void *ptr );

            int main( int argc, char *argv[] )
            {
            pthread_t thread_id;
            while( 1 )
            {
            pthread_create( &thread_id, NULL, (void *)print_message, (void *)NULL );// 一個(gè)線程默認(rèn)的狀態(tài)是joinable

            }

            return 0;
            }

            void print_message( void *ptr )
            {
            pthread_detach(pthread_self());//pthread_detach(pthread_self()),將狀態(tài)改為unjoinable狀態(tài),確保資源的釋放
            static int g;
            printf("%d\n", g++);
            pthread_exit(0) ;//pthread_exit時(shí)自動(dòng)會(huì)被釋放

            posted @ 2014-02-12 22:12 不會(huì)飛的鳥 閱讀(2724) | 評(píng)論 (0)編輯 收藏

            linux下IPTABLES配置詳解

            如果你的IPTABLES基礎(chǔ)知識(shí)還不了解,建議先去看看.
            開(kāi)始配置
            我們來(lái)配置一個(gè)filter表的防火墻.
            (1)查看本機(jī)關(guān)于IPTABLES的設(shè)置情況
            [root@tp ~]# iptables -L -n
            Chain INPUT (policy ACCEPT)
            target       prot opt source                 destination         
            Chain FORWARD (policy ACCEPT)
            target       prot opt source                 destination         
            Chain OUTPUT (policy ACCEPT)
            target       prot opt source                 destination         
            Chain RH-Firewall-1-INPUT (0 references)
            target       prot opt source                 destination         
            ACCEPT       all    --    0.0.0.0/0              0.0.0.0/0           
            ACCEPT       icmp --    0.0.0.0/0              0.0.0.0/0             icmp type 255
            ACCEPT       esp    --    0.0.0.0/0              0.0.0.0/0           
            ACCEPT       ah     --    0.0.0.0/0              0.0.0.0/0           
            ACCEPT       udp    --    0.0.0.0/0              224.0.0.251           udp dpt:5353
            ACCEPT       udp    --    0.0.0.0/0              0.0.0.0/0             udp dpt:631
            ACCEPT       all    --    0.0.0.0/0              0.0.0.0/0             state RELATED,ESTABLISHED
            ACCEPT       tcp    --    0.0.0.0/0              0.0.0.0/0             state NEW tcp dpt:22
            ACCEPT       tcp    --    0.0.0.0/0              0.0.0.0/0             state NEW tcp dpt:80
            ACCEPT       tcp    --    0.0.0.0/0              0.0.0.0/0             state NEW tcp dpt:25
            REJECT       all    --    0.0.0.0/0              0.0.0.0/0             reject-with icmp-host-prohibited
            可以看出我在安裝linux時(shí),選擇了有防火墻,并且開(kāi)放了22,80,25端口.
            如果你在安裝linux時(shí)沒(méi)有選擇啟動(dòng)防火墻,是這樣的
            [root@tp ~]# iptables -L -n
            Chain INPUT (policy ACCEPT)
            target       prot opt source                 destination         
            Chain FORWARD (policy ACCEPT)
            target       prot opt source                 destination         
            Chain OUTPUT (policy ACCEPT)
            target       prot opt source                 destination  
            什么規(guī)則都沒(méi)有.
            (2)清除原有規(guī)則.
            不管你在安裝linux時(shí)是否啟動(dòng)了防火墻,如果你想配置屬于自己的防火墻,那就清除現(xiàn)在filter的所有規(guī)則.
            [root@tp ~]# iptables -F        清除預(yù)設(shè)表filter中的所有規(guī)則鏈的規(guī)則
            [root@tp ~]# iptables -X        清除預(yù)設(shè)表filter中使用者自定鏈中的規(guī)則
            我們?cè)趤?lái)看一下
            [root@tp ~]# iptables -L -n
            Chain INPUT (policy ACCEPT)
            target       prot opt source                 destination         
            Chain FORWARD (policy ACCEPT)
            target       prot opt source                 destination         
            Chain OUTPUT (policy ACCEPT)
            target       prot opt source                 destination      
            什么都沒(méi)有了吧,和我們?cè)诎惭blinux時(shí)沒(méi)有啟動(dòng)防火墻是一樣的.(提前說(shuō)一句,這些配置就像用命令配置IP一樣,重起就會(huì)失去作用),怎么保存.
            [root@tp ~]# /etc/rc.d/init.d/iptables save
            這樣就可以寫到/etc/sysconfig/iptables文件里了.寫入后記得把防火墻重起一下,才能起作用.
            [root@tp ~]# service iptables restart
            現(xiàn)在IPTABLES配置表里什么配置都沒(méi)有了,那我們開(kāi)始我們的配置吧
            (3)設(shè)定預(yù)設(shè)規(guī)則
            [root@tp ~]# iptables -p INPUT DROP
            [root@tp ~]# iptables -p OUTPUT ACCEPT
            [root@tp ~]# iptables -p FORWARD DROP
            上面的意思是,當(dāng)超出了IPTABLES里filter表里的兩個(gè)鏈規(guī)則(INPUT,FORWARD)時(shí),不在這兩個(gè)規(guī)則里的數(shù)據(jù)包怎么處理呢,那就是DROP(放棄).應(yīng)該說(shuō)這樣配置是很安全的.我們要控制流入數(shù)據(jù)包
            而對(duì)于OUTPUT鏈,也就是流出的包我們不用做太多限制,而是采取ACCEPT,也就是說(shuō),不在著個(gè)規(guī)則里的包怎么辦呢,那就是通過(guò).
            可以看出INPUT,FORWARD兩個(gè)鏈采用的是允許什么包通過(guò),而OUTPUT鏈采用的是不允許什么包通過(guò).
            這樣設(shè)置還是挺合理的,當(dāng)然你也可以三個(gè)鏈都DROP,但這樣做我認(rèn)為是沒(méi)有必要的,而且要寫的規(guī)則就會(huì)增加.但如果你只想要有限的幾個(gè)規(guī)則是,如只做WEB服務(wù)器.還是推薦三個(gè)鏈都是DROP.
            注:如果你是遠(yuǎn)程SSH登陸的話,當(dāng)你輸入第一個(gè)命令回車的時(shí)候就應(yīng)該掉了.因?yàn)槟銢](méi)有設(shè)置任何規(guī)則.
            怎么辦,去本機(jī)操作唄!
            (4)添加規(guī)則.
            首先添加INPUT鏈,INPUT鏈的默認(rèn)規(guī)則是DROP,所以我們就寫需要ACCETP(通過(guò))的鏈
            為了能采用遠(yuǎn)程SSH登陸,我們要開(kāi)啟22端口.
            [root@tp ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
            [root@tp ~]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT (注:這個(gè)規(guī)則,如果你把OUTPUT 設(shè)置成DROP的就要寫上這一部,好多人都是望了寫這一部規(guī)則導(dǎo)致,始終無(wú)法SSH.在遠(yuǎn)程一下,是不是好了.
            其他的端口也一樣,如果開(kāi)啟了web服務(wù)器,OUTPUT設(shè)置成DROP的話,同樣也要添加一條鏈:
            [root@tp ~]# iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT ,其他同理.)
            如果做了WEB服務(wù)器,開(kāi)啟80端口.
            [root@tp ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
            如果做了郵件服務(wù)器,開(kāi)啟25,110端口.
            [root@tp ~]# iptables -A INPUT -p tcp --dport 110 -j ACCEPT
            [root@tp ~]# iptables -A INPUT -p tcp --dport 25 -j ACCEPT
            如果做了FTP服務(wù)器,開(kāi)啟21端口
            [root@tp ~]# iptables -A INPUT -p tcp --dport 21 -j ACCEPT
            [root@tp ~]# iptables -A INPUT -p tcp --dport 20 -j ACCEPT
            如果做了DNS服務(wù)器,開(kāi)啟53端口
            [root@tp ~]# iptables -A INPUT -p tcp --dport 53 -j ACCEPT
            如果你還做了其他的服務(wù)器,需要開(kāi)啟哪個(gè)端口,照寫就行了.
            上面主要寫的都是INPUT鏈,凡是不在上面的規(guī)則里的,都DROP
            允許icmp包通過(guò),也就是允許ping,
            [root@tp ~]# iptables -A OUTPUT -p icmp -j ACCEPT (OUTPUT設(shè)置成DROP的話)
            [root@tp ~]# iptables -A INPUT -p icmp -j ACCEPT    (INPUT設(shè)置成DROP的話)
            允許loopback!(不然會(huì)導(dǎo)致DNS無(wú)法正常關(guān)閉等問(wèn)題)
            IPTABLES -A INPUT -i lo -p all -j ACCEPT (如果是INPUT DROP)
            IPTABLES -A OUTPUT -o lo -p all -j ACCEPT(如果是OUTPUT DROP)
            下面寫OUTPUT鏈,OUTPUT鏈默認(rèn)規(guī)則是ACCEPT,所以我們就寫需要DROP(放棄)的鏈.
            減少不安全的端口連接
            [root@tp ~]# iptables -A OUTPUT -p tcp --sport 31337 -j DROP
            [root@tp ~]# iptables -A OUTPUT -p tcp --dport 31337 -j DROP
            有些些特洛伊木馬會(huì)掃描端口31337到31340(即黑客語(yǔ)言中的 elite 端口)上的服務(wù)。既然合法服務(wù)都不使用這些非標(biāo)準(zhǔn)端口來(lái)通信,阻塞這些端口能夠有效地減少你的網(wǎng)絡(luò)上可能被感染的機(jī)器和它們的遠(yuǎn)程主服務(wù)器進(jìn)行獨(dú)立通信的機(jī)會(huì)
            還有其他端口也一樣,像:31335、27444、27665、20034 NetBus、9704、137-139(smb),2049(NFS)端口也應(yīng)被禁止,我在這寫的也不全,有興趣的朋友應(yīng)該去查一下相關(guān)資料.
            當(dāng)然出入更安全的考慮你也可以包OUTPUT鏈設(shè)置成DROP,那你添加的規(guī)則就多一些,就像上邊添加
            允許SSH登陸一樣.照著寫就行了.
            下面寫一下更加細(xì)致的規(guī)則,就是限制到某臺(tái)機(jī)器
            如:我們只允許192.168.0.3的機(jī)器進(jìn)行SSH連接
            [root@tp ~]# iptables -A INPUT -s 192.168.0.3 -p tcp --dport 22 -j ACCEPT
            如果要允許,或限制一段IP地址可用 192.168.0.0/24 表示192.168.0.1-255端的所有IP.
            24表示子網(wǎng)掩碼數(shù).但要記得把 /etc/sysconfig/iptables 里的這一行刪了.
            -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT 因?yàn)樗硎舅械刂范伎梢缘顷?
            或采用命令方式:
            [root@tp ~]# iptables -D INPUT -p tcp --dport 22 -j ACCEPT
            然后保存,我再說(shuō)一邊,反是采用命令的方式,只在當(dāng)時(shí)生效,如果想要重起后也起作用,那就要保存.寫入到/etc/sysconfig/iptables文件里.
            [root@tp ~]# /etc/rc.d/init.d/iptables save
            這樣寫 !192.168.0.3 表示除了192.168.0.3的ip地址
            其他的規(guī)則連接也一樣這么設(shè)置.
            在下面就是FORWARD鏈,FORWARD鏈的默認(rèn)規(guī)則是DROP,所以我們就寫需要ACCETP(通過(guò))的鏈,對(duì)正在轉(zhuǎn)發(fā)鏈的監(jiān)控.
            開(kāi)啟轉(zhuǎn)發(fā)功能,(在做NAT時(shí),FORWARD默認(rèn)規(guī)則是DROP時(shí),必須做)
            [root@tp ~]# iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
            [root@tp ~]# iptables -A FORWARD -i eth1 -o eh0 -j ACCEPT
            丟棄壞的TCP包
            [root@tp ~]#iptables -A FORWARD -p TCP ! --syn -m state --state NEW -j DROP
            處理IP碎片數(shù)量,防止攻擊,允許每秒100個(gè)
            [root@tp ~]#iptables -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT
            設(shè)置ICMP包過(guò)濾,允許每秒1個(gè)包,限制觸發(fā)條件是10個(gè)包.
            [root@tp ~]#iptables -A FORWARD -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
            我在前面只所以允許ICMP包通過(guò),就是因?yàn)槲以谶@里有限制.
            二,配置一個(gè)NAT表放火墻
            1,查看本機(jī)關(guān)于NAT的設(shè)置情況
            [root@tp rc.d]# iptables -t nat -L
            Chain PREROUTING (policy ACCEPT)
            target       prot opt source                 destination         
            Chain POSTROUTING (policy ACCEPT)
            target       prot opt source                 destination         
            SNAT         all    --    192.168.0.0/24         anywhere              to:211.101.46.235
            Chain OUTPUT (policy ACCEPT)
            target       prot opt source                 destination    
            我的NAT已經(jīng)配置好了的(只是提供最簡(jiǎn)單的代理上網(wǎng)功能,還沒(méi)有添加防火墻規(guī)則).關(guān)于怎么配置NAT,參考我的另一篇文章
            當(dāng)然你如果還沒(méi)有配置NAT的話,你也不用清除規(guī)則,因?yàn)镹AT在默認(rèn)情況下是什么都沒(méi)有的
            如果你想清除,命令是
            [root@tp ~]# iptables -F -t nat
            [root@tp ~]# iptables -X -t nat
            [root@tp ~]# iptables -Z -t nat
            2,添加規(guī)則
            添加基本的NAT地址轉(zhuǎn)換,(關(guān)于如何配置NAT可以看我的另一篇文章),
            添加規(guī)則,我們只添加DROP鏈.因?yàn)槟J(rèn)鏈全是ACCEPT.
            防止外網(wǎng)用內(nèi)網(wǎng)IP欺騙
            [root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 10.0.0.0/8 -j DROP
            [root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 172.16.0.0/12 -j DROP
            [root@tp sysconfig]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.0.0/16 -j DROP
            如果我們想,比如阻止MSN,QQ,BT等的話,需要找到它們所用的端口或者IP,(個(gè)人認(rèn)為沒(méi)有太大必要)
            例:
            禁止與211.101.46.253的所有連接
            [root@tp ~]# iptables -t nat -A PREROUTING    -d 211.101.46.253 -j DROP
            禁用FTP(21)端口
            [root@tp ~]# iptables -t nat -A PREROUTING -p tcp --dport 21 -j DROP
            這樣寫范圍太大了,我們可以更精確的定義.
            [root@tp ~]# iptables -t nat -A PREROUTING    -p tcp --dport 21 -d 211.101.46.253 -j DROP
            這樣只禁用211.101.46.253地址的FTP連接,其他連接還可以.如web(80端口)連接.
            按照我寫的,你只要找到QQ,MSN等其他軟件的IP地址,和端口,以及基于什么協(xié)議,只要照著寫就行了.
            最后:
            drop非法連接
            [root@tp ~]# iptables -A INPUT     -m state --state INVALID -j DROP
            [root@tp ~]# iptables -A OUTPUT    -m state --state INVALID -j DROP
            [root@tp ~]# iptables-A FORWARD -m state --state INVALID -j DROP
            允許所有已經(jīng)建立的和相關(guān)的連接
            [root@tp ~]# iptables-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
            [root@tp ~]# iptables-A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

            [root@tp ~]# /etc/rc.d/init.d/iptables save

            這樣就可以寫到/etc/sysconfig/iptables文件里了.寫入后記得把防火墻重起一下,才能起作用.

            [root@tp ~]# service iptables restart


            別忘了保存,不行就寫一部保存一次.你可以一邊保存,一邊做實(shí)驗(yàn),看看是否達(dá)到你的要求,
            上面的所有規(guī)則我都試過(guò),沒(méi)有問(wèn)題.
            寫這篇文章,用了我將近1個(gè)月的時(shí)間.查找資料,自己做實(shí)驗(yàn),希望對(duì)大家有所幫助.如有不全及不完善的地方還請(qǐng)?zhí)岢?
            因?yàn)楸酒恼乱耘渲脼橹?關(guān)于IPTABLES的基礎(chǔ)知識(shí)及指令命令說(shuō)明等我會(huì)盡快傳上,當(dāng)然你可以去網(wǎng)上搜索一下,還是很多的.

            posted @ 2013-12-21 22:37 不會(huì)飛的鳥 閱讀(254) | 評(píng)論 (0)編輯 收藏

            echo——輸出顏色字體

            名稱
                   echo - 顯示一行文本
            概要
                   echo [選項(xiàng)]... [字符串]...
            描述
                   向標(biāo)準(zhǔn)輸出輸出字符串。
                   -n     不輸出換行符
                   -e     打開(kāi)反斜杠ESC轉(zhuǎn)義
                   -E     取消反斜杠ESC轉(zhuǎn)義 (默認(rèn))
                   --help 顯示幫助
                   --version 顯示版本
                   \a     alert (BEL)
                   \b     backspace
                   \c     produce no further output
                   \e     escape
                   \f     form feed
                   \n     new line
                   \r     carriage return
                   \t     horizontal tab
                   \v     vertical tab
                   \0NNN  byte with octal value NNN (1 to 3 digits)
                   \xHH   byte with hexadecimal value HH (1 to 2 digits)

            -e 用來(lái)開(kāi)啟echo中的轉(zhuǎn)義,\e 或 \033 來(lái)輸出<Esc>符號(hào)
            設(shè)置顏色的格式: \e[背景色;前景色;高亮m輸出字符 \e[0m

            $ echo -e '\033[31;44;1mThis is print\033[0m'
            $ echo -e '\e[31;44;1mThis is print\e[0m'

             背景色:0 透明(使用終端顏色), 40 黑, 41 紅, 42 綠, 43 黃, 44 藍(lán) 45 紫, 46 青綠, 47白(灰)
             前景色: 30 黑 31 紅, 32 綠, 33 黃, 34 藍(lán), 35 紫, 36 青綠, 37 白(灰)
             高亮度:高亮是1,不高亮是0。注意m后面緊跟字符串。

            posted @ 2013-10-10 15:15 不會(huì)飛的鳥 閱讀(365) | 評(píng)論 (0)編輯 收藏

            Linux下printf輸出字符串的顏色

            printf("/033[字背景顏色;字體顏色m字符串/033[0m" );

            printf("/033[41;32m字體背景是紅色,字是綠色/033[0m/n");
            41是字背景顏色, 32是字體的顏色, 字體背景是紅色,字是綠色是字符串.  后面的/033[0m是控制碼.

            顏色代碼:
            QUOTE:
            字背景顏色范圍: 40--49                   字顏色: 30--39

                            40: 黑                           30: 黑
                            41: 紅                           31: 紅
                            42: 綠                           32: 綠
                            43: 黃                           33: 黃
                            44: 藍(lán)                           34: 藍(lán)
                            45: 紫                           35: 紫
                            46: 深綠                         36: 深綠
                            47: 白色                         37: 白色

            ANSI控制碼:
            QUOTE:
              /033[0m   關(guān)閉所有屬性  
              /033[1m   設(shè)置高亮度  
              /03[4m   下劃線  
              /033[5m   閃爍  
              /033[7m   反顯  
              /033[8m   消隱  
              /033[30m   --   /033[37m   設(shè)置前景色  
              /033[40m   --   /033[47m   設(shè)置背景色  
              /033[nA   光標(biāo)上移n行  
              /03[nB   光標(biāo)下移n行  
              /033[nC   光標(biāo)右移n行  
              /033[nD   光標(biāo)左移n行  
              /033[y;xH設(shè)置光標(biāo)位置  
              /033[2J   清屏  
              /033[K   清除從光標(biāo)到行尾的內(nèi)容  
              /033[s   保存光標(biāo)位置  
              /033[u   恢復(fù)光標(biāo)位置  
              /033[?25l   隱藏光標(biāo)  
              /33[?25h   顯示光標(biāo)

            這樣, 在某些時(shí)候就可以實(shí)現(xiàn)動(dòng)態(tài)的輸出.

            如圖:


            posted @ 2013-10-07 15:39 不會(huì)飛的鳥 閱讀(2353) | 評(píng)論 (2)編輯 收藏

            一款簡(jiǎn)單的正則表達(dá)式處理模塊([轉(zhuǎn)]Fast-regular-expressions)

            原文出處:http://www.codeproject.com/Articles/798/Fast-regular-expressions

            Fast regular expressions

            By , 29 Oct 2000
             

            Sample Image - RexSearch.jpg

            Introduction

            Regular expressions are a well recognized way for describing string patterns. The following regular expression defines a floating point number with a (possibly empty) integer part, a non empty fractional part and an optional exponent:

            Collapse | Copy Code
            [0-9]* \.[0-9]+ ([Ee](\+|-)?[0-9]+)?

            The rules for interpreting and constructing such regular expressions are explained below. A regular expression parser takes a regular expression and a source string as arguments and returns the source position of the first match. Regular expression parsers either interpret the search pattern at runtime or they compile the regular expression into an efficient internal form (known as deterministic finite automaton). The regular expression parser described here belongs to the second category. Besides being quite fast, it also supports dictionaries of regular expressions. With the definitions $Int= [0-9], $Frac= \.[0-9]+ and $Exp= ([Ee](\+|-)?[0-9]+), the above regular expression for a floating point number can be abbreviated to $Int* $Frac $Exp?.

            Interface

            I separated algorithmic from interface issues. The files RexAlgorithm.h and RexAlgorithm.cpp implement the regular expression parser using only standard C++ (relying on STL), whereas the file RexInterface.h and RexInterface.cpp contain the interfaces for the end user. Currently there is only one interface, implemented in the class REXI_Search. Interfaces for replace functionality and for programming language scanners are planned for future releases.

            Collapse | Copy Code
            struct REXI_DefErr{
                enum{eNoErr,eErrInName,eErrInRegExp} eErrCode;
                string  strErrMsg;
                int     nErrOffset;
                };
                class REXI_Search : public REXI_Base
                {
                public:
                REXI_Search(char cEos='\0');
                REXI_DefErr
                AddRegDef   (string strName,string strRegExp);
                inline  REXI_DefErr
                SetRegexp  (string strRegExp);
                bool    MatchHere   (const char*& rpcszSrc, int& nMatchLen,bool& bEos);
                bool    Find        (const char*& rpcszSrc, int& nMatchLen,bool& bEos);
                private:
                bool    MatchHereImpl();
                int     m_nIdAnswer;
                };

            Example usage

            Collapse | Copy Code
            int main(int argc, char* argv[])
                {
                const char szTestSrc[]= "3.1415 is the same as 31415e-4";
                const int ncOk= REXI_DefErr::eNoErr;
                REXI_Search rexs;
                REXI_DefErr err;
                err= rexs.AddRegDef("$Int","[0-9]+");  assert(err.eErrCode==ncOk);
                err= rexs.AddRegDef("$Frac","\\.[0-9]+"); assert(err.eErrCode==ncOk);
                err= rexs.AddRegDef("$Exp","([Ee](\\+|-)?[0-9]+)");
                assert(err.eErrCode==ncOk);
                err= rexs.SetRegexp("($Int? $Frac $Exp?|$Int \\. $Exp?|$Int $Exp)[fFlL]?");
                assert(err.eErrCode==ncOk);
                const char*     pCur= szTestSrc;
                int             nMatchLen;
                bool            bEosFound= false;
                cout    <<  "Source text is: \""    <<  szTestSrc   << "\"" <<  endl;
                while(rexs.Find(pCur,nMatchLen,bEosFound)){
                cout <<  "Floating point number found  at position "
                <<  ((pCur-szTestSrc)-nMatchLen)
                <<  " having length "  <<  nMatchLen  <<  endl;
                }
                int i;
                cin >> i;
                return 0;
                }

            Performance issues

            A call to the member function REXI_Search::SetRegexp(strRegExp)involves quite a lot of computing. The regular expression strRegExp is analyzed and after several steps transformed into a compiled form. Because of this preprocessing work, which is not needed in the case of an interpreting regular expression parser, this regular expression parser shows its efficiency only when you apply it to large input strings or if you are searching again and again for the same regular expression. A typical application which profits from the preprocessing needed by this parser is a utility which searches all files in a directory.

            Limitations

            Currently Unicode is not supported. There is no fundamental reason for this limitation and I think that a later release will correct this. I just did not yet find an efficient representation of a compiled regular expression which supports Unicode.

            Constructing regular expressions

            Regular expressions can be built from characters and special symbols. There are some similarities between regular expressions and arithmetic expressions. The most basic elements of arithmetic expressions are numbers and expressions enclosed in parens ( ). The most basic elements of regular expressions are characters, regular expressions enclosed in parens ( ) and character sets. On the next higher level, arithmetic expressions have '*' and '/' operators, whereas regular expressions have operators indicating the multiplicity of the preceding element.

            Most basic elements of regular expressions

            • Individual characters. e.g. "h" is a regular expression. In the string "this home" it matches the beginning of 'home'. For non printable characters, one has to use either the notation \xhh where h means a hexadecimal digit or one of the escape sequences \n \r \t \v known from "C". Because the characters * + ? . | [ ] ( ) - $ ^ have a special meaning in regular expressions, escape sequences must also be used to specify these characters literally: \*  \+  \?  \.  \|  \[  \]  \(  \)  \-  \$  \^ . Furthermore, use '\ ' to indicate a space, because this implementation skips spaces in order to support a more readable style.
            • Character sets enclosed in square brackets [ ]. e.g. "[A-Za-z_$]" matches any alphabetic character, the underscore and the dollar sign (the dash (-) indicates a range), e.g. [A-Za-z$_] matches "B", "b", "_", "$" and so on. A ^ immediately following the [ of a character set means 'form the inverse character set'. e.g. "[^0-9A-Za-z]" matches non-alphanumeric characters.
            • Expressions enclosed in round parens ( ). Any regular expression can be used on the lowest level by enclosing it in round brackets.
            • the dot . It means 'match any character'.
            • an identifier prefixed by a $. It refers to an already defined regular expression. e.g. "$Ident" stands for a user defined regular expression previously defined. Think of it as a regular expression enclosed in round parens, which has a name.

            Operators indicating the multiplicity of the preceding element

            Any of the above five basic regular expressions can be followed by one of the special characters * + ? /i

            • * meaning repetition (possibly zero times); e.g. "[0-9]*" not only matches "8" but also "87576" and even the empty string "".
            • + meaning at least one occurrence; e.g. "[0-9]+" matches "8", "9185278", but not the empty string.
            • ? meaning at most one occurrence; e.g. "[$_A-Z]?" matches "_", "U", "$", .. and ""
            • \i meaning ignore case

            Catenation of regular expressions

            The regular expressions described above can be catenated to form longer regular expressions. E.g. "[_A-Za-z][_A-Za-z0-9]*" is a regular expression which matches any identifier of the programming language "C", namely the first character must be alphabetic or an underscore and the following characters must be alphanumeric or an underscore. "[0-9]*\.[0-9]+" describes a floating point number with an arbitrary number of digits before the decimal point and at least one digit following the decimal point. (The decimal point must be preceded by a backslash, otherwise the dot would mean 'accept any character at this place'). "(Hallo (,how are you\?)?)\i" matches "Hallo" as well as "Hallo, how are you?" in a case insensitive way.

            Alternative regular expressions

            Finally - on the top level - regular expressions can be separated by the | character. The two regular expressions on the left and right side of the | are alternatives, meaning that either the left expression or the right expression should match the source text. E.g. "[0-9]+ | [A-Za-z_][A-Za-z_0-9]*" matches either an integer or a "C"-identifier.

            A complex example

            The programming language "C" defines a floating point constant in the following way: A floating point constant has the following parts: An integer part, a decimal point, a fraction, an exponential part beginning with e or E followed by an optional sign and digits and an optional type suffix formed by one the characters f, F, l, L. Either the integer part or the fractional part can be absent (but not both). Either the decimal point or the exponential part can be absent (but not both).

            The corresponding regular expression is quite complex, but it can be simplified by using the following definitions:

            Collapse | Copy Code
            $Int = "[0-9]+."
                $Frac= "\.[0-9]+".
                $Exp = "([Ee](\+|-)?[0-9]+)".

            So we get the following expression for a floating point constant:

            Collapse | Copy Code
            ($Int? $Frac $Exp?|$Int \. $Exp?|$Int $Exp)[fFlL]?

            posted @ 2013-01-08 19:45 不會(huì)飛的鳥 閱讀(385) | 評(píng)論 (0)編輯 收藏

            [轉(zhuǎn)]linux下c/c++方式訪問(wèn)curl的幫助手冊(cè)

                 摘要: 有個(gè)業(yè)務(wù)需求需要通過(guò)curl 代理的方式來(lái)訪問(wèn)外網(wǎng) 百度了一把,測(cè)試可以正常使用。記錄下來(lái)方便后續(xù)查找   example:   1. http://curl.haxx.se/libcurl/c/example.html  2. http://www.libcurl.org/book:  1. http://www.linuxdevcente...  閱讀全文

            posted @ 2012-11-12 23:51 不會(huì)飛的鳥 閱讀(7855) | 評(píng)論 (0)編輯 收藏

            [轉(zhuǎn)]支持V4協(xié)議飛信機(jī)器人發(fā)布(20101205002服務(wù)/命令模式均支持)

            支持V4協(xié)議飛信機(jī)器人發(fā)布(20101205002服務(wù)/命令模式均支持)

            看到論壇上有人質(zhì)疑飛信機(jī)器人彈出圖形驗(yàn)證碼的行為是為了賺錢,見(jiàn)該貼:http://bbs.it-adv.net/viewthread.php?tid=1096(請(qǐng)飛信機(jī)器人學(xué)學(xué)360啦)

            在此聲明,彈出圖形驗(yàn)證碼是官方飛信服務(wù)器需要的,不是我程序的行為。不信的話,可以自己去抓包分析。請(qǐng)不要在論壇上抗議圖形碼的事情,要抗議,打10086去抗議。飛信機(jī)器人命令行版本永久免費(fèi)使用。





            移動(dòng)發(fā)布公告(http://feixin.10086.cn/bulletin/2521/1):11月20日之后,將停止3.5及以下版本飛信的支持。特此發(fā)布新版支持V4協(xié)議的機(jī)器人程序,請(qǐng)使用原09版機(jī)器人的朋友迅速測(cè)試并升級(jí)。

            新版飛信機(jī)器人版本號(hào)起于 20101113002,凡之前版本的飛信機(jī)器人,11月20日之后將不能使用。

            1. 遇到輸入圖形驗(yàn)證碼時(shí),自動(dòng)生成圖形驗(yàn)證碼,用戶可以手工輸入識(shí)別后的驗(yàn)證碼(解決之前的422問(wèn)題,識(shí)別及輸入方法見(jiàn)Q&A)。
            2. 可以設(shè)定遇到圖形驗(yàn)證時(shí)的行為,退出( --exit-on-verifycode=1)或者手工輸入(當(dāng)機(jī)器人后臺(tái)執(zhí)行時(shí),如果等待輸入將導(dǎo)致程序無(wú)限期等待)
            3. 第一次運(yùn)行時(shí)將緩存配置數(shù)據(jù)(文件名:登錄賬號(hào).cache),之后運(yùn)行自動(dòng)加載緩存,提高發(fā)送速度。
            4. 集成推立方(http://www.tui3.com)短信發(fā)送協(xié)議,發(fā)送到聯(lián)通、電信手機(jī)或遇飛信服務(wù)器不可用時(shí),可以直接使用本客戶端進(jìn)行發(fā)送(注:該服務(wù)為收費(fèi)服務(wù),詳細(xì)情況請(qǐng)見(jiàn):http://www.tui3.com/page/tuixin )


            和下載以往版本一樣,先回復(fù)后下載噢。

            >> 安裝 <<
                本程序?yàn)榫G色程序,無(wú)需安裝,下載解壓后即可使用。
                1. 根據(jù)您的需求,分別下載附件中的windows或者linux版本的機(jī)器人主程序(回貼后才能下載噢,請(qǐng)別嫌麻煩),解壓(注意:主程序所在目錄的路徑中不要存在空格,如   c:\Program files\... 這樣的目錄)
                下載:
               
            以下內(nèi)容需要回復(fù)才能看到

            windows版本:  fetion.rar (156.81 KB)   linux版本:  fetion (491.63 KB) 





                2. 下載機(jī)器人支持庫(kù)(使用機(jī)器人以前版本的朋友可以直接忽略),把壓縮包中的文件解壓到主程序相同的目錄
                windows 版本:http://www.it-adv.net/fetion/win32dll_20101113.rar
                linux版本:http://www.it-adv.net/fetion/linuxso_20101113.rar (在Redhat4 下編譯,其它LINUX發(fā)行版的朋友請(qǐng)測(cè)試)
                linux64位(centos5.4)版本: http://www.it-adv.net/fetion/cenos54X64_20101113.rar (感謝QQ“走過(guò)你的風(fēng)”網(wǎng)友提供。Centos5.4 64位linux系統(tǒng)下,如果用上面提供的linux版本動(dòng)態(tài)庫(kù),會(huì)提示Segmentation fault,程序異常退出)

                (為何分開(kāi)下載?因?yàn)闄C(jī)器人主程序經(jīng)常更新,而支持庫(kù)不會(huì)更新)
               注意:linux用戶,請(qǐng)不要把支持庫(kù)中的 lib* 復(fù)制到 /usr/lib 下,因?yàn)榘l(fā)行版本不同,可能會(huì)覆蓋您機(jī)器中的核心庫(kù),導(dǎo)致嚴(yán)重系統(tǒng)問(wèn)題。您可以把庫(kù)解壓到主程序的相同目錄,然后以 LD_LIBRARY_PATH=. ./fetion 來(lái)運(yùn)行)

            >> 使用說(shuō)明 <<

                 以下參數(shù)提供登錄用的賬號(hào)密碼(三種方式,手機(jī)號(hào)-密碼 飛信號(hào)-密碼 文件--索引)
                
                --mobile=[手機(jī)號(hào)]       登錄手機(jī)號(hào)
                --sid=[飛信號(hào)]        登錄飛信號(hào)
                --pwd=[密碼]         登錄密碼
                --config=[文件名]       存儲(chǔ)手機(jī)號(hào)、密碼的文件。
                --index=[索引號(hào)]       索引

                以下參數(shù)提供接收者
                --to=[手機(jī)號(hào)/飛信號(hào)/URI]   接收消息的手機(jī)號(hào)/飛信號(hào)/URI.如果知道對(duì)方URI,則只需自己在對(duì)方好友列表,無(wú)需對(duì)方在自己好友列表就能發(fā)送。
                        支持多個(gè)號(hào)碼,中間用,逗號(hào)分隔
                --msg-utf8=[信息]  
                 發(fā)送的消息,UTF8編碼
                --msg-gb=[信息]  
                  發(fā)送的消息,GB編碼
                --file-utf8=[文件utf8格式]
                  發(fā)送文件內(nèi)容
                --file-gb=[文件gb格式]
                  發(fā)送文件內(nèi)容
                --msg-type=[0/1/2] 
                  發(fā)送消息類型:普通消息 長(zhǎng)消息 智能短信

                 小工具   
                 --query-cmcc-no  查詢移動(dòng)公司手機(jī)段

                 以下為可選項(xiàng)
                --debug  
                顯示調(diào)試信息
                --hide     
                隱身登錄
                 --exit-on-verifycode
                服務(wù)器需要進(jìn)行圖形驗(yàn)證時(shí),程序退出(1)或者等待用戶手工輸入識(shí)別信息(程序默認(rèn))


                --proxy-ip=http代理ip
                --proxy-port=http代理端口
                (機(jī)器人需要  HTTP CONNECT代理,大家常用的 ccproxy 是支持的)

            >> 舉例 <<

                以下為 windows 下舉例:
                fetion --mobile=13711123456 --pwd=mypwd --to=137xxxxxxxx --msg-gb=測(cè)試
                fetion --sid=6630321 --pwd=mypwd --to=137xxxxxxxx --msg-gb=測(cè)試
                fetion --config=sample.conf --index=1 --to=137xxxxxxxx --msg-gb=測(cè)試

                linux下,請(qǐng)使用如下命令:
                LD_LIBRARY_PATH=. ./fetion --mobile=13711123456 --pwd=mypwd --to=137xxxxxxxx --msg-utf8=測(cè)試
                LD_LIBRARY_PATH=. ./fetion --sid=6630321 --pwd=mypwd --to=137xxxxxxxx --msg-utf8=測(cè)試
                LD_LIBRARY_PATH=. ./fetion --config=sample.conf --index=1 --to=137xxxxxxxx --msg-utf8=測(cè)試

              
                發(fā)送消息中如果需要換行,請(qǐng)用 \n 

              // 以下為 sample.conf 內(nèi)容,文件內(nèi)容中, #號(hào)為注釋行 

              # This config file is for fetion robot tool.
               # Usage demo: ./fetion --config=/etc/fetion.conf --index=1
               # ID Mobile  Password
               1  137xxxx  1234234

            使用推立方收費(fèi)短信服務(wù):
              fetion --mobile=接收人手機(jī) --t3key=推立方APIKEY --msg-gb=gbk編碼的發(fā)送內(nèi)容( 或者 --msg-utf8=utf8格式的內(nèi)容 )
              推立方APIKEY:到推立方官方網(wǎng)站(http://www.tui3.com/)注冊(cè)會(huì)員(注冊(cè)成功后,贈(zèng)送10條短信),進(jìn)行產(chǎn)品配置,則可以獲取該KEY。

            新版提示:
            1. 使用復(fù)雜的密碼(數(shù)字+字母+符號(hào)),將不會(huì)彈出圖形驗(yàn)證碼(我測(cè)試時(shí)是這種表現(xiàn))。
            2. 第一次使用機(jī)器人時(shí),即使是復(fù)雜密碼,也可能會(huì)彈出圖形驗(yàn)證碼,以后將不再提示。
            3. 如果您是使用其它程序調(diào)用本程序,請(qǐng)?jiān)谶\(yùn)行時(shí),指定--exit-on-verifycode=1,否則,程序?qū)o(wú)限期等待。當(dāng)您以該參數(shù)運(yùn)行機(jī)器人時(shí),當(dāng)彈出圖形驗(yàn)證碼時(shí),程序?qū)⒁酝顺龃a29結(jié)束( 在 linux 中,通過(guò)$?獲取,在 windows中,通過(guò) %ERRORLEVEL% 獲取)
            4.  請(qǐng)確保目錄權(quán)限可寫




            補(bǔ)充Q&A:
            1.WIN2003不能使用:感謝32樓lvjinhua提供的解決辦法“win2003不能用的問(wèn)題,安裝 vs2008 sp1的vcredist_x86.exe(http://www.microsoft.com/downloads/en/confirmation.aspx?familyid=a5c84275-3b97-4ab7-a40d-3802b2af5fc2&displaylang=en) 就好了!”
            2.運(yùn)行時(shí)顯示的中文亂碼:程序運(yùn)行時(shí),LINUX環(huán)境以UTF8編碼輸出,WINDOWS環(huán)境以GBK編碼輸出,請(qǐng)注意您使用控制臺(tái)的編碼方式。另外,即使中文亂碼也不影響您的使用,那句話的提示就是讓您打開(kāi) 圖形文件,輸入圖片識(shí)別碼。
            3.WINDOWS下密碼中如果有特殊字符如何輸入,比如&|: 請(qǐng)用 ^ 進(jìn)行轉(zhuǎn)義,如密碼中有 &,請(qǐng)輸入 ^&
            4.輸過(guò)一次驗(yàn)證碼以后,以后還會(huì)再要求輸入嗎?目前的表現(xiàn)是這樣(如過(guò)一次,再登錄就不會(huì)輸入),但不保證以后移動(dòng)飛信服務(wù)器修改驗(yàn)證規(guī)則以后,會(huì)不會(huì)再?gòu)?qiáng)制你輸入(比如飛信服務(wù)器認(rèn)為你的賬號(hào)有異常,或者你的賬號(hào)頻繁登錄、頻繁發(fā)短信)。
            5.如何輸入圖片驗(yàn)證碼?因?yàn)轱w信機(jī)器人是控制臺(tái)程序,無(wú)法顯示圖片,所以,請(qǐng)把生成的圖片用查看圖片的辦法打開(kāi)后進(jìn)行識(shí)別。如果您的環(huán)境是在linux下,并且沒(méi)有X環(huán)境,那么您可以把圖片下載到WINDOWS機(jī)器中查看。人工識(shí)別后,把識(shí)別后的內(nèi)容輸入即可。
            6.494錯(cuò)誤:發(fā)送U到12520,就可以解除受限。

            更新日志:
            20101205002:支持服務(wù)模式
            20101115005:FIXBUG:某些環(huán)境下,不能獲取圖形驗(yàn)證碼,提示:getpiccodev4 return error xml(感謝網(wǎng)友QQ五斗米的協(xié)助)
            20101113002:支持飛信最新V4協(xié)議初始版本





            服務(wù)模式開(kāi)發(fā)使用指南

            在服務(wù)模式下,飛信機(jī)器人將長(zhǎng)期在線,可以用來(lái)構(gòu)造交互性的機(jī)器人應(yīng)用。具體演示可以加藍(lán)色理想網(wǎng)站飛信機(jī)器人:806908614。

            服務(wù)模式運(yùn)行方法:
            fetion --mobile=手機(jī)號(hào) --pwd=密碼
            fetion --sid=飛信號(hào)  --pwd=密碼

            服務(wù)模式支持電子郵箱注冊(cè)的飛信號(hào)碼

            服務(wù)模式開(kāi)發(fā)使用資料導(dǎo)航:

            1.飛信機(jī)器人服務(wù)版配置視頻教程:http://bbs.it-adv.net/viewthread.php?tid=188&extra=page%3D1
                (該視頻教程為windows版本,linux版本和此類似)
            2. 飛信機(jī)器人框架配置指南:http://bbs.it-adv.net/viewthread.php?tid=174&extra=page%3D1
            3. 自帶演示框架數(shù)據(jù)庫(kù)說(shuō)明及操作指南(PDF):http://bbs.it-adv.net/viewthread.php?tid=172&extra=page%3D1 
            4. 插件原理:http://bbs.it-adv.net/viewthread.php?tid=28&extra=page%3D1
            5. 機(jī)器人PHP框架及數(shù)據(jù)庫(kù)SQL文件:http://www.it-adv.net/fetion/downng/plugins_sql.rar
            6. 控制指令集:http://wiki.blueidea.com/index.php?title=%E9%A3%9E%E4%BF%A1%E6%9C%BA%E5%99%A8%E4%BA%BA/%E6%8E%A7%E5%88%B6%E6%8C%87%E4%BB%A4%E9%9B%86
            7. 事件插件:http://wiki.blueidea.com/index.php?title=%E9%A3%9E%E4%BF%A1%E6%9C%BA%E5%99%A8%E4%BA%BA/%E4%BA%8B%E4%BB%B6%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91%E8%AF%B4%E6%98%8E
            8.藍(lán)色理想飛信機(jī)器人WIKI:http://wiki.blueidea.com/index.php?title=%E9%A3%9E%E4%BF%A1%E6%9C%BA%E5%99%A8%E4%BA%BA

            服務(wù)版本未授權(quán)的用戶使用時(shí),有如下限制:
            1、不支持加好友請(qǐng)求事件(handle_contact_request)
            2、不支持刪除指令(buddy-delete)
            3、不支持獲取好友信息指令(contact-info)
            4、發(fā)送消息時(shí),后面有網(wǎng)站信息

            新版變化:
            1、新版插件第一個(gè)參數(shù)傳遞的飛信號(hào)碼(以前傳遞的是手機(jī)號(hào),由此帶來(lái)的問(wèn)題是:如果還是用原來(lái)的框架,那么生成的cmd文件,前面是飛信號(hào)。但是飛信機(jī)器人主程序認(rèn)的命令文件是 手機(jī)號(hào)_id.cmd, 所以,請(qǐng)修改相應(yīng)代碼,把飛信號(hào)換成手機(jī)號(hào))
            2、handle_contact_request,傳遞的userid(之前是uri)
            3、buddy-delete 使用 userid
            4、accept_contact_request: 使用 userid
            5、buddy-data: 新增加一個(gè)字段:carrier-region,例如:CN.bj.10.

            升級(jí)注意事項(xiàng):
            1、以前使用飛信機(jī)器人服務(wù)版框架的朋友進(jìn)行升級(jí)時(shí)一定注意:因插件的第一個(gè)參數(shù)由以前的手機(jī)號(hào)改成了飛信號(hào), plugin_buddy_data 中一段代碼需要?jiǎng)h除,否則會(huì)造成好友等幾個(gè)數(shù)據(jù)表清空。
            2、V4協(xié)議中,用戶所屬城市信息由以前的 省+市 改成了 carrier-region ,plugin_buddy_data 中的 province  和 city 不在有效


            posted @ 2012-11-10 10:36 不會(huì)飛的鳥 閱讀(1118) | 評(píng)論 (5)編輯 收藏

            【轉(zhuǎn)】STL_string的字符串替換函數(shù)

            標(biāo)準(zhǔn)C++中的string中的函數(shù)不多,沒(méi)有CString的功能強(qiáng)大,但是如果想在Unicode編碼下使用多字節(jié),就不能使用CString,于是自己寫了一個(gè)類似于CString的Replace函數(shù)。
            string replace( const string& inStr, const char* pSrc, const char* pReplace )
            {
                 string str = inStr;
                string::size_type stStart = 0;
                string::iterator iter = str.begin();
                while( iter != str.end() )
                {
                    // 從指定位置 查找下一個(gè)要替換的字符串的起始位置。
                    string::size_type st = str.find( pSrc, stStart );
                    if ( st == str.npos )
                    {
                        break;
                    }
                    iter = iter + st - stStart;
                    // 將目標(biāo)字符串全部替換。
                    str.replace( iter, iter + strlen( pSrc ), pReplace );
                    iter = iter + strlen( pReplace );
                    // 替換的字符串下一個(gè)字符的位置
                    stStart = st + strlen( pReplace );
                }
                return str;
            }

            上述方法在執(zhí)行replace( "h h h h h h h h h h h h h h h h h h h ", " ", " " )時(shí)出現(xiàn)問(wèn)題。
            下面再列出一種方法:
            string CComFunc::replace( const string& inStr, const char* pSrc, const char* pReplace )
            {
                string strSrc = inStr;
                string::size_type pos=0;      
                string::size_type srclen = strlen( pSrc );       
                string::size_type dstlen = strlen( pReplace );       
                while( (pos=strSrc.find(pSrc, pos)) != string::npos)
                {               
                    strSrc.replace(pos, srclen, pReplace);               
                    pos += dstlen;       
                }
                return strSrc;
            }

            補(bǔ)充,經(jīng)過(guò)測(cè)試,上面方法再執(zhí)行,replace( “暴”, "\\","==" )時(shí),依然會(huì)遇到問(wèn)題。
            在日文系統(tǒng)上,因?yàn)?#8220;暴”占兩個(gè)字節(jié),而"\\"只占一個(gè)字節(jié),但與“暴”的低位字節(jié)ASCII碼相同。
            而string的Find函數(shù),是按照字節(jié)比較的,所以,將這個(gè)字節(jié)替換了,導(dǎo)致文本替換出現(xiàn)問(wèn)題。
            于是考慮到不應(yīng)該按字節(jié)比較,應(yīng)該按字符比較,測(cè)試發(fā)現(xiàn),CString的替換函數(shù)沒(méi)有問(wèn)題,于是考慮按照CString的方法重新寫一個(gè)replace函數(shù)。
            代碼如下:
            因?yàn)镃String在_MBCS和_UNICODE下是變寬的,而我寫的replace函數(shù),只針對(duì)string。
            string CComFunc::replace( const string& inStr, const char* pSrc, const char* pReplace )
            {
                string strSrc = inStr;
                LPSTR lpch = ( CHAR* )strSrc.c_str();
                int   nOldLength = strlen( lpch );
                int    nSourceLen = strlen(pSrc);
                if (nSourceLen == 0)
                {
                    return lpch;
                }
                int   nReplacementLen = strlen(pReplace);
                LPSTR lpszStart = lpch;
                LPSTR lpszEnd = lpszStart + nOldLength;
                LPSTR lpszTarget;

                // 先列出判斷替換字符是否存在的方法, 但在此函數(shù)中不使用這段代碼。
            /*
                // judge whether exist
                while (lpszStart < lpszEnd)
                {
                    while ((lpszTarget = (CHAR*)_mbsstr(( const unsigned char * )lpszStart, ( const unsigned char * )pSrc)) != NULL)
                    {
                        nCount++;
                        lpszStart = lpszTarget + nSourceLen;
                    }
                    lpszStart += strStart.length() + 1;
                }
                *//
               
                // 下面是替換的代碼。
                while (lpszStart < lpszEnd)
                {
                    while ((lpszTarget = (CHAR*)_mbsstr(( const unsigned char * )lpszStart, ( const unsigned char * )pSrc)) != NULL)
                    {
                        int nBalance = nOldLength - (lpszTarget - lpch + nSourceLen);
                        memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
                            nBalance * sizeof(CHAR));
                        memcpy(lpszTarget, pReplace, nReplacementLen*sizeof(CHAR));
                        lpszStart = lpszTarget + nReplacementLen;
                        lpszStart[nBalance] = '\0';
                        nOldLength += (nReplacementLen - nSourceLen);
                    }
                    lpszStart += strlen(lpszStart) + 1;
                }
                return lpch;
            }

            此方法最關(guān)鍵的是_mbsstr函數(shù),在"MBSTRING.H"頭文件中聲明。

            posted @ 2012-10-26 16:20 不會(huì)飛的鳥 閱讀(1465) | 評(píng)論 (0)編輯 收藏

            僅列出標(biāo)題
            共9頁(yè): 1 2 3 4 5 6 7 8 9 
            精品一二三区久久aaa片| 久久久久无码中| 亚洲人成网亚洲欧洲无码久久| 久久九九久精品国产免费直播| 色婷婷综合久久久久中文| 久久香蕉国产线看观看精品yw | 国产亚州精品女人久久久久久 | 精品久久久一二三区| 久久综合亚洲欧美成人| 亚洲一区中文字幕久久| 国产精品久久久久免费a∨| 99久久99久久精品免费看蜜桃| 久久精品无码免费不卡| 亚洲精品国精品久久99热一| 久久久久国产成人精品亚洲午夜| 久久狠狠爱亚洲综合影院| 久久精品国产99国产精品| 丁香五月网久久综合| 亚洲综合精品香蕉久久网| 久久天天躁狠狠躁夜夜不卡| 99久久99久久久精品齐齐| 亚洲午夜无码久久久久| 久久一区二区三区免费| 久久久久久久尹人综合网亚洲| 亚洲精品无码久久久久| 2021久久精品免费观看| 亚洲精品99久久久久中文字幕| 99久久精品无码一区二区毛片| 国产精品禁18久久久夂久| 久久影院综合精品| 久久久久久久女国产乱让韩| 伊人久久无码精品中文字幕| 国产精品丝袜久久久久久不卡| 色综合久久中文综合网| 国产成人精品久久| 久久久精品人妻一区二区三区蜜桃| 无码伊人66久久大杳蕉网站谷歌| 伊人色综合久久天天人手人婷 | 99久久国产亚洲高清观看2024 | 久久国产劲爆AV内射—百度| 久久综合亚洲色一区二区三区|