最近有个网友问WANem怎么玩,如何自动设置WANem参数。
我看了一下,WANem2.3自己提供的Shell功能非常有限,那么只好通过发送Http请求的方法来设置了,趁中午休息时写了个脚本:
#/usr/bin/python
#coding: utf8
#-------------------------------------------------------------------------------
# Name: wanem
# Purpose:
#
# Author: rong
#
# Created: 29/07/2013
# Copyright: (c) rong 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
import httplib
import ConfigParser
import sys
import urllib
import re
class WANem(object):
def __init__(self):
self.sessionId = None
self.Ip = None
self.prop = None
self.origin = None
self.Port = "80"
self.cookie = None
self.conn = None
def loadConfigFile(self, ConfigFile):
self.prop = ConfigParser.ConfigParser()
self.prop.optionxform = str
self.prop.readfp(open(ConfigFile,"r"))
self.Ip = self.prop.get('head','Host')
self.origin = "http://"+self.Ip
pass
def getPostBody(self, section):
items = dict(self.prop.items(section))
path = items.pop('path')
refer = self.origin+items.pop('referer')
items = urllib.urlencode(items)
return (path, refer, items)
def sendConfig(self, config):
path, refer, postData = self.getPostBody(config)
headers = {}
headers['Referer'] = refer
headers['DNT'] = 1
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['Cache-Control'] = 'no-cache'
headers['Connection'] = 'Keep-Alive'
headers['User-Agent'] = 'Mozilla/4.0'
headers['Accept-Encoding'] = 'gzip, deflate'
headers['Cookie'] = self.cookie
headers['Accept'] = '*/*'
headers['Accept-Language']='zh-CN'
self.conn.request('POST', path, postData, headers)
response = self.conn.getresponse()
result = response.read()
#print result
return result
def startWan(self):
self.conn = httplib.HTTPConnection(self.Ip, self.Port)
path, refer, postData = self.getPostBody('start')
headers = {}
headers['Referer'] = refer
headers['DNT'] = 1
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['Cache-Control'] = 'no-cache'
headers['Connection'] = 'Keep-Alive'
headers['User-Agent'] = 'Mozilla/4.0'
headers['Accept-Encoding'] = 'gzip, deflate'
headers['Accept'] = '*/*'
headers['Accept-Language']='zh-CN'
self.conn.request('POST', path, postData, headers)
response = self.conn.getresponse()
result = response.read()
cookie = response.getheader('set-cookie')
self.cookie = re.search('PHPSESSID=(.*?);',cookie).group().replace(";", "")
if result.find("WANem is running"):
print "start wanem succesfully"
else:
print "start wanem failure"
def main(configFile):
#just for test
wanem = WANem()
wanem.loadConfigFile(configFile)
result, message = wanem.sendConfig()
if (result) :
print "success"
else:
print "error " + message
if __name__ == '__main__':
if(len(sys.argv) < 2):
print "please give a config name"
exit(1)
if (os.path.exists('config.ini') == False):
print "config file does not exist, please check!"
wanem = WANem()
wanem.loadConfigFile("config.ini")
wanem.startWan()
result = wanem.sendConfig(sys.argv[1])
print result
配置文件放在脚本同一目录下:
[head]
Host=192.168.189.140
[config1]
path=/WANem/index-advanced.php
referer=/WANem/index-advanced.php
txtLimit1=2000
selSym1=Yes
txtBandwidthAuto1=Other
txtBandwidth1=0
txtDelay1=1
txtLoss1=4
txtDup1=6
txtReorder1=0
txtCorrupt1=0
txtDelayJitter1=2
txtLossCorrelation1=5
txtDupCorrelation1=7
txtReorderCorrelation1=0
txtDelayCorrelation1=3
txtGap1=0
selDelayDistribution1=-N/A-
selidtyp1=none
txtidtmr1=
txtidsctmr1=
selrndtyp1=none
txtrndmttflo1=
txtrndmttfhi1=
txtrndmttrlo1=
txtrndmttrhi1=
selrcdtyp1=none
txtrcdmttflo1=
txtrcdmttfhi1=
txtrcdmttrlo1=
txtrcdmttrhi1=
txtSrc1=any
txtSrcSub1=
txtDest1=any
txtDestSub1=
txtPort1=any
btnApply=Apply settings
[confgi2]
#你可以复制上面的config1的内容到这里,然后调整你需要的参数
[start]
path=/WANem/index-advanced.php
referer=/WANem/start_advance.php
selInt=eth0
btnAdvanced=Start
[rest]
path=/WANem/index-advanced.php
referer=/WANem/index-advanced.php
txtLimit1=3230
selSym1=Yes
txtBandwidthAuto1=3228
txtBandwidth1=3228
txtDelay1=0
txtLoss1=0
txtDup1=0
txtReorder1=0
txtCorrupt1=0
txtDelayJitter1=0
txtLossCorrelation1=0
txtDupCorrelation1=0
txtReorderCorrelation1=0
txtDelayCorrelation1=0
txtGap1=0
selDelayDistribution1=-N/A-
selidtyp1=none
txtidtmr1=
txtidsctmr1=
selrndtyp1=none
txtrndmttflo1=
txtrndmttfhi1=
txtrndmttrlo1=
txtrndmttrhi1=
selrcdtyp1=none
txtrcdmttflo1=
txtrcdmttfhi1=
txtrcdmttrlo1=
txtrcdmttrhi1=
txtSrc1=any
txtSrcSub1=
txtDest1=any
txtDestSub1=
txtPort1=any
btnApply=Apply settings
缺点:
1. 参数名可能不是很容易看懂,直接抓包来的
2. 我的WANem只有一个网卡,假设使用场景是这样的。。。