I've a script which establishes a SSH connection and execute few bash commands. All these activities are getting logged in a log file. I'm using Net::Telnet to achieve the same. The script is working fine on Fedora 20, however if I run the same script of CentOS 7.5, it is running into a issue where it is unable to log the command sent to a file. Could you please take a look and let me know what went wrong and how to fix it. Please find the code snipped below-
#!/usr/bin/perl use strict; use warnings; use api::Telnet; use Data::Dumper; my $host_prompt = '/[\$%#>] $/'; my $owner = &open_pty ( user_name => "root", user_pswd => "a", ip_addr => "127.0.0.1", prompt => $host_prompt, log_file => 'Log/Owner', dump_log => 'Log/dump_log', # input_log => 'Log/input_log', ); my @output = $owner->cmd ( String => "date", Prompt => $host_prompt ); print "Output is @output\n";
open_pty function definition -
package api::Telnet; use strict; use warnings; use Net::Telnet; use Exporter; use Carp; our @ISA = qw (Exporter); our @EXPORT = qw ( &open_pty &close_pty ); # Constant global variables use constant SSH_TIMEOUT => 1800; sub spwan_pty { my (@cmd) = @_; my ($pid, $tty, $tty_fd); # Create a new pseudo terminal use IO::Pty (); my $pty = new IO::Pty or die $!; # Execute the program in another process # Child process unless ($pid = fork) { die "problem spawning program: $!\n" unless defined $pid; # Disassociate process from existing controlling terminal use POSIX (); POSIX::setsid or die "setsid failed: $!"; # Associate process with new controlling terminal $pty->make_slave_controlling_terminal; $pty->set_raw(); $tty = $pty->slave(); $tty_fd = $tty->fileno; close $pty; # Make stdio use the new controlling terminal open STDIN, "<&$tty_fd" or die $!; open STDOUT, ">&$tty_fd" or die $!; open STDERR, ">&STDOUT" or die $!; close $tty; exec @cmd or die "problem executing $cmd[0]\n"; } # end child process $pty; } sub open_pty { my (%args) = @_; # Start ssh program. my $pty = &spwan_pty("ssh", "-l", $args{user_name}, "-e", "none", "-F", "/dev/null", "-o", "PreferredAuthentications=password", "-o", "NumberOfPasswordPrompts=1", "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", $args{ip_addr} ); # Create a Net::Telnet object to perform I/0 on ssh's tty my $ssh = new Net::Telnet ( -fhopen => $pty, -prompt => $args{prompt}, -telnetmode => 0, -cmd_remove_mode => 1, -timeout => SSH_TIMEOUT, -output_record_separator => "\r", #-errmode => sub { print "Telnet FAIL\n"; } ); # Wait for the password prompt and send password. $ssh->waitfor(-match => '/password: ?$/i', -errmode => "return") or die "problem connecting to \"$args{ip_addr}\": ", $ssh->las +tline; $ssh->print($args{user_pswd}); # Wait for the shell prompt. my (undef, $match) = $ssh->waitfor( -match => $ssh->prompt, -match => '/^Permission denied/m', -errmode => "return" ) or return $ssh->error("login failed: expected shell prompt ", "d +oesn't match actual\n"); return $ssh->error("login failed: bad login-name or password\n") i +f $match =~ /^Permission denied/m; # logging $ssh->input_log($args{log_file}); $ssh->cmd("ifconfig"); $ssh->cmd("date"); return $ssh; } sub close_pty { my ($tty) = shift; $tty->close(); } 1;
Log File (Owner) when execute on Fedora 20
[root@n3fips-346 ~]# date Wed Jul 3 14:33:03 IST 2019
Dump Log when executed on Fedora 20
< 0x00000: 5b 72 6f 6f 74 40 6e 33 66 69 70 73 2d 33 34 36 [root@n +3fips-346 < 0x00010: 20 7e 5d 23 20 ~]# > 0x00000: 64 61 74 65 0d date. < 0x00000: 64 61 74 65 0d 0a date.. < 0x00000: 57 65 64 20 4a 75 6c 20 20 33 20 31 34 3a 33 33 Wed Jul + 3 14:33 < 0x00010: 3a 30 33 20 49 53 54 20 32 30 31 39 0d 0a :03 IST + 2019.. < 0x00000: 1b 5d 30 3b 72 6f 6f 74 40 6e 33 66 69 70 73 2d .]0;roo +t@n3fips- < 0x00010: 33 34 36 3a 7e 07 346:~. < 0x00000: 5b 72 6f 6f 74 40 6e 33 66 69 70 73 2d 33 34 36 [root@n +3fips-346 < 0x00010: 20 7e 5d 23 20 ~]#
Log File (Owner) when executed on CentOS 7.5
[root@hyd1658 ~]# Wed Jul 3 14:39:47 IST 2019
Dump log when executed on CentOS
< 0x00000: 5b 72 6f 6f 74 40 68 79 64 31 36 35 38 20 7e 5d [root@h +yd1658 ~] < 0x00010: 23 20 # > 0x00000: 64 61 74 65 0d date. < 0x00000: 57 65 64 20 4a 75 6c 20 20 33 20 31 34 3a 33 39 Wed Jul + 3 14:39 < 0x00010: 3a 34 37 20 49 53 54 20 32 30 31 39 0a :47 IST + 2019. < 0x00000: 1b 5d 30 3b 72 6f 6f 74 40 68 79 64 31 36 35 38 .]0;roo +t@hyd1658 < 0x00010: 3a 7e 07 :~. < 0x00000: 5b 72 6f 6f 74 40 68 79 64 31 36 35 38 20 7e 5d [root@h +yd1658 ~] < 0x00010: 23 20 #

In reply to Command sent via Net::Telnet is not getting logged in CentOS. However, the same is getting logged in Fedora 20 by shoundic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.