Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 13th, 2005, 10: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
Old Jul 13th, 2005, 11:08 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 14th, 2005, 12:04 AM   #3
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
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! */
sykkn is offline   Reply With Quote
Old Jul 14th, 2005, 1:26 AM   #4
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
sorry i didnt know ...anyon can help me ?
sonson is offline   Reply With Quote
Old Jul 14th, 2005, 6:42 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 14th, 2005, 9:55 AM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,475
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jul 14th, 2005, 5:07 PM   #7
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
yeah but any code that may help please?
sonson is offline   Reply With Quote
Old Jul 16th, 2005, 8:45 PM   #8
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
anyone?
sonson is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC