Command executed in SSH terminal displays extra line contents

南楼画角 提交于 2020-02-04 18:59:09

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/57511768/command-executed-in-ssh-terminal-displays-extra-line-contents

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!