作者:CppExplore 網(wǎng)址:http://www.shnenglu.com/CppExplore/
最近為了測(cè)試自己的程序,又避免和其它模塊聯(lián)調(diào),用perl寫了幾個(gè)陪測(cè)程序,拿來秀一下。工具就是工具而已,我無意于創(chuàng)造華麗的輪子,陪測(cè)工具夠用就好。這幾個(gè)陪測(cè)程序演示了udp/tcp server、udp/tcp client,希望對(duì)大家能有點(diǎn)幫助。
第一個(gè):radius server。近來的項(xiàng)目都是一期項(xiàng)目,radius只做認(rèn)證,因此陪測(cè)程序也只做了認(rèn)證,加accout也是很簡(jiǎn)單的事情。radius是個(gè)很簡(jiǎn)單的協(xié)議,簡(jiǎn)單描述下:client發(fā)送請(qǐng)求包的結(jié)構(gòu)如下:8位code表示請(qǐng)求類型,8位packetid表示包的順序號(hào),16位包長(zhǎng)度,128位標(biāo)識(shí),后邊跟屬性,8位屬性名,8位屬性長(zhǎng)度,后面接屬性value,后面繼續(xù)屬性。server端接收到以后,回復(fù)包結(jié)構(gòu):8位code表示回復(fù)類型,8位packetid,16位長(zhǎng)度,128位標(biāo)識(shí),后面屬性。注意這里的128位標(biāo)識(shí)不是client發(fā)來的,它是該包的code+packetid+client的標(biāo)識(shí)+該包后面屬性+公用密鑰后md5計(jì)算,取前128位的結(jié)果。
#!/usr/bin/perl
use Socket;
use Digest::MD5 qw(md5 md5_hex md5_base64);
print "radius server start
"."\n";
open(fp1,"./server_config");
$/="\r\n";
while($a=<fp1>)
{
if($a=~/port=/)
{
$a=~s/port=//;
chomp($port=$a);
print "port=".$port;
}
if($a=~/secret=/)
{
$a=~s/secret=//;
chomp($secret=$a);
print "secret=".$secret;
}
}
socket(sockmain,PF_INET,SOCK_DGRAM,0)||die "socket error:$!\n";
setsockopt(sockmain, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) ||die "setsockopt: $!";
bind(sockmain, sockaddr_in($port, INADDR_ANY)) ||die "bind: $!";
while(1)
{
$hispaddr=recv(sockmain,$buf,1024,0);
print "a client connect
"."\n";
my $requestTypeord=ord(substr($buf,0,1));
print '$requestTypeord='.$requestTypeord."\n";

my $packetId=ord(substr($buf,1,1));
print '$packetId='.$packetId;

my $packet_length=ord(substr($buf,2,1))*256+ord(substr($buf,3,1));
print '$packet_length='.$packet_length."\n";

my $identifier=substr($buf,4,16);

$location=20;
while($location<$packet_length)
{
$attribute=ord(substr($buf,$location,1));
print "\n".'$attribute='.$attribute."\n";

$attribute_length=ord(substr($buf,$location+1,1));
print '$attribute_length='.$attribute_length."\n";

$value=substr($buf,$location+2,$attribute_length-2);
print '$value='.$value."\n";
$location+=$attribute_length;
}
$data=chr(2).chr($packetId).chr(0).chr(20).$identifier.$secret;
$data=md5($data);

$response=chr(2).chr($packetId).chr(0).chr(20).substr($data,0,16);

send(sockmain,$response,0,$hispaddr);
print "send access sucess!"."\n";
}
print "close
."."\n";
close ($main_sock);
其中,./server_config是配置文件,里面配置了兩項(xiàng),端口和公用密鑰。
port=6011
secret=12345678
第二個(gè):http sever。我們部門的項(xiàng)目對(duì)各個(gè)網(wǎng)元server的管理命令采用http協(xié)議,也就有了這個(gè)和后面的http client陪測(cè)程序:client發(fā)送參數(shù)使用post方式,server回送的數(shù)據(jù)內(nèi)容采用xml。
#!/usr/bin/perl
use Socket;
$SIG{CHLD} ="IGNORE";
print "http server start
\n";
$\="\r\n";
my %map;
open(fp1,"./server_config");
$/="\r\n";
while($a=<fp1>)
{
if($a=~/cmd=/)
{
$a=~s/cmd=//;
chomp($cmd=$a);
$value="";
while($b=<fp1>)
{
$b=~s/{//;
$b=~s/\s*(\w*)\s*/$1/;
chomp($b);
if($b=~/}/)
{
$b=~s/}//;
$value.=$b;
last;
}
$value.=$b;
}
$map{$cmd}=$value;
print $cmd."=".$value."\n";
}
if($a=~/port=/)
{
$a=~s/port=//;
chomp($port=$a);
print "port=".$port;
}
}
socket(sockmain,PF_INET,SOCK_STREAM,0)||die "socket error:$!\n";
setsockopt(sockmain, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) ||die "setsockopt: $!";
bind(sockmain, sockaddr_in($port, INADDR_ANY)) ||die "bind: $!";
listen(sockmain,5)|| die "listen: $!";

while (accept($new_sock,sockmain) )
{
$pid = fork();
die "cann't fork " unless defined($pid);
if ($pid == 0)
{
print ">>>>>>>>>>>>>>>>\n";

recv($new_sock,$buf,1024,0);
print "$buf";

$len=index($buf,"POST",0);
$len2=index($buf,' HTTP/1.1',$len);
$new_cmd=substr($buf,$len+6,$len2-$len-6);
print $new_cmd;

$msg="<?xml version=\"1.0\" encoding=\"UTF-8\"?><response command=\"".
$new_cmd."\">".
$map{$new_cmd}.
"</response>";
$result="HTTP/1.1 200 OK\r\n".
"Content-Length: ".length($msg)."\r\n".
"Content-Type: text/xml; charset=UTF-8\r\n".
"\r\n".$msg;
send($new_sock,$result,0);
print "<<<<<<<<<<<<<<<<<\n";
print $result."\n";
exit(0);
}
}
print "close
.\n";
close ($main_sock);
其中./server_config是配置文件,里面配置了端口,需要響應(yīng)的命令以及響應(yīng)的內(nèi)容。
port=9120
cmd=DeviceRegister
{
<result code="0">OK</result>
}
第三個(gè):http client。再copy代碼就太羅唆了,呵呵。還是老樣子,先讀取配置文件,拿到要連接的ip:port,要發(fā)送的命令,post的參數(shù),然后構(gòu)造http信令,connect到server,send就好了,之后再recv下server的響應(yīng)。
第四個(gè):rtsp client。思路也在上一篇文章里說了,不再羅唆了。這個(gè)和前幾個(gè)不同的是,這個(gè)陪測(cè)腳本是我上個(gè)項(xiàng)目主要的業(yè)務(wù)測(cè)試腳本,是業(yè)務(wù)的發(fā)起者。因此(1)配置文件名作為參數(shù)傳遞進(jìn)去 (2)配置文件可以配置各種信令的以及各種發(fā)送流程(3)使用一個(gè)新的腳本調(diào)用大量使用不同配置文件的如此腳本,用來做穩(wěn)定性壓力測(cè)試