<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>浮山狼de博客 &#187; linux</title>
	<atom:link href="https://www.fushanlang.com/category/programming-skill/linux/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fushanlang.com</link>
	<description>next station - 下一站，活在当下，且行且思</description>
	<lastBuildDate>Sat, 29 Nov 2014 15:14:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.5</generator>
	<item>
		<title>Linux Skype Robot</title>
		<link>https://www.fushanlang.com/skype-robot-linux-2361/</link>
		<comments>https://www.fushanlang.com/skype-robot-linux-2361/#comments</comments>
		<pubDate>Tue, 25 Feb 2014 01:50:51 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[robot]]></category>
		<category><![CDATA[skype]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=2361</guid>
		<description><![CDATA[Skype Robot  in Ubuntu skype linux 机器人 <p>1.安装所需的软件包</p> apt-get install python-software-properties sudo add-apt-repository "deb http://archive.canonical.com/$(lsb_release -sc) partner" sudo apt-get update sudo apt-get install gcc sudo apt-get install skype sudo apt-get install xvfb sudo apt-get install fluxbox x11vnc sudo apt-get install dbus sudo apt-get install vnc4server <p>http://www.qxs.ch/2011/01/07/skype-instant-messages-from-zabbix/</p> wget http://sourceforge.net/projects/skype4py/files/skype4py/1.0.32.0/Skype4Py-1.0.32.0.tar.gz tar -xzf Skype4Py-1.* cd Skype4Py-1.0.32.0/ python setup.py install <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/skype-robot-linux-2361/">Linux Skype Robot</a></span>]]></description>
				<content:encoded><![CDATA[<h1>Skype Robot  in Ubuntu</h1>
<h2>skype linux 机器人</h2>
<p><strong>1.安装所需的软件包</strong></p>
<pre class="php" name="code">apt-get install python-software-properties
sudo add-apt-repository "deb http://archive.canonical.com/$(lsb_release -sc) partner"
sudo apt-get update
sudo apt-get install gcc
sudo apt-get install skype
sudo apt-get install xvfb
sudo apt-get install fluxbox x11vnc
sudo apt-get install dbus
sudo apt-get install vnc4server</pre>
<p><a href="http://www.qxs.ch/2011/01/07/skype-instant-messages-from-zabbix/" rel="nofollow">http://www.qxs.ch/2011/01/07/skype-instant-messages-from-zabbix/</a></p>
<pre class="php" name="code">wget http://sourceforge.net/projects/skype4py/files/skype4py/1.0.32.0/Skype4Py-1.0.32.0.tar.gz
tar -xzf Skype4Py-1.*
cd Skype4Py-1.0.32.0/
python setup.py install
vi ./build/lib.linux-i686-2.7/Skype4py/api/posix_dbus.py
(64位： vi ./build/lib.linux-x86_64-2.7/Skype4Py/api/posix_dbus.py)
replace os.execlp('skype') with os.execlp('skype', 'skype')
python setup.py install</pre>
<p><strong>2.create file skype_chat.py</strong></p>
<div>
<pre class="python" name="code">import os
import sys
import optparse
import Skype4Py

parser = optparse.OptionParser(version='%prog 1.0.0');
parser.add_option('--create-channel', help='create a new channel');
parser.add_option('--user', help='skype username');
parser.add_option('--disband-channel', help='disband a channel');
parser.add_option('--sendto-channel', help='select a channel');
parser.add_option('--message', help='send msg to channel');
parser.add_option('--from-channel', help='from channel');
parser.add_option('--kick', help='kicks member(s) from chat');

opts, args = parser.parse_args();
if args:
    parser.error('unexpected argument(s)'); 

skype = Skype4Py.Skype(Transport='x11');
channel_file = 'skype_channel'; 
def SkypeInit():
    if not skype.Client.IsRunning:
        skype.Client.Start();
    skype.Attach(); 
def GetChatName(channel):
    if not os.path.isfile(channel_file):
        print 'can not find channel file skype_channel, please create new channel.';
        exit();
    f = open(channel_file,'r');
    lines = f.readlines();
    chat_name = None;
    for line in lines:
        channel_list = line.split();
        channel_name = channel_list[0];
        if channel_name == channel:
            chat_name = channel_list[1];
            break;
    f.close();
    return chat_name; 
if opts.create_channel != None and opts.user != None:
    channel = opts.create_channel;
    user = opts.user;
    SkypeInit();
    chat = skype.CreateChatWith(user);
    f = open(channel_file,'a+');
    lines = f.readlines();

    for line in lines:
        cname = line.split();
        cname = cname[0];
        if cname == channel:
            f.close();
            print 'channel ', cname , ' has existed!';
            exit();    
    f.write(channel + ' ' + chat.Name + '\n');
    f.close();
    print 'channel ', channel, ' created successfull.';
elif opts.sendto_channel != None and opts.message != None:
    channel = opts.sendto_channel;
    msg = opts.message;
    chat_name = GetChatName(channel);
    if chat_name != None:
        SkypeInit();
        chat = skype.Chat(chat_name);
        chat.SendMessage(msg);
    else:
         print 'channel ', channel_name, ' not find!';
         exit();
elif opts.kick != None and opts.from_channel != None:
    channel = opts.from_channel;
    chat_name = GetChatName(channel);
    if chat_name != None:
        SkypeInit();
        chat = skype.Chat(chat_name);
        chat.Kick(opts.kick);
    else:
        print 'channel ', channel_name, ' not find!';</pre>
</div>
<p><strong>3.server socket:</strong></p>
<p>listen port 47777<br />
vi skype_socket.c</p>
<table class="confluenceTable tablesorter">
<thead>
<tr class="sortableHeader">
<th class="confluenceTh sortableHeader" data-column="0">
<div class="tablesorter-header-inner">skype_socket.c</div>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="confluenceTd">
<p><span style="color: rgb(0,128,0);">#include &lt;stdio.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;stdlib.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;string.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;signal.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;sys/types.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;sys/wait.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;unistd.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">#include &lt;netinet/in.h&gt;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">#define MAXLINE 1024</span></p>
<p><span style="color: rgb(0,128,0);">#define SERV_PORT 47777</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">int substr(char* dchr,char *schr,int begin,int len){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; int slen=0;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; int rc=0; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; if (begin&lt;=0) begin=1;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; slen=strlen(schr)-begin; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; if (slen&lt;=0||len==0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp; *dchr=NULL;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp; return rc;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; if (len&lt;0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; len=-len;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; if(len&gt;strlen(schr)) begin=1;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; else if((begin-len)&lt;=0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len=begin;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin=1;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; else {</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp; begin-=len;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp; begin++;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; begin&#8211;;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; schr+=begin;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; int i=0;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; for(i=0;i&lt;len&amp;&amp;*schr!=0;i++){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; *dchr++ = *schr++;&nbsp;&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; *dchr=0;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; rc=i;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; return rc;</span></p>
<p><span style="color: rgb(0,128,0);">}</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">int main(int argc,char *argv[]){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; pid_t pid;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; struct sockaddr_in servaddr,cliaddr;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; socklen_t cliaddr_len;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; int listenfd,connfd;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; char buf[MAXLINE];</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; char str[INET_ADDRSTRLEN];</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; int i,n,m;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; char delim[10] = &#8220;;;&#8221;; //default delim</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; int max_size = 800;&nbsp;&nbsp; &nbsp; //default max msg size</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; if(argc == 2 ){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; strcpy(delim, argv[1]);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; if(argc == 3){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; max_size = (int)argv[2];</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; listenfd = socket(AF_INET,SOCK_STREAM,0);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; if( listenfd &lt; 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; printf(&#8220;Create Socket Failed!&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; bzero(&amp;servaddr,sizeof(servaddr));</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; servaddr.sin_family = AF_INET;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; servaddr.sin_addr.s_addr = htonl(INADDR_ANY);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; servaddr.sin_port = htons(SERV_PORT);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; if(bind(listenfd,(struct sockaddr *)(&amp;servaddr),sizeof(servaddr)) == -1){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;&nbsp; perror(&#8220;bind Error&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;}</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; if(listen(listenfd,20) &lt; 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&#8220;listen Error&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;}</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; while(1){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;&nbsp; cliaddr_len = sizeof(struct sockaddr_in); &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connfd = accept(listenfd,(struct sockaddr *)(&amp;cliaddr),&amp;cliaddr_len);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;&nbsp; if(connfd &lt; 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&#8220;accept Error&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;&nbsp; m = fork();</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(m == -1){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&#8220;call to fork error&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(m == 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//sub process</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(1){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; n = read(connfd,buf,MAXLINE);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(n &lt; 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&#8220;read Error&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(n == 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //printf(&#8220;the other side has been closed.\n&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *p;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char channel[20], msg[max_size];</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; memset(channel, &#8216;\0&#8242;, sizeof(channel));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p = strstr(buf, delim);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; substr(channel, buf, 0, strlen(buf)-strlen(p));</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; substr(msg, p, 3, max_size);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //printf(&#8220;%s\n&#8221;, msg);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!strcasecmp(channel, &#8220;ls&#8221;)){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;system(&#8220;ls -lh /data/binary/ &#8220;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char cmdfile[10] = &#8220;./&#8221;;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char filename[] = &#8220;send.sh&#8221;;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcat(cmdfile, filename);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *arg[]={filename, channel, msg, NULL};</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( access(filename, F_OK) != 0 ) {</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //printf(&#8220;file %s not find.\n&#8221;, filename);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; //execute shell script</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(execvp(cmdfile, arg) &lt; 0){</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //printf(&#8220;execvp error&#8221;);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close(connfd);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else{</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close(connfd);</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="color: rgb(0,128,0);">}</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong> create &#8220;send.sh&#8221; : </strong></p>
<pre class="php">#!/bin/bash
channel=$1
msg=$2
python skype_chat.py --sendto-channel="$channel" --message="(*) $msg"</pre>
<p><strong>4. compile skype_socket.c:</strong></p>
<p>gcc -o skype_socket skype_socket.c</p>
<p>&nbsp;</p>
<p><strong>5.start socket and test send message by VNC:</strong></p>
<p>on server that skype installed run vncserver:</p>
<p>vncserver</p>
<p>enter password: skype</p>
<p>on your client computer run vnc clien and connect to server, you will open a window and a shell window, then run command:</p>
<p>skype &amp;</p>
<p>then login skype with ROBOT account, and then start socket:</p>
<pre class="php" name="code">./skype_socket &gt;/dev/null 2&gt;&amp;1 &amp;</pre>
<p>run &#8220;netstat -tlnp|grep skype&#8221;, you can find listen port 47777</p>
<p>create chat channel:</p>
<pre class="php" name="code">python skype_chat.py --create-channel="AAA" --user="your_skype_account"</pre>
<p><strong>6.on any server ,send message to chat chennel</strong>:</p>
<pre class="php" name="code">echo "AAA;;Hello everyone."|nc Server 47777</pre>
<p>引自：<br />
<a title="binary.chen.skype.robot" href="http://www.76ku.cn/articles/archives/252"><strong>http://www.76ku.cn/articles/archives/252</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/skype-robot-linux-2361/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>vps推荐之DigitalOcean</title>
		<link>https://www.fushanlang.com/vps-recommended-digitalocean-2349/</link>
		<comments>https://www.fushanlang.com/vps-recommended-digitalocean-2349/#comments</comments>
		<pubDate>Tue, 14 Jan 2014 16:36:42 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[colud]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[webserver]]></category>
		<category><![CDATA[虚拟主机]]></category>
		<category><![CDATA[digitalocean]]></category>
		<category><![CDATA[vps]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=2349</guid>
		<description><![CDATA[<p>作为一个爱折腾的网站”程序猿“，我用过多家vps，由于一般支持paypal 月付， 所以基本上都会用两三个月，不行就换另一家。</p> <p>1.Yard VPS</p> <p>台湾人开的，有中文支持，貌似也支持支付宝付款，偶尔会有7折，5折，甚至4折的优惠码，这个用过xen plan 768MB的一款vps，后来由于晚上的时候，网站速度慢，所以就没坚持继续使用，换了另外一家PhotonVPS ，后来才知道原来是一家的囧。</p> <p>2.BudgetVM</p> <p>这家也还好，openvz的vps 流量限制挺高都超过1T，3T的，xen plan 也很实惠，机房选的是洛杉矶，速度还可以当时用过时间最长的是xen vps plan 是10刀/月。</p> <p>3.Virpus</p> <p>找便宜性价比较高的vps是找到这个，当时用是9.5刀/月的 plan,流量1T，现在各大vps都在比流量限制，所以Virpus的流量限制也像BudgetVM一样 提高到3T了。</p> <p>4.SemoWeb</p> <p>是个老主机商（跟最近这两年雨后春笋般的新主机商比），卖的vps plan很便宜但是用过了才知道，一分钱一分货，如果手头紧，想拿vps练练手还是可以试用一下的。</p> <p>5.WeLoveServers</p> <p>这家vps打折打得很凶，在赵荣vps推荐里有很多打折信息，入手了一个openvz的，但是选的德州机房，做外贸站的可以考虑。</p> <p>6.Linode</p> <p>如果想省心再省心，但是别想着省钱，Linode绝对是最佳选择，尤其是为了适应新的vps提供商竞争，linode 升级了自己的vps plan之后。</p> <p>7.DigitalOcean</p> <p>lucky 7! 用过了这么多vps，终于等到了DigitalOcean，终于不想用再试了。目前已用半年了，操作模式跟普通VPS不太相同，用过Amazon Ec2的同学都会知道，它是按需收费的，它的直接竞争对手是伟大的amazon ec2.</p> <p>一个1G 内存30G SSD硬盘 双核CPU的 droplet（digitalocean 创建的节点称之为droplet 水滴）一个小时是0.7美分，你没看错就是这么便宜。</p> <p>又一次调试一个第三方功能，公司没测试主机了，我临时创建了一个droplet，搞了2个多小时测试完毕，账单时1.5美分，最有趣的是，DigitalOcean 竟然运行你的billing 余额是负数， 这也就意味着你可以先用再付钱， oh my god，就像水电煤气费一样，真的与前面几家预付费的vps很大不同。</p> <p>有兴趣的同学可以尝试一下。</p> <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/vps-recommended-digitalocean-2349/">vps推荐之DigitalOcean</a></span>]]></description>
				<content:encoded><![CDATA[<p>作为一个爱折腾的网站”程序猿“，我用过多家vps，由于一般支持paypal 月付， 所以基本上都会用两三个月，不行就换另一家。</p>
<p>1.<a href="http://www.yardvps.com/billing/aff.php?aff=2239" target="_blank">Yard VPS</a></p>
<p>台湾人开的，有中文支持，貌似也支持支付宝付款，偶尔会有7折，5折，甚至4折的优惠码，这个用过xen plan 768MB的一款vps，后来由于晚上的时候，网站速度慢，所以就没坚持继续使用，换了另外一家<a href="http://www.photonvps.com/billing/aff.php?aff=3656" target="_blank">PhotonVPS </a>，后来才知道原来是一家的囧。</p>
<p>2.<a href="https://www.budgetvm.com/account/aff.php?aff=251" target="_blank">BudgetVM</a></p>
<p>这家也还好，openvz的vps 流量限制挺高都超过1T，3T的，xen plan 也很实惠，机房选的是洛杉矶，速度还可以当时用过时间最长的是xen vps plan 是10刀/月。</p>
<p>3.<a href="http://myvirpus.com/aff.php?aff=1690" target="_blank">Virpus</a></p>
<p>找便宜性价比较高的vps是找到这个，当时用是9.5刀/月的 plan,流量1T，现在各大vps都在比流量限制，所以<a href="http://myvirpus.com/aff.php?aff=1690" target="_blank">Virpus</a>的流量限制也像<a href="https://www.budgetvm.com/account/aff.php?aff=251" target="_blank">BudgetVM</a>一样 提高到3T了。</p>
<p>4.<a href="http://www.semoweb.com/billing/aff.php?aff=389" target="_blank">SemoWeb</a></p>
<p>是个老主机商（跟最近这两年雨后春笋般的新主机商比），卖的vps plan很便宜但是用过了才知道，一分钱一分货，如果手头紧，想拿vps练练手还是可以试用一下的。</p>
<p>5.<a href="http://core.weloveservers.net/aff.php?aff=293" target="_blank">WeLoveServers</a></p>
<p>这家vps打折打得很凶，在赵荣vps推荐里有很多打折信息，入手了一个openvz的，但是选的德州机房，做外贸站的可以考虑。</p>
<p>6.<a href="https://www.linode.com/?r=45a535f84ed2429b6d9c867a8be9e79a3b4a4542" target="_blank">Linode</a></p>
<p>如果想省心再省心，但是别想着省钱，<a href="https://www.linode.com/?r=45a535f84ed2429b6d9c867a8be9e79a3b4a4542" target="_blank">Linode</a>绝对是最佳选择，尤其是为了适应新的vps提供商竞争，linode 升级了自己的vps plan之后。</p>
<p>7.<a href="https://www.digitalocean.com/?refcode=a55d9e682c3a" target="_blank">DigitalOcean</a></p>
<p>lucky 7! 用过了这么多vps，终于等到了<a href="https://www.digitalocean.com/?refcode=a55d9e682c3a" target="_blank">DigitalOcean</a>，终于不想用再试了。目前已用半年了，操作模式跟普通VPS不太相同，用过Amazon Ec2的同学都会知道，它是按需收费的，它的直接竞争对手是伟大的amazon ec2.</p>
<p>一个1G 内存30G SSD硬盘 双核CPU的 droplet（digitalocean 创建的节点称之为droplet 水滴）一个小时是0.7美分，你没看错就是这么便宜。</p>
<p>又一次调试一个第三方功能，公司没测试主机了，我临时创建了一个droplet，搞了2个多小时测试完毕，账单时1.5美分，最有趣的是，<a href="https://www.digitalocean.com/?refcode=a55d9e682c3a" target="_blank">DigitalOcean </a>竟然运行你的billing 余额是负数， 这也就意味着你可以先用再付钱， oh my god，就像水电煤气费一样，真的与前面几家预付费的vps很大不同。</p>
<p>有兴趣的同学可以尝试一下。</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/vps-recommended-digitalocean-2349/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>sysctl.conf网络内核参数说明(转)</title>
		<link>https://www.fushanlang.com/sysctl-conf-network-kernel-parameter-description-rpm-2340/</link>
		<comments>https://www.fushanlang.com/sysctl-conf-network-kernel-parameter-description-rpm-2340/#comments</comments>
		<pubDate>Fri, 13 Dec 2013 03:44:05 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[system]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=2340</guid>
		<description><![CDATA[<p>下面是我的理解，可能有误，仅供参考。</p> <p>要调优，三次/四次握手必须烂熟于心。</p> <p>client                  server (SYN_SENT)      —&#62;  (SYN_RECV) (ESTABLISHED)   &#60;— —&#62;  (ESTABLISHED)</p> <p>client(主动)            server (FIN_WAIT_1)    —&#62;    (CLOSE_WAIT) (FIN_WAIT_2)    &#60;— (TIME_WAIT)     &#60;—    (LAST_ACK) —&#62;    (CLOSED)</p> <p>大家熟知的 SYN flooding/SYN spoofing 就是在 SYN_RECV 的状态下发起的进攻。这种由于 TCP/IP 协议引起的缺陷只能防治而不好根治，除非换了 TCP/IP。通过下面的方式，可以在一定程度上缓解 DDOS 攻击。</p> 增大半连接的队列，即 backlog queue 人工干预以减少 SYS_RECV 的时间，可以降低第一个重传包的时间或者减少重传的次数 <p>检测 SYN 攻击，可以使用 netstat 命令查看当前的连接类型以及连接数目,如果发现有大量的 SYN_RECV,就值得怀疑了: $ netstat -tuna &#124; grep <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/sysctl-conf-network-kernel-parameter-description-rpm-2340/">sysctl.conf网络内核参数说明(转)</a></span>]]></description>
				<content:encoded><![CDATA[<p>下面是我的理解，可能有误，仅供参考。</p>
<p>要调优，三次/四次握手必须烂熟于心。</p>
<p>client                  server<br />
(SYN_SENT)      —&gt;  (SYN_RECV)<br />
(ESTABLISHED)   &lt;—<br />
—&gt;  (ESTABLISHED)</p>
<p>client(主动)            server<br />
(FIN_WAIT_1)    —&gt;    (CLOSE_WAIT)<br />
(FIN_WAIT_2)    &lt;—<br />
(TIME_WAIT)     &lt;—    (LAST_ACK)<br />
—&gt;    (CLOSED)</p>
<p>大家熟知的 SYN flooding/SYN spoofing 就是在 SYN_RECV 的状态下发起的进攻。这种由于 TCP/IP 协议引起的缺陷只能防治而不好根治，除非换了 TCP/IP。通过下面的方式，可以在一定程度上缓解 DDOS 攻击。</p>
<ul>
<li>增大半连接的队列，即 backlog queue</li>
<li>人工干预以减少 SYS_RECV 的时间，可以降低第一个重传包的时间或者减少重传的次数</li>
</ul>
<p>检测 SYN 攻击，可以使用 netstat 命令查看当前的连接类型以及连接数目,如果发现有大量的 SYN_RECV,就值得怀疑了:<br />
<code>$ netstat -tuna | grep :80 | awk '{print $6}' | sort | uniq -c</code></p>
<p>或者可以通过 wget/curl 从远端实际来测试一下访问的速度：<br />
<code>$ time wget -O /dev/null www.example.com</code><br />
正常情况下，其 real time 在个位数(s)左右，如果出现长达数十秒乃至几百秒的情况，有可能是此类情况。</p>
<p>最简单的方式是通过 syncookie 实现，Linux 还实现了一种称作 SYN cookie 的机制，开启：<br />
<code># echo 1 &gt; /proc/sys/net/ipv4/tcp_syncookies</code></p>
<p>该机制会在服务器收到 SYN 请求后，构造一个带有 ISN(initial sequence number)的 SYN/ACK 包，该 ISN 称为 cookie，其实就是一个哈希。通过此就可以验证客户端的真实性了<br />
<strong>注意：</strong>SYN cookie 机制不会使用到 backlog queue，因此不必担心 queue 被填满然后服务器主动放弃连接。</p>
<p>使用了 SYN cookie 之后，在 /var/log/kern.log 会发现不少如下的 log，起作用了 <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br />
possible SYN flooding on port 80. Sending cookies</p>
<p>除了使用 syncookie，还可以修改 backlog queue 来达到目的。backlog queue 是一个用来处理在三次握手过程中带有 SYN 标志的包的数据结构，可以用来控制系统同时处理的最大连接，当达到该阈值后，接下来的请求会被系统丢弃。这需要系统开辟额外的内存来处理进来的包。如果处理的不好会导致系统内存耗尽，导致严重的性能问题。</p>
<p>tcp_max_syn_backlog 定义了 backlog queue 的半连接数量：<br />
<code># echo 90000 &gt; /proc/sys/net/ipv4/tcp_max_syn_backlog</code></p>
<p>当客户端发起 SYN 请求后，服务端会立刻发送 SYN+ACK 的回应，该次半连接会到 backlog queue 中，服务器会等待客户返回 ACK，如果在一段时间内没有应答，服务器会重新发送刚刚的 SYN+ACK，经历了几次还是没有回应后，服务器会主动断开此次半连接。<br />
我们就可以修改重发的次数来减少整个半连接的时间：<br />
<code># echo 3 &gt; /proc/sys/net/ipv4/tcp_synack_retries</code></p>
<p>——————————————————————————-<br />
|Value|    Time of retransmission      |         Total time         |<br />
——————————————————————————-<br />
|1       | in 3rd second                          |            9 seconds      |<br />
——————————————————————————-<br />
|2       | in 3rd and 9th second            |            21 seconds   |<br />
——————————————————————————-<br />
|3       | in 3rd, 9th and 21st second  |            45 seconds    |<br />
——————————————————————————-<br />
这张表格显示了不同重传次数消耗的总时间</p>
<p>上面属于 passive 连接，也就是客户端连接服务端，还有个相反的 active TCP connection 参数：<br />
<code># echo 3 &gt; /proc/sys/net/ipv4/tcp_syn_retries</code></p>
<p>tcp_fin_timeout 参数会通知 kernel 在 FIN_WAIT_2 状态 sockets 的存活时间，根据理解应该是 server 主动终止，像下面这样。</p>
<p>server                        client<br />
(FIN_WAIT_1)    —&gt;    (CLOSE_WAIT)<br />
(FIN_WAIT_2)    &lt;—<br />
(TIME_WAIT)     &lt;—    (LAST_ACK)<br />
—&gt;    (CLOSED)</p>
<p>当处于 CLOST_WAIT 的 client 有意(攻击)/无意(client 突然崩溃等)不发 fin 来继续时，server 会一直停留在 FIN_WAIT_2 状态，造成资源的浪费。<br />
可以适当的减小该时间：<br />
<code># echo 15 &gt; /proc/sys/net/ipv4/tcp_fin_timeout</code></p>
<p>跟 tcp_fin_timeout 相关的有 tcp_max_orphans 参数，表示没有跟任何用户文件相关联的 socket 最大个数，超出的将被内核丢弃。<br />
建议该参数只增加不减小，但增加也意味着内存的消耗增加：<br />
<code># echo 327680 &gt; /proc/sys/net/ipv4/tcp_max_orphans</code></p>
<p>相关的 tcp_orphans_retries 关闭本端 TCP 连接前的重试次数，默认 7，高负载的 webserver 建议可以减小。这里解释了设置为 0 的情况。</p>
<p>下面这三个参数一起解释：<br />
tcp_tw_recycle<br />
tcp_tw_reuse<br />
tcp_timestamps</p>
<p>其中 tcp_tw_recycle/tcp_tw_reuse 这两个官方的建议保持默认为 0，而 tcp_timestamps 这个参数在特定的情况开启会引起很严重的问题(via <a href="http://serverfault.com/questions/235965/why-would-a-server-not-send-a-syn-ack-packet-in-response-to-a-syn-packet">1</a>, <a href="http://serverfault.com/questions/307376/many-different-tcp-timestamps-through-nat-device-cause-server-to-drop-packets-p">2</a>)。</p>
<p>基本的情况就是，你的客户或者你的服务器在一个 NAT 后面，如果开启这个参数，会导致服务器能收到三次握手的 SYN 但是不会返回任何的 SYN+ACK，其结果是客户无法访问你的网站。可以通过 tcpdump 或者下面的这个查看：<br />
<code># netstat -s | grep timestamp</code></p>
<p>tcp_timestamps 是 tcp 协议中的一个扩展项，通过时间戳的方式来检测过来的包以防止 PAWS(Protect Against Wrapped  Sequence numbers)，可以提高 tcp 的性能，2.6 的内核默认是打开的。只要 client/server/nat/loadbalancer 不同时打开该选项就不会出现上面的问题。与之相关的包括 tcp_tw_recycle，如果 tcp_timestamps 和 tcp_tw_recycle 同时开启，就会开启时间戳选项，导致上面的问题。如果有上述的网络结构，比较合理的方式是禁用 tcp_tw_recyle 而开启 tcp_timestamps。禁用了 tcp_tw_recycle 其 TIME_OUT sockets 回收功能就没了，可以配合 tcp_tw_reuse 让 TIME_WAIT 降下来。</p>
<p>netdev_max_backlog 这个参数跟 TCP 的传输队列有关,发送队列长度是 txqueuelen ，netdev_backlog 则决定接收队列的长度。<br />
前者通过 ifconfig 命令改变：<br />
<code># ifconfig eth0 txqueuelen 10000</code><br />
对于高吞吐的网络而言，默认的 100 肯定是不够的，一个 rrt 为 120ms 的千兆以太网络，可以设置成 10000 以上的值。</p>
<p>对于接受端而言，需要修改的话就涉及到了 /proc/sys/net/core/netdev_max_backlog 了，如果接收包的速度大于内核能处理的速度，则需要队列来维持，此数值表示最大队列值。默认为 1000，如果超过该数值，会引起丢包，根据实际情况增大。</p>
<p>对于网络不是很好的情况，可以开启 tcp_sack 参数。该实现是 TCP 的一个选项，称为 Selective Acknowlegement(SACK)，默认开启，对于千兆网络可以关闭，能提高一定的性能。</p>
<p>keepalive 的情况，Linux 内置支持，只需要开启相应的内核参数就可以了，主要是下面三个：<br />
tcp_keepalive_time 表示 TCP 发出第一个 keepalive 信息之前等待的时间，默认为 7200<br />
tcp_keepalive_intvl keepalive 的时间间隔，默认是 75<br />
tcp_keepalive_probes 触发的次数，默认是 9</p>
<p>ip_local_port_range 128M 内存以上的机器默认是 32768 61000，可以进一步扩大 10240 65535，尽量不要使用 1024 周围的，避免冲突。</p>
<p>以上只是网络内核参数的一小小部分，有待继续补充。</p>
<p>ref:</p>
<p>http://www.frozentux.net/ipsysctl-tutorial/chunkyhtml/tcpvariables.html<br />
http://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt<br />
http://www.symantec.com/connect/articles/hardening-tcpip-stack-syn-attacks<br />
http://www.saview.net/archives/201</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3 class="zemanta-related-title" style="margin: 0 0 10px 0; padding: 0; clear: both;">Related articles across the web</h3>
<ul class="zemanta-article-ul zemanta-article-ul-image" style="margin: 0; padding: 0; overflow: hidden;">
<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;"></li>
<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;"><a style="padding: 2px; display: block; text-decoration: none;" href="http://networking.answers.com/tcp-ip/tcp-and-ip-all-you-need-to-know-about-network-basics" target="_blank"><img style="border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;" alt="" src="http://i.zemanta.com/158573031_150_150.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://networking.answers.com/tcp-ip/tcp-and-ip-all-you-need-to-know-about-network-basics" target="_blank">TCP and IP: All You Need to Know About Network Basics</a></li>
<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;"><a style="padding: 2px; display: block; text-decoration: none;" href="http://www.c0t0d0s0.org/archives/7665-tcp_init_wnd_chk.html" target="_blank"><img style="border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;" alt="" src="http://i.zemanta.com/noimg_7_150_150.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://www.c0t0d0s0.org/archives/7665-tcp_init_wnd_chk.html" target="_blank">tcp_init_wnd_chk</a></li>
<li class="zemanta-article-ul-li-image zemanta-article-ul-li" style="padding: 0; background: none; list-style: none; display: block; float: left; vertical-align: top; text-align: left; width: 104px; font-size: 12px; margin: 0 5px 10px 0;"><a style="padding: 2px; display: block; text-decoration: none;" href="http://ranjan01.wordpress.com/2013/11/07/tuning-tcp-sockets-for-web-server/" target="_blank"><img style="border-radius: 3px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); padding: 0; margin: 0; border: 0; display: block; width: 100px; max-width: 100%;" alt="" src="http://i.zemanta.com/218557505_150_150.jpg" /></a><a style="display: block; overflow: hidden; text-decoration: none; line-height: 12pt; height: 80px; padding: 5px 2px 0 2px;" href="http://ranjan01.wordpress.com/2013/11/07/tuning-tcp-sockets-for-web-server/" target="_blank">Tuning TCP Sockets for web server</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/sysctl-conf-network-kernel-parameter-description-rpm-2340/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HAProxy &#8211; route by domain name</title>
		<link>https://www.fushanlang.com/haproxy-route-by-domain-name-2336/</link>
		<comments>https://www.fushanlang.com/haproxy-route-by-domain-name-2336/#comments</comments>
		<pubDate>Thu, 07 Nov 2013 01:30:07 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[新技术]]></category>
		<category><![CDATA[haproxy]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=2336</guid>
		<description><![CDATA[<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;by Sean Mcgary on September 28, 2013</p> <p>http://seanmcgary.com/posts/haproxy&#8212;route-by-domain-name</p> <p>&#160;</p> <p>I tend to build a lot of web applications in NodeJS using the Express.js webserver. When you have a few of these apps running on one server, you generally want to run them on unique ports and put some kind of proxy in front of <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/haproxy-route-by-domain-name-2336/">HAProxy &#8211; route by domain name</a></span>]]></description>
				<content:encoded><![CDATA[<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;by Sean Mcgary on September 28, 2013</p>
<p><a href="http://seanmcgary.com/posts/haproxy---route-by-domain-name">http://seanmcgary.com/posts/haproxy&#8212;route-by-domain-name</a></p>
<p>&nbsp;</p>
<p>I tend to build a lot of web applications in NodeJS using the Express.js webserver. When you have a few of these apps running on one server, you generally want to run them on unique ports and put some kind of proxy in front of them. Nginx works great for this and Apache can be another decent, though more bloated, alternative. Recently I decided to branch out for the sake of variety and to learn something new. HAProxy filled that role.</p>
<p>For the uninformed, HAProxy is more than just a reverse proxy; it&#8217;s a high performance load balancer. Sites with lots of traffic will use something like HAProxy to funnel traffic to a cluster of web servers or even balance taffic between database servers. But what happens when we want to route multiple domains (or subdomains) to different hosts or clusters?</p>
<p>Setting up a single proxy<br />
First lets take a look at the basics of proxying to an app server.</p>
<p># config for haproxy 1.5.x</p>
<p>global<br />
log 127.0.0.1 local0<br />
log 127.0.0.1 local1 notice<br />
maxconn 4096<br />
user haproxy<br />
group haproxy<br />
daemon</p>
<p>defaults<br />
log global<br />
mode http<br />
option httplog<br />
option dontlognull<br />
option forwardfor<br />
option http-server-close<br />
stats enable<br />
stats auth someuser:somepassword<br />
stats uri /haproxyStats</p>
<p>frontend http-in<br />
bind :80<br />
default_backend web-app-cluster</p>
<p>backend web-app-cluster<br />
balance leastconn<br />
option httpclose<br />
cookie JSESSIONID prefix<br />
server node1 10.0.0.1:8080 cookie A check<br />
server node2 10.0.0.2:8080 cookie A check<br />
server node3 10.0.0.3:8080 cookie A check<br />
So this is a pretty basic config that will loadbalance across 3 application servers, each of which is on a unqiue IP and probably on its own dedicated machine. Generally you&#8217;ll also want to run your load balancer(s) on a different server.</p>
<p>So what does this all mean? global and defaults should be pretty self-explanatory, then we have a frontend and abackend. The frontend, as you can see, tells HAProxy what to bind to and defines a default backend. There are a lot of things that can be specified in the front end and you can also have multiple frontend definitions (for example, if you wanted to provide an unsecure route running on port 80 and SSL on port 443 and have different, or the same, backends for each). We&#8217;ll go over some other options in the multiple domain example.</p>
<p>Diving into multiple domains and ACLs<br />
Now lets take a look at how to route to multiple domains based on matching specific domain names.</p>
<p># config for haproxy 1.5.x</p>
<p>global<br />
log 127.0.0.1 local0<br />
log 127.0.0.1 local1 notice<br />
maxconn 4096<br />
user haproxy<br />
group haproxy<br />
daemon</p>
<p>defaults<br />
log global<br />
mode http<br />
option httplog<br />
option dontlognull<br />
option forwardfor<br />
option http-server-close<br />
stats enable<br />
stats auth someuser:somepassword<br />
stats uri /haproxyStats</p>
<p>frontend http-in<br />
bind *:80</p>
<p># Define hosts<br />
acl host_bacon hdr(host) -i ilovebacon.com<br />
acl host_milkshakes hdr(host) -i bobsmilkshakes.com</p>
<p>## figure out which one to use<br />
use_backend bacon_cluster if host_bacon<br />
use_backend milshake_cluster if host_milkshakes</p>
<p>backend baconcluster<br />
balance leastconn<br />
option httpclose<br />
option forwardfor<br />
cookie JSESSIONID prefix<br />
server node1 10.0.0.1:8080 cookie A check<br />
server node1 10.0.0.2:8080 cookie A check<br />
server node1 10.0.0.3:8080 cookie A check</p>
<p>backend milshake_cluster<br />
balance leastconn<br />
option httpclose<br />
option forwardfor<br />
cookie JSESSIONID prefix<br />
server node1 10.0.0.4:8080 cookie A check<br />
server node1 10.0.0.5:8080 cookie A check<br />
server node1 10.0.0.6:8080 cookie A check<br />
So here we are routing between two applications; ilovebacon.com and bobsmilkshakes.com. Each one has its own cluster of app servers that we want to load balance between. Lets take a closer look at where all the magic happens in the frontend.</p>
<p>frontend http-in<br />
bind *:80</p>
<p># Define hosts<br />
acl host_bacon hdr(host) -i ilovebacon.com<br />
acl host_milkshakes hdr(host) -i bobsmilkshakes.com</p>
<p>## figure out which one to use<br />
use_backend bacon_cluster if host_bacon<br />
use_backend milshake_cluster if host_milkshakes<br />
If you&#8217;ve ever used nginx or Apache as reverse proxies, youd generally set things up using virtual hosts. HAProxy uses the notion of access control lists (acl) which can be used to direct traffic.</p>
<p>After we bind to port 80, we set up two acls. The hdr (short for header) checks the hostname header. We also specify -i to make sure its case insensitive, then provide the domain name that we want to match. You could also setup acls to match routes, file types, file names, etc. If you want to know more, feel free to check the docs. So now we effectively have two variables; host_bacon and host_milkshakes. Then we tell HAProxy what backend to use by checking to see if the variable is true or not. One other thing that we could do after the use_backend checks is specify a default_backend like we did in the first example to act as a catch-all to traffic hitting our load balancer that doesn&#8217;t match either of the domain names.</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/haproxy-route-by-domain-name-2336/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>convert mysql table engine myisam to innodb</title>
		<link>https://www.fushanlang.com/convert-mysql-table-engine-myisam-to-innodb-2331/</link>
		<comments>https://www.fushanlang.com/convert-mysql-table-engine-myisam-to-innodb-2331/#comments</comments>
		<pubDate>Sat, 26 Oct 2013 12:14:57 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=2331</guid>
		<description><![CDATA[ #!/bin/bash if [ $# -ne 1 ]; then echo "usage: convert.sh database" exit 1 fi date db=$1 echo "Convert database $db" ; mysql -B -N -e "SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = '$db' and engine='myisam'" &#124;awk '{print $1}'&#124; while read table; \ do \ echo "+ Converting Table $table"; \ mysql <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/convert-mysql-table-engine-myisam-to-innodb-2331/">convert mysql table engine myisam to innodb</a></span>]]></description>
				<content:encoded><![CDATA[<pre class="javascript" name="code">

#!/bin/bash
if [ $# -ne 1 ]; then
  echo "usage: convert.sh  database"
  exit 1
fi
date
db=$1
echo  "Convert database $db" ;
mysql -B -N -e "SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = '$db' and engine='myisam'"  |awk '{print $1}'| while read table; \
do \
     echo "+ Converting Table $table"; \
     mysql -B -N -e "alter table $table engine=innodb" $db; \
done;

</pre>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/convert-mysql-table-engine-myisam-to-innodb-2331/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
