![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
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; |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Please learn to use code tags. These pages eat your indentation, otherwise (one presumes you actually started with some). Unformatted code is uglier than my first mother-in-law, and SHE was the only one in the hatchet fight without a hatchet.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
w/code tags
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;
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ]; /* Don't make me use it! */ |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
sorry i didnt know
...anyon can help me ? |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
File to run at system startup:
**** if not connectionGood open connection or die if not logfileOpen open log file or die while connectionGood and logfileOpen collect data ********
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programming Guru
![]() ![]() ![]() |
I'm not to good with Perl... but you could write a script and put it into cron to check the status of this app on a given time interval and have it start back up if its offline... just an idea.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
yeah but any code that may help please?
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
anyone?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|