Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1864795
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类:

2006-02-26 15:25:22

OS: FC4
perl v5.8.6
本来想自己读写socket,但是总出问题,最后使用了Expect模块。
BTW: thanks to xiaosuo,我还是觉得直接用expect写的脚本看起来好些。
 
 
#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use Expect;
my @serv_ip; # server's ip address
my $serv_port;
my $user; # BBS ID
my $pass; # BBS passwd
my $buf;
sub usage()
{
        print "Usage: $0 \n";
}
sub my_exit($)
{
        print "$_[0]\n";
        exit;
}
sub check_opt()
{
        if(not defined($ARGV[0]))
        {
                usage();
        }
        elsif($ARGV[0] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/)
        {
                @serv_ip = split /\./, $ARGV[0];
                for(my $i = 0; $i < 4; $i ++)
                {
                        if($serv_ip[$i] < 0 || $serv_ip[$i] > 255)
                        {
                                my_exit("Illegal IP!");
                        }
                }
        }
        if(not defined($ARGV[1]))
        {
                $serv_port = 23;
        }
        elsif($ARGV[1] > 0 && $ARGV[1] < 65535)
        {
                $serv_port = $ARGV[1];
        }
        else
        {
                my_exit("Illegal Port!");
        }
}
sub get_name()
{
        $user = ;
        chomp($user);
}
sub get_pass()
{
        print "(echo-off)";
        `stty -echo`;
        $pass = ;
        `stty echo`;
        chomp($pass);
}
sub connect_server()
{
        my $timeout = 6;
        my $ok = 0;
        my $exp = Expect->spawn("telnet -8 $ARGV[0] $serv_port")
        or die "Cannot spawn telnet: $!\n";
        $exp->expect($timeout,
        [ -re=>'防止使用程式上站',
                sub
                {
                        my $exp = shift;
                        sleep 1;
                        $exp->send("\003");
                        exp_continue;
                }],
        [-re=>'请输入帐号',
                sub
                {
                        my $exp = shift;
                        get_name();
                        $exp->send("$user\r");
                        exp_continue;
                }],
        [ -re=>'请输入密码: ',
                sub
                {
                        my $exp = shift;
                        get_pass();
                        $exp->send("$pass\r");
                        exp_continue;
                }],
        [-re=>'无此',
                sub
                {
                        die "错误ID!请检查.";
                }],
        [-re=>'密码输入错误 (Password Error)',
                sub
                {
                        die "错误密码.";
                }],
        [ -re=>'您想删除重复的 login 吗',
                sub
                {
                        my $exp = shift;
                        $exp->send("N\n");
                        exp_continue;
                }],
        [ -re=>'按任何键继续',
                sub
                {       my $exp = shift;
                        $exp->send("\r");
                        exp_continue;
                }],
        [ -re=>'上次连线时间为',
                sub
                {
                        my $exp = shift;
                        $exp->send("\n");
                        exp_continue;
                }],
        [ -re=>'您要删除以上密码输入错误的记录吗',
                sub
                {
                        my $exp = shift;
                        $exp->send("\n");
                        exp_continue;
                }],
        [ -re=>'好朋友列表',
                sub
                {
                        my $exp = shift;
                        $exp->send("q");
                        exp_continue;
                }],
        );
        while(1)
        {
                $exp->expect(10,
                [ qr/\(\^Z回\)/i,
                        sub
                        {
                                my $exp = shift;
                                $exp->send(" Hello! I am a robot for fb2k BBS :-)\n");
                                exp_continue;
                        }],
                [ timeout => sub { $exp->send_slow(1,"e\r");}]
                );
        };
}

check_opt();
connect_server();
阅读(1245) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~