Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

CLIENT help!

by eanmrtn123 (Initiate)
on Jun 10, 2012 at 11:03 UTC ( #975413=perlquestion: print w/ replies, xml ) Need Help??
eanmrtn123 has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I am having trouble with a socket server and client interaction. I want to execute a perl script by default when a client TELNETs into my server. I tried this once, but it ran the script on the server's side, and not the client's side. Help?!

Here is the full code:

#! /usr/bin/perl -w # server0.pl #-------------------- #use strict; use Socket; # use port 7890 as default my $port = shift || 7890; 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\n"; # open CLIENT, ("system ("perl io-test-1.pl");"); # open (CLIENT, "system ("perl io-test-1.pl")"); # open CLIENT, "do `perl io-test-1.pl`"; #my $status = CLIENT, system("perl io-test-1.pl"); #CLIENT $status = CLIENT, system("perl io-test-1.pl"); close CLIENT; }

Lines 37 - 40 are my attempts at trying to run the file (in this case, called 'io-test-1.pl') to the client.

I want a default perl program to run when someone successfully TELNET's into the server.

If you want to see an example, TELNET into www.telehack.com. That is what I am trying to do, run a script when someone telnet's into the server.

Comment on CLIENT help!
Download Code
Re: CLIENT help!
by sauoq (Abbot) on Jun 10, 2012 at 12:01 UTC
    I tried this once

    Just once? Okay... Show us what you tried.

    -sauoq
    "My two cents aren't worth a dime.";
      I updated the question with the code.
Re: CLIENT help!
by Anonymous Monk on Jun 10, 2012 at 12:22 UTC

    Hello! I am having trouble with a socket server and client interaction. I want to execute a perl script by default when a client TELNETs into my server. I tried this once, but it ran the script on the server's side, and not the client's side. Help?!

    You mean to say, when somebody (a client) speak (opens socket) to you (your server), you want to make that somebody sing opera (run your program)?

    You can't do that without prior agreement (client install your software ), esp using telnet, since that is mind control (security violation)

      Indeed.

      Though perhaps what the questioner actually wants is to run the script at the server end, but display the script's output at the client end? (And is just not expressing that especially clearly.) That is, after all, a fairly common desire.

      In which case, it's something along these lines...

      my $output = `perl the-script.pl`; print CLIENT $output;
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        I tried it, and added the lines:

        my $output = `perl the-script.pl`; print CLIENT $output;

        But it just prints "smile from the server" and doesnt run the perl program. :-/

        Here is the total code:

        #! /usr/bin/perl -w # server0.pl #-------------------- #use strict; use Socket; # use port 7890 as default my $port = shift || 7890; 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\n"; # open CLIENT, ("system ("perl io-test-1.pl");"); # open (CLIENT, "system ("perl io-test-1.pl")"); # open CLIENT, "do `perl io-test-1.pl`"; #my $status = CLIENT, system("perl io-test-1.pl"); #CLIENT $status = CLIENT, system("perl io-test-1.pl"); my $output = `perl io-test-1.pl`; print CLIENT $output; close CLIENT; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://975413]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2014-07-30 21:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (240 votes), past polls