vkk05
vkk05

Reputation: 3222

Command executed in SSH terminal displays extra line contents

Using Net::SSH::Expect perl module I tried to connect to the server and connection is success. But when its executing the command in connected server and displaying output in current window it displays extra line of output.

Below is my code

#!/usr/bin/perl 

use Net::SSH::Expect;

my $ssh = Net::SSH::Expect->new (
    host => 'hostip', #deb-2-scripting ip
    user => 'user',
    password => 'password',
    raw_pty => 1
);

$ssh->run_ssh() or die "SSH process couldn't start: $!";


$ssh->waitfor('password: ');

$ssh->send("password");

$ssh->exec("stty -echo");

my $hostname = $ssh->exec('hostname');
print "HOST:$hostname\n";

$ssh->close();

And output is below:

[vinod@deb-3-serv1 TEST_DIR]$ perl test_ssh.pl
HOST:deb-2-scripting
[user@deb-2-scripting(Host) ~]$

I wanted output should be displayed like below when I execute test_ssh.pl

HOST:deb-2-scripting

and last line shouldn't be printed which shows in my output result above.

Upvotes: 2

Views: 207

Answers (1)

user10678532
user10678532

Reputation:

Pass the no_terminal => 1 option to the constructor.

my $ssh = Net::SSH::Expect->new (
    host => 'hostip', #deb-2-scripting ip
    user => 'user',
    password => 'password',
    raw_pty => 1,
    no_terminal => 1,
);

That will prevent the ssh server from allocating a pseudo-tty, and the remote shell should not print a prompt when not running in a tty.

Upvotes: 2

Related Questions