【问题标题】:use of uninitialized value. How can I fix this error?使用未初始化的值。我该如何解决这个错误?
【发布时间】: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


【解决方案1】:

来自the documentation of Net::SSH::Expect

void send($string) - 发送 $string 到 SSH 服务器,不返回任何内容

因此,send 显然不返回任何内容 (void),这就是为什么您在尝试打印 send 的(不存在的)返回值时收到此警告的原因。如果您想从服务器取回数据,请使用 peekeatread_all 或类似文档。

【讨论】:

  • 你能举个例子吗?
  • @popeonhabbo123:你看过文档了吗?它包含示例。
  • 我解决了,谢谢 :) 我改用 exec 并且它有效。感谢您让我知道 send 是无效的 :) 它真的很有帮助
猜你喜欢
  • 1970-01-01
  • 2021-12-23
  • 2016-07-21
  • 2016-09-09
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多