0

I am trying to solve the following problem: Connect to a given ip address and port number using Netcat. Enter the password when prompted by "Enter password:". Then the remote machine echoes 2 lines of text (1 random). My task is to then send these messages back to the host before the time out and it terminates.

It seems like I should be able to do this with a simple script, which reads from the command line (local) and sends the data back those lines to the remote machine. I've tried with a perl script that looks like:

#!/bin/perl -w;

use strict;
use warnings;

my $PASSWORD = "password";

my $timeout = 20;
my $timenow = time;

while (time - $timenow < $timeout) { 
    while (my $input = <STDIN> ) {
        if ($input =~ "password") {
            print $PASSWORD . "\n";
        } else {
            print $input;
        }
    }
}

Then run:

nc <address> <port> | ./perlscript.pl

This gets as far as printing out the password to my terminal, but then fails. After some poking around it would appear that the password is not actually being sent to the remote machine as doing:

echo "PASSWORD" | nc <address> <port> | ./perlscript.pl

gets me past the password prompt, prints out the messages the remote machine sends, but again would seem they are not actually being sent back.

Does anyone have any ideas how I could achieve this goal? It seems that using some kind of mknode pipe thing might help with the I/O replaying back to the remote machine, but not sure how to do that?

It's also likely that this approach is fundamentally flawed, so open to any other suggestions of how to solve this?

Thanks,

Matt

2 Answers 2

2

Use Expect for interactive commands : http://search.cpan.org/~rgiersig/Expect-1.20/Expect.pod

use Expect;
my $command="nc";
my @params="ipaddress port";
my $exp = Expect->spawn($command, @params);
$exp->expect(10,'-re', "Enter password:");
$exp->send("your_password\n");
my $output = $expect->after() 
# you may need some more code based on the output from nc
1
  • Thanks for this, I'll keep this in mind for the future as it looks like a nice simple way to get around a lot of the headaches I was encountering in my original script.
    – Choc13
    Mar 5, 2013 at 11:33
1

You should use the perl sockets library to do what you need. Since your example was perl code, I thought I'd recommend this approach for you.

http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/

The examples at the URL I linked show UDP and TCP, while TCP examples should work for you. Redirecting stdin/stdout with netcat can be problematic with send/expect scenarios.

1
  • Thanks for the link. I agree that redirecting stdin/stdout with Netcat is a pain! In the end I went with the telnetlib in Python to do what I needed to do, but given that my question was in Perl, hence your answer related to Perl, I am accepting yours as the solution as it is the closest to what I went for in the end. Thanks.
    – Choc13
    Mar 5, 2013 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.