如何让perl获取https网站链接以触发cron作业

时间:2018-06-05 07:55:40

标签: perl https cron

目前我有一个perl脚本可以在多个网站上触发cron作业,但该脚本仅适用于http而不适用于https。换句话说,当脚本尝试使用SSL证书触发域上的链接时,cron永远不会被触发。触发https cron URL需要什么?

#!/usr/bin/perl
#
# Enter domains, one per line.
#
# They do not have to be indented.
#
# Do not use an '#' on the beginning of your domains.
#
# Enter domains below next line: (without #)
my @domains = qw/#############################

    #www.example.com
    #www.example.com
    #www.example.com

###############_END_OF_DOMAINS_###############/;

my $num_domains = 200; # number of domains
my $cycle_time  = 900; # seconds per cycle

# So if $num_domain=200 and $cycle_time=900,
# there will be 4.5 seconds between sites.

# Nothing below here to service.....
#############################################################################
#############################################################################
#############################################################################
#############################################################################

use strict;
use warnings;
use IO::Socket;
use constant DEBUG => 0;

my $time_wait  = $cycle_time / $num_domains;

my @stdin = -t STDIN
    ? ()
    : grep !/^$/, map { s/^\s+//; s/\s+$//; s/\#.*?$//; $_ } <STDIN>
;

for my $site ( grep !/^\s*\#/, @domains, @stdin ) {
    chomp $site;

    print "contacting: $site\n" if DEBUG;
    my $sock = IO::Socket::INET->new(
             PeerHost   => $site,
             PeerPort   => 'http(80)',
             Proto      => 'tcp',
             Type       => SOCK_STREAM,
             Timeout    => 10,
    );

    unless ( defined $sock ) {
        warn "Couldn't connect to $site: $!\n" if DEBUG;
        next;
    }

    #Example target:
    # http://www.example.com/index.php?option=com_acymailing&ctrl=cron

    print $sock
        "GET /index.php?option=com_acymailing&ctrl=cron HTTP/1.0\n" .
        "Host: $site\n\n"
    ;

    # wait for response...
    while (defined( $_ = scalar <$sock> )) {
        # we dont care what it said...
        print 'recv: ', $_ if DEBUG;
    }
    print "\ndone\n" if DEBUG;
    $sock->close;

}
continue {
    print "sleeping $time_wait seconds\n" if DEBUG;
    select( undef, undef, undef, $time_wait );
}

__END__

1 个答案:

答案 0 :(得分:0)

问题似乎是因为您总是将套接字对象配置为使用HTTP端口(PeerPort => 'http(80)')。 HTTPS端口是443,而不是80。

但是我不知道你为什么要使用低级套接字编程来编写这样的代码。建议您查看LWP::Simple之类的内容(您还需要安装LWP::Protocol::https以获得HTTPS支持)。