Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

hiding Expect output

by AzryelRyvel (Initiate)
on Feb 04, 2013 at 21:54 UTC ( #1017017=perlquestion: print w/replies, xml ) Need Help??
AzryelRyvel has asked for the wisdom of the Perl Monks concerning the following question:

I've got a perl script that automatically logs me via ssh by spawning ssh with It actually works pretty well and here is my ouput :
$ ngh bestServerEver GPG Pass : root@**********'s password: Linux **** 2.6.32-042stab059.7 #1 SMP Tue Jul 24 19:12:01 MSK 2012 x86 +_64 GNU/Linux motd!!!!!!!!!! Last login: Mon Feb 4 22:18:10 2013 from ******************* bash [root@******:~]$ bash [root@******:~]$
I'm trying to suppress this output to get to the server shell directly after typing my command. Like that :
$ngh BestServerEver root@server#
I've tried that answer :

I've also tried :
local (*OUT, *ERR); open OUT, ">&STDOUT"; open ERR, ">&STDERR"; close STDOUT; close STDERR; print "don't print"; open STDOUT, ">&OUT"; open STDERR, ">&ERR";
Both are okay when it's about standard STDOUT but Expect seems to be a different kind of handle or whatever. I've also tried setting :
But it did not hide anything Finally, here is the sub that spawns ssh <-- with color!

if you have some tips to give me on how to hide that junk :p
sub interactiveSsh { my ($pConfig,$pass)=@_; my $exp = new Expect; $exp->slave->clone_winsize_from(\*STDIN); $exp->spawn("ssh root\@".$pConfig->{'host'}); my $spawn_ok; $exp->expect(40, [ qr'(yes/no)', sub { my $fh = shift; $fh->send("yes\n"); exp_continue; } ], [ qr'assword:', sub { if ($spawn_ok) { $exp->interact(); } my $fh = shift; $fh->send("$pass\n"); $spawn_ok=1; exp_continue; } ], [ qr'#', sub { my $fh = shift; $fh->send("bash\n"); $exp->send("stty -echo\n"); $exp->interact(); exp_continue; } ], [ eof => sub { if ($spawn_ok) { print BOLD GREEN, "SSH + close connexion to ".$pConfig->{'host'}.".\n", RESET; exit 0; } else { die "ERROR: could not +spawn ssh.\n"; } } ], [ timeout => sub { die "No login.\n"; } ], '-re', qr'[#>:] $', ); $exp->interact();

Replies are listed 'Best First'.
Re: hiding Expect output
by salva (Abbot) on Feb 05, 2013 at 08:58 UTC
    use Net::OpenSSH; my $ssh = Net::OpenSSH->new($host, user => $user, password => $password, master_stdout_discard => 1, master_stderr_discard => 1); $ssh->system;
Re: hiding Expect output
by 7stud (Deacon) on Feb 05, 2013 at 06:01 UTC
    Check out the Expect faq here link.
Re: hiding Expect output
by AzryelRyvel (Initiate) on Feb 04, 2013 at 22:24 UTC
    well, thanks to someone else, i got to hide the motd part(with $exp->log_stdout(0);) but i can still see the commands i send : When $exp->send("bash\n"); I get :
    bash root@server# bash root@server#

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017017]
Approved by muba
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2018-03-22 03:42 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (272 votes). Check out past polls.