【发布时间】:2018-01-13 15:19:29
【问题描述】:
#!/usr/bin/perl
use Net::SSH::Expect;
use warnings;
use strict;
#my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$usr")
# Making an ssh connection with user-password authentication
# 1) construct the object
my $ssh = Net::SSH::Expect->new (
host => "host",
password=> 'pwd',
user => 'user',
raw_pty => 1
#Expect=>log_file("finally.txt")
);
# 2) logon to the SSH server using those credentials.
# test the login output to make sure we had success
my $login_output = $ssh->login();
if ($login_output !~ /Welcome/) {
die "Login has failed. Login output was $login_output";
}
# disable terminal translations and echo on the SSH server
# executing on the server the stty command:
$ssh->exec("stty raw -echo");
my $stdout = $ssh->send(chr(13));
my $stdout2 = $ssh->send("SDT-FI");
my $stdout3 = $ssh->send("ENG");
my $stdout4 = $ssh->send('SORT FI-WIP "84144"');
my $stdout5 = $ssh->send(chr(13));
my $stdout6 = $ssh->send("OFF");
my $stdout7 = $ssh->send(chr(13));
print($stdout3);
#$expect->log_file("adp-n.txt");
#y $line;
# returns the next line, removing it from the input stream:
# while ( defined ($line = $ssh->read_all()) ) {
# print $line . "\n";
#}
所以我正在尝试打印 $stdout3 以便获取有关输出的信息 但我不断收到“在 connnn3.pl 第 50 行打印中使用未初始化值 $stdout3”
我的代码有问题吗? 我该如何解决这个问题?
更新,已解决! 它返回“使用未初始化值”的原因是因为函数
send()
是无效的,所以我改为使用
exec()
这样就解决了
【问题讨论】:
-
当您拥有
print($stdout2)(和其他号码)时会发生什么?祝你好运。 -
@shellter 它给了我同样的错误
-
请不要将像 solved 这样的 cmets 编辑到您的问题中。相反,请用适当的解决方案自己回答您的问题,以便其他人受益。 Stack Overflow 不是一个论坛,而是一个问答网站。
标签: perl ubuntu ssh command-line terminal