Subversion Repositories configs

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
121 - 1
#!/usr/bin/perl -w
2
###################################################
3
##
4
##  DnsExit.Com Dynamic IP update script
5
##  v1.7
6
##
7
##################################################
8
BEGIN { my $PROGRAMDIR=`dirname $0`; chop $PROGRAMDIR; push @INC, $PROGRAMDIR; };
9
use strict;
10
use Http_get;
11
#
12
# Get config variables
13
#
14
my $cfile = "/etc/dnsexit.conf";
15
my %keyVal=();
16
open (CFG, "< $cfile") || (print STDERR "Fail open config file $cfile. You need to run dns-setup.pl script" && exit );
17
while (<CFG>)
18
{
19
  (my $line = $_ ) =~ s/\s+$//;
20
 
21
  if ( length( $line ) < 2 || ( $line =~ /^\s*#/ ) )
22
  {
23
    next;
24
  }
25
  my ($key, $value) = split(/=/, $line);
26
  $keyVal{$key} = $value;
27
}
28
my $ipfile   = $keyVal{"cachefile"} || '/tmp/dnsexit-ip.txt';
29
my $pidfile  = $keyVal{"pidfile"} || '/var/run/ipUpdate.pid';
30
my $daemon   = lc($keyVal{"daemon"}) || 'yes';
31
my $interval = $keyVal{"interval"} || 600;
32
my $logfile  = $keyVal{"logfile"} || '/var/log/dnsexit.log';
33
 
34
if ( ! ( $daemon eq "yes" ) )
35
{
36
  my $ip = getProxyIP();
37
 
38
  my $ipFlag = isIpChanged($ip);
39
  if ( $ipFlag == 1 )
40
  {
41
    mark("INFO", "100", "IP is not changed from last successful update");
42
    exit 0;
43
  }
44
  postNewIP( $ip );
45
}
46
else
47
{
48
  check_running();
49
  daemonize();
50
  while(1)
51
  {
52
    mark("INFO", "100", "Started in daemon mode");
53
    my $ip = getProxyIP();
54
    my $ipFlag = isIpChanged($ip);
55
    if ( $ipFlag == 1 )
56
    {
57
      mark("INFO", "100", "IP is not changed from last successful update");
58
    }
59
    else
60
    {
61
      postNewIP( $ip );
62
    }
63
    sleep( $interval );
64
  }
65
}
66
exit 0;
67
#-----------------------------------------------------
68
#-- Sub Routines
69
#-----------------------------------------------------
70
sub postNewIP
71
{
72
  my $newip = shift ( @_ );
73
  my $get = new Http_get;
74
  my $url = $keyVal{"url"};
75
  my $login = $keyVal{"login"};
76
  my $password = $keyVal{"password"};
77
  my $host = $keyVal{"host"};
78
  my $posturl = "${url}?login=${login}&password=${password}&host=${host}";
79
  if ( $newip =~ /\d+\.\d+\.\d+\.\d+/ )
80
  {
81
    $posturl = ${posturl} . "&myip=${newip}";
82
  }
83
 
84
  my $response = $get->request($posturl);
85
  if ($response->is_success)
86
  {
87
    #record successful update of the ip address
88
 
89
    my $result = $response->content;
90
    if ( $result =~ /(\d+)=(.+)/ )
91
    {
92
      mark("Success", "$1", "$2");
93
      open S, "> $ipfile";
94
      print S $newip;
95
      close S;
96
    }
97
    else
98
    {
99
      mark("ERROR", "-99", "Return content format error");
100
    }
101
 
102
  }
103
  else
104
  {
105
    mark("ERROR", $response->code, $response->message);
106
  }
107
 
108
}
109
 
110
sub isIpChanged
111
{
112
  my $newip = shift(@_);
113
  return 0 unless -e $ipfile;
114
  open SS, "< $ipfile";
115
  my $preip = <SS>;
116
  close SS;
117
  #print "new=[$newip] old=[$preip]";
118
  if (!($newip eq $preip))
119
  {
120
    return 0;
121
  }
122
  return 1;
123
}
124
sub getProxyIP
125
{
126
  my $get = new Http_get;
127
  my $ipServs = $keyVal{"proxyservs"};
128
  my @servs = split(/;/, $ipServs);
129
  foreach my $server ( @servs )
130
  {
131
    my $myUrl = "http://" . $server;
132
    my $response = $get->request($myUrl);
133
    if ($response->is_success)
134
    {
135
      if ( $response->content =~ /\D*(\d+\.\d+\.\d+\.\d+).*/ )
136
      {
137
    	mark("INFO", "100", "$myUrl says your IP address is: $1");
138
    	return ( $1 );
139
      }
140
      else
141
      {
142
    	mark("ERROR", "-100", "Return message format error.... Fail to grep the IP address from ".$myUrl);
143
      }
144
    }
145
  }
146
  mark("ERROR", "-99", "Fail to get the proxy IP of your machine");
147
  return "";
148
}
149
sub mark
150
{
151
  my ($type, $code, $message) = @_;
152
  open (LOGFILE, ">>$logfile");
153
  my $msg=localtime()."\t$type\t".$code."\t".$message."\n";
154
  print $msg;
155
  print LOGFILE $msg;
156
  close LOGFILE;
157
}
158
 
159
# Daemonize the process and write pid to pidfile
160
sub daemonize
161
{
162
  print "$0 started.\nLog file: $logfile\n";
163
  open (STDIN, '/dev/null')   or die "Can't read /dev/null: $!";
164
  open (STDOUT, '>>/dev/null') or die "Can't write to /dev/null: $!";
165
  open (STDERR, '>>/dev/null') or die "Can't write to /dev/null: $!";
166
  defined(my $pid = fork)   or die "Can't fork: $!";
167
  if ($pid )
168
  {
169
    open (PIDFILE, ">$pidfile");
170
    print PIDFILE $pid;
171
    close PIDFILE;
172
    exit;
173
  }
174
  umask 0;
175
}
176
 
177
sub check_running
178
{
179
  my $program = $0;
180
  my $running = `/bin/ps aux| grep $program | grep "/usr/bin/perl" | grep -v "grep $program" | grep -v " vim "`;
181
  #return 1 if(length($running) < 2);
182
  my (@line) = split("\n", $running);
183
  my $rcount = scalar (@line) - 1;
184
  if( $rcount > 0 )
185
  {
186
    print "Another instance of $program is running. \n..try to kill it........\n";
187
    my $lpid = `cat $pidfile`;
188
    system("kill -9 $lpid");
189
    sleep( 2 );
190
  }
191
}