Search code examples
perlopenssh

Redirect debugging output in OpenSSH


I'm working on a program that runs interactive commands via OpenSSH and I can't figure out how I'm going to redirect the debugging output to debug.txt.

This is the code:

#!/usr/bin/perl

use strict;                                                 
use Net::OpenSSH;

my ($u_name, $acc_name, $acc_uid);

print "Enter account ID: ";
$acc_uid = <STDIN>;
chomp ($acc_uid);

print "Login as: ";
$u_name = <STDIN>;
chomp ($u_name);

my $createuser = "sudo useradd $acc_uid";

system ("clear");

BEGIN { 
    open (my $out, '>>', '/usr/local/debug.txt') or warn $!;
    $Net::OpenSSH::debug_fh = $out;
    $Net::OpenSSH::debug = 16;
}  

my $ssh = Net::OpenSSH->new( "$u_name\@myserver", strict_mode => 0);

$ssh->system({tty => 1 }, "$createuser") 
                or die "remote command failed: " . $ssh->error;         

I found the code for Net::OpenSSH::debug in Net::OpenSSH and this is the output of the program:

Enter account ID: usertest
Login as: testuser
# open_ex: ['ssh','-V']
# _waitpid(711) => pid: 711, rc:
testuser@myserver's password:
# open_ex: ['ssh','-O','check','-T','-S','/root/.libnet-openssh-perl/testuser-myserver-344-438107','-l','testuser','myserver','--']
# _waitpid(349) => pid: 349, rc:
# open_ex: ['ssh','-qtt','-S','/root/.libnet-openssh-perl/testuser-myserver-344-438107','-l','testuser','myserver','--','sudo useradd testuser123']
[sudo] password for testuser:
# _waitpid(350) => pid: 350, rc:
# open_ex: ['ssh','-O','exit','-T','-S','/root/.libnet-openssh-perl/testuser-myserver-344-438107','-l','testuser','myserver','--']
# _waitpid(351) => pid: 351, rc:

And when I checked debug.txt, it's empty. Thank you!


Solution

  • You said:

    The version is 0.62.

    As the changelog explains:

    0.66 Oct 11, 2015

    • documentation fix (reported by Alex Kok)
    • allow redirecting debug output to a custom file handle

    $Net::OpenSSH::debug_fh is only supported in Net::OpenSSH 0.66+. In older versions it always logs to STDERR.