1月 04

centos下搭建yum服务器自动同步源

  在内网中建立yum服务器,内网linux服务器可以使用yum升级。这样做有两个好处,一内网服务器不用登陆公网比较安全,二节约了流量,不用每台服务器更新都连到公网上。
  安装配置比较简单。需要nginx或apache,本次只列出了nginx配置。还需要rsync工具,建议使用命令“yum install -y rsync”。
  yum服务器内网地址192.168.100.100

Nginx配置

#######################################yum server
server {
        # 直接使用IP地址
        server_name 192.168.100.100;
        # 开启自动显示目录
        autoindex on;
        # 显示文件大小
        autoindex_exact_size off;
        # 显示服务器本地时间
        autoindex_localtime on;
        # yum放置目录
        root    /u01/yumserver/;
        # 内网可见
        allow 192.168.100.0/24;
        # 除上列ip均不能查看
        deny all;
        # 限制ip后会报403错误,做个跳转
        error_page 400 403 404 500 502 503 504 = http://www.simonzhang.net;
        }

同步脚本,将脚本有可执行权限,将此设置到crontab即可。

#!/bin/bash
# -------------------------------
# Revision: 
# Date:        2012-12-11
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Web:         www.simonzhang.net
# ------------------------------- 

# base value
# 要同步的源
YUM_SITE="rsync://mirrors.kernel.org/centos/"
# 本地存放目录
LOCAL_PATH="/u01/mirrors/centos/"
# 需要同步的版本,我只需要5和6版本的
LOCAL_VER="5 5* 6 6*"
# 同步时要限制的带宽
BW_limit=512
# 记录本脚本进程号
LOCK_FILE="/var/log/yum_server.pid"
# 如用系统默认rsync工具为空即可。
# 如用自己安装的rsync工具直接填写完整路径
RSYNC_PATH=""

# check update yum server  pid
MY_PID=$$
if [ -f $LOCK_FILE ]; then
    get_pid=`/bin/cat $LOCK_FILE`
    get_system_pid=`/bin/ps -ef|grep -v grep|grep $get_pid|wc -l`
    if [ $get_system_pid -eq 0] ; then
        echo $MY_PID>$LOCK_FILE
    else
        echo "Have update yum server now!"
        exit 1
    fi
else
    echo $MY_PID>$LOCK_FILE
fi

# check rsync tool
if [ -z $RSYNC_PATH ]; then
    RSYNC_PATH=`/usr/bin/whereis rsync|awk ' ''{print $2}'`
    if [ -z $RSYNC_PATH ]; then
        echo 'Not find rsync tool.'
        echo 'use comm: yum install -y rsync'
    fi
fi

# sync yum source
for VER in $LOCAL_VER;
do 
    # Check whether there are local directory
    if [ ! -d "$LOCAL_PATH$VER" ] ; then
        echo "Create dir $LOCAL_PATH$VER"
        `/bin/mkdir -p $LOCAL_PATH$VER`
    fi
    # sync yum source
    echo "Start sync $LOCAL_PATH$VER"
    $RSYNC_PATH -avrtH --delete --bwlimit=$BW_limit --exclude "isos" $YUM_SITE$VER $LOCAL_PATH$VER
done

# clean lock file
`/bin/rm -rf $LOCK_FILE`

echo 'sync end.'
exit 1

centos_yum_server

客户端配置
编辑/etc/yum.repos.d/CentOS-Base.repo
#base
[base]
name=CentOS-$releasever – Base
baseurl=http://192.168.100.100/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-$releasever – Updates
baseurl=http://192.168.100.100/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful
[extras]
name=CentOS-$releasever – Extras
baseurl=http://192.168.100.100/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever – Plus
baseurl=http://192.168.100.100/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

#contrib – packages by Centos Users
[contrib]
name=CentOS-$releasever – Contrib
baseurl=http://192.168.100.100/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6

5月 22

python 访问接口获得 WSDL 数据

  需求访问http://192.168.1.100:8080/Service?wsdl,获取统计数字,接口“Count”参数有“user:string,pwd:string”。
  WSDL是Web Service的描述语言,是一种接口定义语言,用于描述Web Service的接口信息等。
  首先,安装SOAPpy
easy_install SOAPpy
代码如下:

def get_service():
    _url = "http://192.168.1.100:8080/Service?wsdl"
    _user = "test"
    _pwd = "test"
    try:
        server = SOAPpy.SOAPProxy(_url)
        get_result = server.Count(_user, _pwd)
    except:
        get_result = "Error!"
    return "%s" % get_result
4月 07

android的monkey测试简单记录

  需要对android的一个软软件做个疲劳测试,使用android SDK自带的工具最为方便。所就开始做monkey测试,调用和回播的脚本是从网上搜集而来,自己做了小修改。原始脚本的网页url忘记记录,所以不能提供参考网连接。下面开始对我所做的简单测试做个记录。
  首先是在系统上安装java运行环境,这个比较简单。下载android SDK包,直接解压后就可以使用非常方便。
  进入android-sdk-windows/tools目录,在其下面写两个脚本。
录脚本的启动脚本名monkey_recorder.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder

device = mr.waitForConnection()
recorder.start(device)

回放脚本名为monkey_playback.py脚本内容如下:

#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from com.android.monkeyrunner import MonkeyRunner

# The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command.  The line is split into 2
# parts with a | character.  Text to the left of the pipe denotes
# which command to run.  The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. 

# Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
    'TOUCH': lambda dev, arg: dev.touch(**arg),
    'DRAG': lambda dev, arg: dev.drag(**arg),
    'PRESS': lambda dev, arg: dev.press(**arg),
    'TYPE': lambda dev, arg: dev.type(**arg),
    'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
    }

# Process a single file for the specified device.
def process_file(fp, device):
    for line in fp:
        (cmd, rest) = line.split('|')
        try:
            # Parse the pydict
            rest = eval(rest)
        except:
            print 'unable to parse options'
            continue

        if cmd not in CMD_MAP:
            print 'unknown command: ' + cmd
            continue
        CMD_MAP[cmd](device, rest)
        
def go(file,num):
    n = num
    device = MonkeyRunner.waitForConnection()
    for i in range(int(n)):
        print 'now %s'%i
        fp = open(file, 'r')
        process_file(fp, device)
        fp.close();

def main():
    file = sys.argv[1]
    try :
        number = sys.argv[2]
    except :
        number = 1
    go(file,number)
 
if __name__ == '__main__':
    main()

  到此环境和脚本就算部署完了。下面开始记录脚本使用。
  首先将手机和电脑用usb线连好,使用“cmd”调出命令行,然后进入“android-sdk-windows/tools”目录。输入命令:
monkeyrunner.bat monkey_recorder.py
如果没有报错,并且出现以下界面,就可以开始录制脚本了。

  “wait”为每步操作间的等待时间。“Press a Button“发送,MENU,HOME,or SEARCH 按钮。“Type Something”为输入文字。“Fling”为模拟滑动操作。“Export Actions”为保存刚才录制的脚本。最后一个为刷新。   
  保存的脚本可以直接用文本编辑器手动编辑。假如我们录制的脚本为text.txt。下一步就是回放脚本,使用命令为:
monkeyrunner.bat monkey_playback.py test.txt 100
  最后的数字是将这个脚本循环100次。
  我只需要到此即可了。如果需要抓图片\照相\判断等高级功能,将来需要时再学习。