CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
help with my first script!

 



zizo
Novice

May 23, 2005, 9:50 PM

Post #1 of 18 (1807 views)
help with my first script! Can't Post

Hi everyone iam new here and iam new to perl so please bare with meÖI found out this is a great language and iam still in the process of learning it.



I just wanted help with a script if anyone can help please. I want to write a script that would get onto a port and from there it can gather output data into it which is stored in there and is continuously running. It does this by making one file per day and hence it keeps creating a new file for each day while running the script. If connection is lost it need to be able to reconnect back into it by pinging it or some other way. The main problem iam having here is writing a script to actually Log onto the port itself. Would anyone be able to help me as this is the first thing they ask me do in my new job? Any help would b great. I heard I can get scripts off the net for free but donít know where exactly.

Thanks a lot.


davorg
Thaumaturge / Moderator

May 24, 2005, 2:58 AM

Post #2 of 18 (1804 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

Sure, there are plenty of people around here who will be happy to help you. Just post the code you have and explain the problems you are having.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

May 24, 2005, 1:55 PM

Post #3 of 18 (1797 views)
Re: [davorg] help with my first script! [In reply to] Can't Post

Thanks alot Sir. I just need help with the start of the program which proves to be the hardest bit of it and that is how do i write a script that would actually log onto the port itself...as iam wkring on big network systems...


davorg
Thaumaturge / Moderator

May 24, 2005, 2:07 PM

Post #4 of 18 (1795 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

Sounds like you should be looking at the IO::Socket::INET module.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

May 24, 2005, 2:12 PM

Post #5 of 18 (1794 views)
Re: [davorg] help with my first script! [In reply to] Can't Post

where can i refrenece that? any ideas..sorry iam still v new..is it hard to write this script?


davorg
Thaumaturge / Moderator

May 24, 2005, 2:26 PM

Post #6 of 18 (1793 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

IO::Socket::INET - it's a standard part of the Perl distribution.

But, to be honest, if you're really that new to Perl then I don't recommend that you start by trying to write a program that interacts with sockets.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

May 24, 2005, 2:38 PM

Post #7 of 18 (1792 views)
Re: [davorg] help with my first script! [In reply to] Can't Post

well its somethin that i have to do..do you recommend anyway of starting?? thnks for your help


davorg
Thaumaturge / Moderator

May 25, 2005, 5:01 AM

Post #8 of 18 (1784 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

I recommend working through "Learning Perl" and "Learning Perl Objects, References, and Modules".

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

May 25, 2005, 2:01 PM

Post #9 of 18 (1778 views)
Re: [davorg] help with my first script! [In reply to] Can't Post

ok i will show what i got...

for the server i got the following...

#! /usr/bin/perl -w
# server.pl
#--------------------

use strict;
use Socket;

# use port 7890 as default
my $port = shift || 8000;
my $proto = getprotobyname('tcp');

# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!";

# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);

# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port ";

# accepting a connection
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER))
{
# find out who connected
my ($client_port, $client_ip) = sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host = gethostbyaddr($client_ip, AF_INET);
# print who has connected
print "got a connection from: $client_host","[$client_ipnum] ";
# send them a message, close connection
print CLIENT "Smile from the server";
close CLIENT;
}
and for the client side i got

#! /usr/bin/perl -w
# client1.pl - a simple client
#----------------

use strict;
use Socket;

# initialize host and port
my $host = shift || 'localhost';
my $port = shift || 7890;

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)
a. or die "socket: $!";
connect(SOCKET, $paddr) or die "connect: $!";

my $line;
while ($line = )
{
print $line;
}
close SOCKET or die "close: $!";



does this make sense at all??? and when i do them do i have them on serpate files?? thks for your help..


davorg
Thaumaturge / Moderator

May 26, 2005, 6:57 AM

Post #10 of 18 (1773 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

Like I suggested before, I think you'll find it all gets a lot easier if you use IO::Socket::INET instead of Socket.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


zizo
Novice

May 26, 2005, 2:14 PM

Post #11 of 18 (1764 views)
Re: [davorg] help with my first script! [In reply to] Can't Post

any other suggestions as i have already started writing the script!


zizo
Novice

May 29, 2005, 2:23 PM

Post #12 of 18 (1750 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

anyone please :(


KevinR
Veteran


May 29, 2005, 3:16 PM

Post #13 of 18 (1748 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

sorry zizo, it's something I have no experience with otherwise I would try and help.
-------------------------------------------------


zizo
Novice

May 29, 2005, 3:42 PM

Post #14 of 18 (1747 views)
Re: [KevinR] help with my first script! [In reply to] Can't Post

ok would you be able to help with somethin..i need a somethin that would help me to actually
read from a file and checking the date for one day and till that date reaches 11:59pm then it
opens a new file for the next day starting 12:00am ..its kinda a log thing..so that
information gets send to these daily files...and once the file notices a date change
it rolls over and opens a new file to accept new info for the next day! any help pleasE?


zizo
Novice

May 30, 2005, 3:08 PM

Post #15 of 18 (1721 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

anyone there to help me please? i need to know how to redirect an output of a program to a file??


displeaser
Novice

May 31, 2005, 1:05 AM

Post #16 of 18 (1693 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

Hi,

heres a small function to log to a file and will change the filename every 24 hours.

sub loginfo{
($DAY, $MONTH, $YEAR) = (localtime)[3,4,5];
++$MONTH;
$YEAR+=1900;
my $newname="errors-"."$DAY-$MONTH-$YEAR."."log";
open(LOGFILE, ">> $newname");
my $mydate=localtime;
print LOGFILE "$mydate: $_[0]: $_[1]";
close LOGFILE;
}

it quite simple but should give you some idea for yourself on one way to do it.

it will create files such as:

error-30-5-2005.log

error-31-5-2005.log



it's called simply like

loginfo("Text to insert to the log");

Hope this helps.

Displeaser


displeaser
Novice

May 31, 2005, 1:08 AM

Post #17 of 18 (1692 views)
Re: [zizo] help with my first script! [In reply to] Can't Post

Hi again,

you want to redirect output of a program.

You can use the backticks for that.

an example:

use strict;
use warnings;

my $cmd="dir c:";

my $results=`$cmd`;

print "Output of command:\n$results\n";



Hope this helps.

Displeaser


zizo
Novice

May 31, 2005, 4:43 PM

Post #18 of 18 (1680 views)
Re: [displeaser] help with my first script! [In reply to] Can't Post

thnks but i got more issue if i wanan run the partially written script from initab how do you do that? thats in unix ofcourse!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives