8月 03

bash 产生随机数

  使用bash产生n位的随机数。测试中为产生8位。

#!/bin/bash
# -------------------------------
# Filename:    test.sh
# Revision:    1.0
# Date:        2013-08-01
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Description: 
# -------------------------------

function randnum()
{
    di=(0 1 2 3 4 5 6 7 8 9 \
        a b c d e f g h i j k l m n o p q r s t u v w x y z \
        A B C D E F G H I G K L M N O P Q R S T U V W X Y Z \
        ~ ! ^ _)
    for(( i=0; i<$1; i++))
    do
        num=$num`echo -n ${di[$RANDOM % ${#di[*]}]}`
    done
    echo $num
}

get=$(randnum 8)
echo $get

bash产生n位随机数

7月 03

在linux下用python读取其他操作系统编写的配置文件

  在windows、mac、linux下编写的配置文件会有头部或者换行有区别。为了分析方便简单做个记录,只为演示,代码并不完整,需要自己修改

def conf(conf_context):
    # 替换window或mac操作系统下的换行符
    import re
    _get_file = re.sub(r'(\r\n|\r|\n)', '\n', conf_context)
    # 将配置放在内存中
    import StringIO
    _tmp_file = StringIO.StringIO()
    # 将文件写入内存
    _tmp_file.write(_get_file)
    _tmp_file.seek(0)
    # 如果是在window下编辑的文件将,utf-8 BOM开始的头替换掉
    if _tmp_file.read(3).startswith('\xef\xbb\xbf'):
        _tmp_file.seek(3)
    else:
        _tmp_file.seek(0)
    # 导入配置
    import ConfigParser
    try:
        cf = ConfigParser.SafeConfigParser()
        cf.readfp(_tmp_file)
        config_list = cf.sections()
    except:
        _tmp_file.close()
        return "error"
    _tmp_file.close()
    return "ok"
3月 29

linux下python、go、C库处理图片缩放效率对比

  找到go语言的源码
https://github.com/nfnt/resize
http://code.google.com/p/gorilla/source/browse/lib/appengine/example/moustachio/resize/resize.go?r=3dbce6e267e9d497dffbce31220a059f02c4e99d

  使用’go get ‘安装需要使用git。如果是centos 6 直接安装’yum install git’。但是我的是CentOS 5 还要手动安装一下。
# yum install -y gcc make curl curl-devel zlib-devel openssl-devel perl perl-devel

cpio expat-devel gettext-devel
# wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
# tar zxvf git-latest.tar.gz
# cd git-2013-03-28/
# autoconf
# ./configure
# make && make install
# git –version

  git安装完成,开始测试。我的环境,python2.6,go1.0.2,ImageMagick是C/C++语言开发使用也比较广泛。直接用命令测试。。使用一张150k 510×382的图片做测试。缩成宽300的等比例缩小图。

# go get github.com/nfnt/resize

go 测试代码

package main

import (
    "github.com/nfnt/resize"
    "image/jpeg"
    "os"
)

func main() {
    // open "test.jpg"
    file, err := os.Open("test.jpg")
    if err != nil {
        print("Open File Error")
    }

    // decode jpeg into image.Image
    img, err := jpeg.Decode(file)
    if err != nil {
        print("Not image file")
    }
    file.Close()

    // resize to width 1000 using Lanczos resampling
    // and preserve aspect ratio
    m := resize.Resize(300, 0, img, resize.Lanczos3)

    out, err := os.Create("test_go.jpg")
    if err != nil {
        print("Save Image Error!")
    }
    defer out.Close()

    // write new image to file
    jpeg.Encode(out, m, nil)
}

python 测试代码

#!/bin/env python
# -*- coding:utf-8 -*-
# --------------------------------
# Filename:    cut_image.py
# Revision:    1.1
# Author:      simon-zzm
# Web:         www.simonzhang.net
# Email:       simon-zzm@163.com
# --------------------------------
import Image


def main():
    file = Image.open('test.jpg')
    w = file.size[0]
    h = file.size[1]
    re_data = file.resize((300, int(h/(float(w)/300))),)
    re_data.save('test_py.jpg', 'JPEG',)


if __name__ == '__main__':
    main()

  在linux下使用time进行测试结果
# time python cut_image.py

real 0m0.051s
user 0m0.040s
sys 0m0.009s

# time go run cut_image.go

real 0m2.736s
user 0m2.695s
sys 0m0.039s

# time convert -resize 300x test.jpg test_c.jpg

real 0m0.073s
user 0m0.070s
sys 0m0.002s

-rw-r–r– 1 root root 150332 Jul 16 2012 test.jpg
-rw-r–r– 1 root root 12929 Mar 28 23:13 test_go.jpg
-rw-r–r– 1 root root 13087 Mar 28 23:13 test_py.jpg
-rw-r–r– 1 root root 58591 Mar 28 23:14 test_c.jpg

总结:GO使用这个方法作为图片缩放的处理速度和python处理速度的差距太大了。烦请高手指点如何处理。不过GO的语法还是比较简单,值得学习。之前做的多语言简单累加计算测试,GO效率还是比较高,所以处理业务逻辑处理的效率还应该不错。
源码下载

3月 27

不明IP登陆linux服务器时邮件通知

  当有人登陆服务器时希望能知道是那个IP在登陆,如果是黑客登陆了也能及时知道。
  处理方法是,python时时检查linux下 secure 文件,如果是登陆成功的就发邮件。然后用shell脚本调用py启停,然后配置crontab定时检查py是否运行,如果检查进行不在则进行启动。
  python 脚本

#!/bin/python
#-*- coding:utf-8 -*-
# Filename:
# Revision:    1.0
# Date:        2013-3-26
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import re
import os
import time

import smtplib
from email.mime.text import MIMEText

#### 基础设置
mail_host = 'smtp.exmail.qq.com'
mail_user = 'warning'
mail_pwd = 'aaa'
mail_to = "simon-zzm@163.com"
mail_cc = "simon-zzm@"
secure_file = '/var/log/secure'
conn_fail_key = 'Failed password'
conn_access_key = 'Accepted password'
exclude_ip = ['192.168.100.8', \
              '192.168.100.10', \
              '192.168.100.11']
my_path = os.getcwd()


####
def mail_send(text):
    content = '%s' % text
    msg = MIMEText(content)
    msg['From'] = mail_user
    msg['Subject'] = 'access login server'
    msg['To'] = mail_to
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user, mail_pwd)
        s.sendmail(mail_user, [mail_to, mail_cc], msg.as_string())
        s.close()
    except Exception, e:
        print e

####
# get local host ip
####
def get_ip_address():
    ip = os.popen("/sbin/ifconfig | grep 'inet addr' | awk '{print $2}'").read()
    ip = ip[ip.find(':')+1:ip.find('\n')]
    return ip

#### 
def parse_secure(_data):
    # 获取IP地址
    re_ip = re.compile(r'''\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}''',re.DOTALL)
    try:
        get_ip = re_ip.findall(_data)[0]
    except:
        get_ip = ''
    #### 获得关键字位置
    try:
        key_index = _data.index(conn_access_key)
    except:
        key_index = 0
    #### 将正常登陆但不在已知范围的IP获取
    if (get_ip not in exclude_ip) and (get_ip != '') and (key_index > 0):
        return get_ip
    else:
        return ''


def main():
    #### 获取上次分析的最后一行
    try:
        last_line = open('%s/lastline.txt' % my_path, 'rb').readlines()[0][:-1]
    except:
        last_line = ''
    #### 打开安全日志,并记录最后一行
    get_secure = open(secure_file, 'rb').readlines()
    write_line = open('%s/lastline.txt' % my_path, 'wb')
    write_line.write(get_secure[-1])
    write_line.close()
    #### 获取没有处理的数据,如果最后一行为空着处理全部数据。
    if last_line != "":
        last_id = get_secure.index("%s\n" % last_line)
        get_secure = get_secure[last_id + 1:]
    #### 开始处理数据,只处理登陆成功和登陆失败部分数据
    access_login_list = ''
    for _get in get_secure:
        re_get_ip = parse_secure(_get)
        if re_get_ip != '':
            access_login_list += "%s " % re_get_ip
    #### 判断是否需要报警
    if len(access_login_list) > 1:
        mail_send("%s access login %s server" % (access_login_list, get_ip_address())) 


if __name__ == '__main__':
    while True:
        main()
        time.sleep(3)

  shell 脚本

#! /bin/bash
#
# make simon-zzm@163.com
#
#
### END INIT INFO

# Source function library.
. /etc/profile
cd `pwd`
key=monitoring_secure.py
# See how we were called.
case "$1" in
  stop)
     /bin/ps -ef|grep "${key}"|grep -v grep |awk ' ''{print $2}'|xargs kill -9
     ;;
  start)
      /usr/local/bin/python ${key} &
      ;;
  restart)
      stop
      sleep 1
      /usr/local/bin/python ${key} &
      ;;
  check)
      process_count=`/bin/ps -ef|grep "${key}"|grep -v grep|wc -l`
      case "${process_count}" in
          0)
            $0 start
            ;;
          1)
            ;;
          *)
           $0 stop
           sleep 1
           $0 start
           ;;
      esac
      ;;
  status)
      /bin/ps -ef|grep "${key}"|grep -v grep 
      ;;
  *)
        echo $"Usage: $0 {stop|start|restart|check|status}"
        exit 1
esac

exit

  crontab的配置,每8个小时检查一次。
0 */8 * * * /bin/sh /program/script/check_login_user/monitoring_secure.sh check >/dev/null 2>&1
代码下载