Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Embedded net cat command not allowing variable port number?

JoeMcJoeMc Posts: 55Member
Hi All,

I've written a small perl module to send a local netcat command
message to a port listener and it works fine as long as I hard code
the port number. But It would make my life so much simplier if I
could make this command a bit more dynamic with a port varaible.

Here's the working command I'm currently using:
[code]system(qq{echo "$message" | nc $server_name 3018});[/code]

I tried the following command with no success:
[code]$port_num = 3020;
system(qq{echo "$message" | nc $server_name $port_num});[/code]

Is it because this is a perl module and not a Perl script?
Or am I just brain fried and nedd a rest?

Any help would be much appreciated..
Thanks in advance for any advice.

Regards,
JoeMc


Comments

  • JonathanJonathan Posts: 2,914Member
    : Here's the working command I'm currently using:
    : [code]: system(qq{echo "$message" | nc $server_name 3018});[/code]:
    :
    : I tried the following command with no success:
    : [code]: $port_num = 3020;
    : system(qq{echo "$message" | nc $server_name $port_num});[/code]
    Oddness...I can't actually see anything wrong here. Aside from you've used a different port number in the second example. Does it give an error of some kind?

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • JoeMcJoeMc Posts: 55Member
    : : Here's the working command I'm currently using:
    : : [code]: : system(qq{echo "$message" | nc $server_name 3018});[/code]: :
    : :
    : : I tried the following command with no success:
    : : [code]: : $port_num = 3020;
    : : system(qq{echo "$message" | nc $server_name $port_num});[/code]:
    : Oddness...I can't actually see anything wrong here. Aside from
    : you've used a different port number in the second example. Does it
    : give an error of some kind?
    :
    : Jonathan
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

    Actually, in testing the second one above with the variable port number I tested with 4 different port number of which 3018 was one. I just wrote one at random as an example. Call me crazy for asking this, but could this have to do with it being called from a module rather than a script?

    No error, just doesn't send message to port.
    Though it works using the first example. ?

    hmmmm...
    I'll try catching errors in an array and print to screen.

    Regards,
    JoeMc
  • JonathanJonathan Posts: 2,914Member
    : Call me crazy for asking this, but could this have to do with it being
    : called from a module rather than a script?
    I was about to say no, then thought of something: are you declaring the variable $port inside the module, or the script? Because if you are using "package" then they will be in different namespaces. You should pass $port along as a variable to the sub or method you are calling in the module. Hope this makes sense.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • JoeMcJoeMc Posts: 55Member
    : : Call me crazy for asking this, but could this have to do with it being
    : : called from a module rather than a script?
    : I was about to say no, then thought of something: are you declaring
    : the variable $port inside the module, or the script? Because if you
    : are using "package" then they will be in different namespaces. You
    : should pass $port along as a variable to the sub or method you are
    : calling in the module. Hope this makes sense.

    Actually, it's just a small module that just sends a message to a port.
    It is called by a cgi script which is called from a remote server.
    The snagg is, the port number is dependent on where the script is "pwd".
    I wouldn't mind sending the port number via the remote server call to the
    cgi script, but I don't always have access to the remote server. So, I'm
    trying to make the decission on my end on which port, allowing for future
    port changes.

    Example of how I'd like to get the port number inside the module:
    [code]use strict;

    my $pwd = $ENV{'PWD'};
    my $portnum = 0;

    if ( $pwd =~ /DEV/ ) {
    $portnum = 3018;
    } elsif ( $pwd =~ /QA/ ) {
    $portnum = 3019;
    } elsif ( $pwd =~ /UAT/ ) {
    $portnum = 3020;
    } elsif ( $pwd =~ /PRODUCTION/ ) {
    $portnum = 3021;
    }

    sub new {
    my ($class_name) = @_;
    my ($self) = {};
    bless ($self, $class_name);
    $self->{'_created'} = 1;
    return $self;
    }

    sub send_info {

    shift;
    my $message = shift;
    my $server_name = shift;

    chomp( $message );
    chomp( $server_name );

    system(qq{echo "$message" | nc $server_name $portnum});

    return 1;
    }
    1;[/code]

    Just trying to keep it simple and do a little forecasting on solving
    future potiential problems in maintance at the same time. :-)

    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • JonathanJonathan Posts: 2,914Member
    Two things...

    : my $pwd = $ENV{'PWD'};
    I don't think $ENV{'PWD'} is portable. Try print $ENV{'PWD'} to make sure you're getting something. Note that your if block will leave $portnum as 0 in the case it is empty; suggest an else clause that dies. See the getcwd module for a portable way.

    : system(qq{echo "$message" | nc $server_name $portnum});
    Stick a print statement in before here to see what it's actually trying to run.

    print qq{echo "$message" | nc $server_name $portnum};

    Any clues?

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • JoeMcJoeMc Posts: 55Member
    : Two things...
    :
    : : my $pwd = $ENV{'PWD'};
    : I don't think $ENV{'PWD'} is portable. Try print $ENV{'PWD'} to make
    : sure you're getting something. Note that your if block will leave
    : $portnum as 0 in the case it is empty; suggest an else clause that
    : dies. See the getcwd module for a portable way.
    :
    : : system(qq{echo "$message" | nc $server_name $portnum});
    : Stick a print statement in before here to see what it's actually
    : trying to run.
    :
    : print qq{echo "$message" | nc $server_name $portnum};
    :
    : Any clues?

    Son of a Gun!
    I figured it out, and I'm so embarassed... :-0
    In the conditions to set the port number depending on the
    pwd, the directories I was looking for turned out to be a softlink.
    problem solved when I re-adjusted the regular expression search pattern.
    Sorry to bug you with this, But Thanks for all your encouragement.
    By the way I did exchange getting the environment pwd with 'use Cwd'.
    Though both would work, may be deploying on a different flavor of Linux/Unix in the future. Good call. Thanks!

    JoeMc

    :
    : Jonathan
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • JonathanJonathan Posts: 2,914Member
    : By the way I did exchange getting the environment pwd with 'use Cwd'.
    : Though both would work, may be deploying on a different flavor of
    : Linux/Unix in the future. Good call. Thanks!
    Well, the bigger concern is non-UNIX/Linux platforms, such as Windows. Glad you're making it portable from the start so you don't have to worry later though. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
Sign In or Register to comment.