文章分类 » code

backup:tcpdump计算每10秒的平均传输速度(含上下行)

#!/usr/bin/env python

import re,sys
import time
import os
import subprocess
import datetime
import signal

GET_LENGTH = re.compile(r'length (\d+):')
LOG = open('./tcp.log', 'a')
IP = '10.235.160.73' #针对指定站点
while True:
        total = 0
        start = time.time()
        tcpdump = subprocess.Popen('tcpdump -e -i eth0 -nn host %s' % IP, shell=True, stdout=subprocess.PIPE)
        for line in iter(tcpdump.stdout.readline, ''):
                rt = GET_LENGTH.findall(line)
                if len(rt):
                        total += int(rt[0])
                if time.time() - start >= 10:
                        os.kill(tcpdump.pid, signal.SIGKILL)
                        os.waitpid(-1, os.WNOHANG)
                        break

        log = '%s\t%s\n' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), total/(time.time() - start)/1024)
        print log,
        LOG.write(log)

backup:kill Pop-up ads for webpagetest client

webpagetest use the “pagetest.exe”, so wan can kill all “iexplore.exe” use the following WSH script 🙂

killIE.js

function killIE() {
    var WMIService = GetObject('winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2');
    var rt = WMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='iexplore.exe'");
    if (!rt.Count) {
        return;
    }
    var co = new Enumerator(rt);
    for(;!co.atEnd();co.moveNext()) {
        co.item().Terminate();
    }
}

while (true) {
    try{
        killIE();
    }catch(e){}
    WScript.Sleep(1000);
}

backup:install virtualbox and vnc server on readhat/centos 6

__WIKI__
==Install pkg from yum==

yum install make gcc gcc-c++ kernel-devel qt qt-x11 libXmu SDL libXt
wget "http://download.virtualbox.org/virtualbox/4.2.4/VirtualBox-4.2-4.2.4_81684_el6-1.x86_64.rpm"
rpm -ivh Virtual*.rpm
yum install wqy-zenhei-fonts.noarch gnome-terminal gnome-session tigervnc-server

==VNC Server Config==
vim /etc/sysconfig/vncservers

#connect vnc with host:5902
VNCSERVERS="2:root"
VNCSERVERARGS[2]="-geometry 800x600"

set password for vnc server

vncpasswd

use gnome for X system

vim ~/.vnc/xstartup

#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
gnome-session &
gnome-terminal &

sql备忘 :分组随机排序,并从分组中取n条数据

select * from (
    select *,
    @group_index := CASE WHEN @prev_group = province THEN @group_index + 1 ELSE 1 END AS group_index,
    @prev_group := province
    from (
        select * from sb_node order by province,rand()
    ) AS T1
) as T2 where group_index <=[n]

参考:http://stackoverflow.com/questions/2643314/mysql-group-by-limit

python产生GMT格式日期时间

#缓存一天
exp = datetime.timedelta(seconds=86400)
now = datetime.datetime.utcnow()
req.setHeader('Cache-Control', 'max-age=86400')
req.setHeader('Expires', (now + exp).strftime('%a, %d %b %Y %H:%M:%S GMT'))

———–
post by Email~