使用未初始化的价值。我该如何解决这个错误?

时间:2017-08-06 01:56:28

标签: perl ubuntu ssh command-line terminal

#!/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()

这解决了它

1 个答案:

答案 0 :(得分:4)

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

  

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

因此,import pandas as pd import re import io text = """ A B Col #3 NaN NaN NaN -0.041158 -0.161571 0.329038 0.238156 0.525878 0.110370 0.606738 0.854177 -0.095147 0.200166 0.385453 0.166235""" with io.StringIO(re.sub("(?<=[0-9]) +", " ", re.sub("^ +", "", text))) as fs: df = pd.read_table(fs, header=0, sep="\s{2,}",engine='python') # A B Col #3 # 0 NaN NaN NaN # 1 -0.041158 -0.161571 0.329038 # 2 0.238156 0.525878 0.110370 # 3 0.606738 0.854177 -0.095147 # 4 0.200166 0.385453 0.166235 显然不会返回任何内容(void),这就是为什么在尝试打印{not 1}}的(不存在的)返回值时会收到此警告的原因。如果您想从服务器获取数据,请使用sendsendpeek或与文档类似的内容。