使用perl脚本发送gtalk消息

gtalk是使用xmpp协议的,因此可以很方便地利用现有的库来登陆gtalk发送消息。下面是参考了网上资料写的一个perl版本的

[pl]

#!/usr/bin/perl
use strict;
use warnings;
use Net::XMPP;
use utf8;
binmode(STDIN, ‘:encoding(utf8)’);
binmode(STDOUT, ‘:encoding(utf8)’);
binmode(STDERR, ‘:encoding(utf8)’);
my $username = "USERNAME"; #用户名,不带@的
my $password = "PASSWORD"; #纯文本密码
my $resource = "perl_script";

my $gtalkserver = "talk.google.com";
my $gtalkport = 5222;
my $componentname = "gmail.com";
my $connectiontype = "tcpip";
my $tls = 1;
my $connect = Net::XMPP::Client->new;
#my $connect = Net::XMPP::Client->new(debuglevel=>2,debugfile=>"stdout",debugtime=>1);

#如果想打印出登陆中的消息信息,可以打开debug模式,把debug信息输出的标注输出。
my $status = $connect->Connect(hostname => $gtalkserver,
port => $gtalkport,
componentname => $componentname,
connectiontype => $connectiontype,
tls => $tls
);
if ( !(defined($status))) {
print "xmpp connect failed! ,$!\n";
exit(1);
}
#change hostname

my $sid = $connect->{SESSION}->{id};
$connect->{STREAM}->{SIDS}->{$sid}->{hostname}=$componentname ;

#authcenticate

my @login =$connect->AuthSend(
username => $username,
password => $password,
resource => $resource );
if ( $login[0] ne "ok" ){
print "login failed: $login[0] -$login[1]\n";
exit(2);

} else {
print "login successful!\n";
}
my $to=’MSG_TO@gmail.com’;
print "input txt:";
while (my $line=<STDIN>) {
chomp $line;
last if ($line eq "quit" );
$connect->MessageSend(
to => "$to",
resource =>$resource,
suject => "nagios",
body => "$line"
);
print "input continue ..\n";
print "input txt:";
}
$connect->Disconnect;

[/pl]
如果登录失败的话,可以开启debug看看,一般是需要把IO::Socket::SSL升级一下才行。

此条目发表在System分类目录。将固定链接加入收藏夹。

发表评论