由于项目需要简单的实现了Telnet协议.有兴趣的朋友可以看看
有兴趣的朋友可以自己看Telnet协议的文档,google上搜一下.就很多.
下面是公司的项目需要自己简单实现了一下,能够正常登陆,完全撇开了Net::Telnet
有兴趣的可以自己再扩展一下...
use strict;
use Socket qw(EFAULT :crlf);
use constant MAX_RECV_LEN => 65536;
use constant WILL => 251; #指示希望开始执行,或者确认现在正在操作指示的选项
use constant WONT => 252; #指出拒绝执行或继续招待所指示的选项
use constant DO => 253; #指出要求对方执行,或者确认希望对方执行指示的选项
use constant DONT => 254; #指出要求对方停止执行,或者确诊要求对方停止执行指示的选项
use constant IAC => 255; #数据字节 255
sub c2d
{
my ($buffer)=@_;
for(my $i=0;$i<length($buffer);$i++)
{
my $op=ord(substr($buffer,$i,1));
print $op." ";
}
}
my ($socket,$buffer,$port,$socket_addr,$host,$send);
my ($user,$pass)=("yiyang".CRLF,"yyyy".CRLF);
$host="10.184.14.198";
$port=23;
$socket_addr=sockaddr_in($port,inet_aton( $host ));
socket( $socket, PF_INET, SOCK_STREAM, getprotobyname( 'tcp' ) ) or die "error";
my $con_ok = connect( $socket, $socket_addr ) or warn "error";
unless ($con_ok) { print "连接失败";exit(0);}
select((select($socket), $|=1)[$[]);
sysread($socket,$buffer,MAX_RECV_LEN);print $buffer."\nRecv:";c2d($buffer);print "\n";
do{
$send="";
for(my $i=0;$i<length($buffer);$i++)
{
my $op=ord(substr($buffer,$i,1));
if($op==DO || $op==WILL){ $send.=chr(WONT); }
else { $send.=chr($op); }
}
print "Send:";c2d($send);print "\n";
syswrite($socket,$send,length($send),0) or warn "error";sleep(1);
sysread($socket,$buffer,MAX_RECV_LEN);print $buffer."\nRecv:";c2d($buffer);print "\n";
}while($buffer!~/login:/);
syswrite($socket,$user,length($user),0) or warn "error";sleep(1);
sysread($socket,$buffer,MAX_RECV_LEN);print $buffer;
syswrite($socket,$pass,length($pass),0) or warn "error";sleep(1);
sysread($socket,$buffer,MAX_RECV_LEN);print $buffer;