we need to have a mechanism at the remote host that can pick up the
requested connection on the specified port, and also handle the debugger interaction from the command line
You can use netcat but to reduce dependencies on external programs, and to give
you a better idea as to what is happening behind the scenes, you can use following program
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use IO::Socket;
use Term::ReadLine;
use constant BIGNUM => 65536;
our $previous_input;
# Set host and port.
my $host = shift || 'localhost';
my $port = shift || 12345; # over 1024 please
die("Usage: $0 hostname portno") unless ($host =~ /\w+/ && $port =~
^\d+$/);
print "listening on $host:$port\n";
my $term = new Term::ReadLine 'local prompter';
my $OUT;
{
# strict subs complains about STDOUT, so turn it off for the moment.
no strict 'subs';
$OUT = $term->OUT || STDOUT;
}
$OUT->autoflush(1);
# Open the socket the debugger will connect to.
my $sock = new IO::Socket::INET(
LocalHost => $host,
LocalPort => $port,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
my $new_sock = $sock->accept();
# Try to pick up the remote hostname for the prompt.
my $remote_host = gethostbyaddr($sock->sockaddr(), AF_INET) || 'remote';
my $prompt = "($remote_host)> ";
my ($buf, $input
# Read output from the debugger, then read debugger input.
while (1) {
# Drop out if the remote debugger went away.
exit 0 unless sysread($new_sock, $buf, BIGNUM);
print $OUT $buf;
# Drop out if we got end-of-file locally (warning: this
# causes the remote Perl to drop dead because the socket goes away).
exit 0 unless defined($input = $term->readline($prompt));
print { $new_sock } munge_input($input);
# Add the line to the terminal history.
$term->addhistory($input) if $input =~ /\S/;
}
# The debugger interaction can get all confused if the string it gets
# passed is just a null. We clean this up here.
sub munge_input {
my $actual_input = shift;
$actual_input = "\n" unless defined $actual_input;
Note also that whichever program you choose as the
listener, you will need to select a port number higher than 1024 unless you’re running as
root
(not a good idea) on that machine.you can debug perl program from remote machine using PERLDB_OPTS
PERLDB_OPTS="RemotePort=192.168.0.7:12345" perl helloworld
Hello World
process id(2798)
IP address:port number