Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

socket programming

by bluntboy31 (Initiate)
on Mar 06, 2013 at 01:37 UTC ( #1021924=perlquestion: print w/ replies, xml ) Need Help??
bluntboy31 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to send shell command down the udp socket I have created between my client and server, have server execute the command and send back the result? Below is my client code:
! /usr/bin/perl -w # client 129.82.12.187(planetlab-1.cs.colostate.edu) use strict; use Socket; #port assignments use constant UDP_PORT => 5001; use constant REMOTE_HOST => '127.0.0.1';#this should be ip of server use constant MAX_RECV_LEN => 65536; #local variable declarations my $transport_service = getprotobyname('udp');# gets port assignment f +or the OS usually 17 my $remote_host = gethostbyname(REMOTE_HOST); my $remote_port = UDP_PORT; my $destination = sockaddr_in( $remote_port, $remote_host); #binding or creating the socket socket(UDP_SOCK, PF_INET, SOCK_DGRAM, $transport_service); $cmd = 'qx(ls)' print "$destination ,$cmd \n"; # = qx(ls); #send( UDP_SOCK, $cmd, 0, $destination ); #print "Uptime is: $info\n"; <code> server side: #! /usr/bin/perl -w # server 129.82.12.188(planetlab-2.cs.colostate.edu) use strict; use Socket; #port assignments use constant UDP_PORT => 5001; use constant MAX_RECV_LEN => 65535;#packet length in bytes use constant LOCAL_INETNAME => '127.0.0.1';#HOST SERVER RUNS ON #local variable declarations my $transport_service = getprotobyname('udp'); #my $local_host = gethostbyname(LOCAL_INETNAME); my $local_port = shift || UDP_PORT; my $local_address = sockaddr_in( $local_port, INADDR_ANY );#use list s +ince perl can be ued for other languages socket( UDP_SOCK, PF_INET, SOCK_DGRAM, $transport_service ); bind( UDP_SOCK, $local_address );#this creates the actual socket my $client_data;#this will contain what is sent to the server from cli +ent #this is the nested loop to wait for and save data from remote client while( 1 ) { my $from_who = recv( UDP_SOCK, $client_data, MAX_RECV_LEN, 0 ); if ( $from_who ) { my ( $the_port, $the_ip ) = sockaddr_in( $from_who ); my $remote_name = gethostbyaddr( $the_ip, AF_INET ) || inet_nt +oa( $the_ip ); warn "Received from $remote_name: ", length( $client_data ), ' -> ', substr( $client_data, 0, 39 ), "\n"; system($client_data); sleep(3); warn "Sending back to client ... \n"; send( UDP_SOCK, $client_data, 0, $from_who ) or warn "udp_s5: send to socket failed.\n"; } else { warn "Problem with recv: $!\n";# this indicates not getting da +ta from client } } # write the data contents to output file

Comment on socket programming
Download Code
Re: socket programming
by kielstirling (Scribe) on Mar 06, 2013 at 01:46 UTC
    I'll try and help if you re post it in a code tag. -Kiel
      I tried but I am not sure if that is any better

        You close code tags with </code>


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: socket programming
by kielstirling (Scribe) on Mar 06, 2013 at 03:27 UTC
    and your question is ?
    Update:
    So people stop voting me down ...

    Sorry I fail to understand what you would like help with. Please explain.
Re: socket programming
by VinsWorldcom (Priest) on Mar 06, 2013 at 11:49 UTC

    Maybe try changing the $cmd to:

    $cmd = `ls`;

    That is, use backticks. Or at least remove the single ticks from your qx() assignment.

Re: socket programming
by aitap (Deacon) on Mar 06, 2013 at 20:03 UTC

    $cmd = 'qx(ls)'
    The server does not eval this string, so you'll have to remove the "qx()" from the string. When you do this, server will receive the string "ls" and will run system("ls") which is probably what you want.

    print "$destination ,$cmd \n";
    You don't actually send anything to the server. Try sending your string or use IO::Socket::INET and print $socket $your_data.

    By the way, you may want to set the server-site socket to the non-blocking mode (for example), because right now you are trying to receive MAX_RECV_LEN (=65535) bytes while the client sends only two ("ls"). I'm not sure, but recv will probably block until it receives LENGTH data or the socket is closed (which is unlikely with UDP).

    And another thing: shouldn't it be better to run some already existing remote shell server? For example, using ssh you'll be able to encrypt your connection, protect your password and restrict the shell access, not leaving it open to any person with a netcat or similar program.

    Did I forget to mention that ssh is scriptable via Perl?

    Sorry if my advice was wrong.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-09-18 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (126 votes), past polls