Thread: Recoonect...
View Single Post
Old Jul 13th, 2005, 9:33 PM   #1
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
Recoonect...

Well i have a full working script that connects to a port and writes logs to a file continuosly one problem with it tho is that when the machine breaks down for couple of hours or minutes it is ssupposed to connect back up to the server but it does not and i miss out on all the logs...
any one has a simple script for doing that...so that when it notice the connection breaks it connects automatically aagain...
thnks in advance...

use strict;
use Socket;

# initialize host and port
my $host = shift || '172.16.10.30';
my $hosts = "/etc/hosts/";

print STDERR "Enter hostname: ";
$host = <STDIN>;
chomp($host);

open (NUMBERS, "/etc/hosts/");

while ($hosts = <NUMBERS>) {
print $hosts;
}

close(NUMBERS);



my $port = shift || 5221;

my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);

# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET
, $paddr) or die "connect: $!";



my @t = localtime(time);

my $fileTimeStamp = ($t[5]+1900) . "-" . ($t[4]+1) . "-" . $t[3];

open(OUTPUT_FILE, ">>/data2/elsonba/zizo/" . $fileTimeStamp . ".log");

select OUTPUT_FILE; $| = 1;

my $line;

while (<SOCKET>)
{
my @ct = localtime(time()); # current localtime

my $currentTimeStamp = ($t[5]+1900) . "-" . ($t[4]+1) . "-" . $t[3];

if($fileTimeStamp ne $currentTimeStamp)

{
$fileTimeStamp = $currentTimeStamp; # set file stamp to current for use in open and later checks
close OUTPUT_FILE || warn($!);
open(OUTPUT_FILE,">>/data2/elsonba/zizo/" . $fileTimeStamp . ".OMlog") || die($!);
}

print OUTPUT_FILE "$_\n";
}
close SOCKET or die "close: $!";

close OUTPUT_FILE;
sonson is offline   Reply With Quote