如何访问具有动态ip的linux服务器

通用的方法可以采用花生壳,如果觉得麻烦,不妨用下面这个脚本,然后用crontab让它每个10分钟执行一次,如果ip发生变化了,便向你指定的邮箱发送一封包含更新了的ip的邮件。

 

ipsender

----------------------------------------------

#!/usr/bin/perl

use LWP::Simple;
use strict;

my $pub_ip_file = '/etc/ipsender/mypubip';

my $old_pub_ip = `cat $pub_ip_file`;
chomp($old_pub_ip);

my $new_pub_ip =  get('http://whatismyip.org');
chomp($new_pub_ip);

if($new_pub_ip ne "" && $new_pub_ip ne $old_pub_ip)
{
    open(IPFILE, ">$pub_ip_file") || die "cannot open $pub_ip_file: $!";
    print IPFILE $new_pub_ip;
    close(IPFILE);

    my $sendmail = "/usr/sbin/sendmail -t";
    my $sendto = 'To: your@mailbox.com';
    my $subject = 'Subject: pubIP@HOME';

    open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
    print SENDMAIL $sendto."/n";
    print SENDMAIL $subject."/n";
    print SENDMAIL "Content-type: text/html/n/n";
    print SENDMAIL "Hi, the public ip at home is changed to $new_pub_ip.";
    close(SENDMAIL);

    system("logger /"public ip updated/"");
}
else
{
    system("logger /"public ip unchanged/"");
}

----------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值