gtalk自动检测bot0.01

来源:互联网 发布:java正则表达式( 编辑:程序博客网 时间:2024/05/29 13:04

原理:

因为google的 gtalk采用了xmpp协议,因此用开源的实现方案轻松无比,任何jabber的客户端经小改之

都可以与GTalk互通,此段代码用的条件

perl 5.6.0及以上

Net:XMPP

Net:Ping

#!/usr/bin/perl -w
use strict;
use Net::XMPP;
use Net::Ping;
###########################################
#use Net::Ping mod check the remote machie
#is alive or not!
#sub ping
############################################

sub ping{
open END, ">/tmp/bot/ping_end_gtalk.txt" or die "cann't open file: $!";
my @ip = qw{x.x.x..30
           x
.x.x.x.33
           x
.x.x.x.34
           x
.x.x.x.35
           x
.x.x.x.36
           x
.x.x.x..39
           };
my $host;
my $check = Net::Ping->new();
foreach $host(@ip){
 
if ($check->ping($host)) {
        
print END "$host is alive. ";
 }
 
else {
        
print END "WARNING!!! $host is unreachable. ";
 }
}
$check->close();
close END;
}
&ping;



##########################################
#gtalk send ping_file to x5ywsir
########################################

my $username = "ark119";
my $password = "xxxxxxxxx";

my $body;
my $to = "x5ywsir";
my $resource = "PerlBot";
## End of configuration

#------------------------------------

# Google Talk & Jabber parameters :


my $hostname = 'talk.google.com';
my $port = 5222;
my $componentname = 'gmail.com';
my $connectiontype = 'tcpip';
my $tls = 1;

#------------------------------------

my $Connection = new Net::XMPP::Client();

# Connect to talk.google.com
my $status = $Connection->Connect(
       hostname 
=> $hostname, port => $port,
       componentname 
=> $componentname,
       connectiontype 
=> $connectiontype, tls => $tls);

if (!(defined($status))) {
   
print "ERROR:  XMPP connection failed. ";
   
print "        ($!) ";
   
exit(0);
}

# Change hostname
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;

# Authenticate
my @result = $Connection->AuthSend(
       username 
=> $username, password => $password,
       resource 
=> $resource);

if ($result[0] ne "ok") {
   
print "ERROR: Authorization failed: $result[0] - $result[1] ";
   
exit(0);
}

# Change hostname
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;

# Authenticate
my @result = $Connection->AuthSend(
       username 
=> $username, password => $password,
       resource 
=> $resource);

if ($result[0] ne "ok") {
   
print "ERROR: Authorization failed: $result[0] - $result[1] ";
   
exit(0);
}

# Send message
open PING_FILE, "/tmp/bot/ping_end_gtalk.txt";
while (<PING_FILE>){
print $body=$_;
$Connection->MessageSend(
       to 
=> "$to@$componentname", body => $body,
       resource 
=> $resource);
sleep 5;
}
close PING_FILE;