linux 禁止SSH到另一个服务器后的输出

yftpprvb  于 2022-11-02  发布在  Linux
关注(0)|答案(2)|浏览(200)

当我用SSH连接到另一个服务器时,当你登录时,总是会输出一些简短的文本。(无论是SSH还是登录到它自己的会话)
“验证横幅”是它打印出来的每一次我要么scp一个文件或SSH到它。
我的代码遍历一个服务器列表并发送一个文件,每次它都输出很多我想隐藏的文本。
这段代码在每个服务器中循环,打印出它在做什么。

for(my $j=0; $j < $#servName+1; $j++)
    {
        print "\n\nSending file: $fileToTransfer to \n$servName[$j]:$targetLocation\n\n";
        my $sendCommand = `scp $fileToTransfer $servName[$j]:$targetLocation`;
        print $sendCommand;
    }

但后来它出来这样:

Sending file: /JacobsScripts/AddAlias.pl to
denamap2:/release/jscripts

====================================================

  Welcome authorized users. This system is company
  property and unauthorized access or use is prohibited
  and may subject you to discipline, civil suit or
  criminal prosecution. To the extent permitted by law,
  system use and information may be monitored, recorded
  or disclosed. Using this system constitutes your
  consent to do so. You also agree to comply with applicable
  company procedures for system use and the protection of
  sensitive (including export controlled) data.

====================================================

Sending file: /JacobsScripts/AddAlias.pl to
denfpev1:/release/jscripts

====================================================

  Welcome authorized users. This system is company
  property and unauthorized access or use is prohibited
  and may subject you to discipline, civil suit or
  criminal prosecution. To the extent permitted by law,
  system use and information may be monitored, recorded
  or disclosed. Using this system constitutes your
  consent to do so. You also agree to comply with applicable
  company procedures for system use and the protection of
  sensitive (including export controlled) data.

====================================================

我还没有尝试太多,我看到一些论坛,提到采取输出到一个文件,然后删除它,但idk,如果这将为我的情况工作。

v440hwme

v440hwme1#

注意事项 * 这个答案假设在所讨论的系统上,ssh/scp消息转到STDERR流(或者甚至直接转到/dev/tty)†,就像它们在我测试的一些系统上所做的那样--这就是问题所在。*

  • 如果不是,那么池上的回答当然会照顾到它:但即使在这种情况下,我也认为这里显示的所有方法都更适合捕获输出(除了涉及shell的方法),特别是在需要两个流的时候。*

可以通过配置服务器或通过.hushlogin文件来抑制这些打印,但这显然取决于服务器管理。
否则,是的,您可以将标准流重定向到文件,或者更好的是,重定向到变量,这使得整体管理更容易。
使用IPC::Run

use IPC::Run qw(run);

my ($file, $servName, $targetLocation) = ...
my @cmd = ("scp", $file, $servName, $targetLocation);

run \@cmd, '1>', \my $out, '2>', \my $err;

# Or redirect both to one variable

# run \@cmd, '>&', \my $out_err;

这个强大而全面的库允许对它运行的外部进程进行很好的控制;它几乎提供了一个迷你外壳。
或者使用简单得多、非常方便的Capture::Tiny

use Capture::Tiny qw(capture);

...    
my ($out, $err, $exit) = capture { system @cmd };

这里的输出可以使用capture_merged合并。使用这个库也明显上级内置的(qxsystem,pipe-open)。
在这两种情况下,检查$out$err变量,因为错误信息取决于你的系统,所以这远不是一成不变的。对于一些错误,库例程die/croak,但对于其他一些错误,它们不会,而只是打印到STDERR。使用库提供的其他工具来检测错误可能更可靠。
ssh/scp“正常”(非错误)消息可能打印到STDERRSTDOUT流,甚至可能直接打印到/dev/tty,†因此可能与错误消息混合。
考虑到意图似乎是将这些scp命令与其他打印命令穿插在一起,那么我推荐这两种方法中的任何一种,而不是下面的其他方法。
另一种选择(我认为总体上最不令人满意)是使用shell在命令本身中重定向输出,或者将其重定向到单独的文件

my ($out_file, $err_file) = ...
system("@cmd 2> $err_file 1> $out_file" ) == 0 
    or die "system(@cmd...) error: $?";  # see "system" in perldoc

或者,可能为了方便起见,两个流都可以转到一个文件

system("@cmd > $out_err_file 2>&1" ) == 0  or die $?;

然后检查文件是否有错误,如果没有什么值得注意的,就删除文件。

my $out_and_err = qx(@cmd 2>&1);

然后检查(可能是多行)变数是否有错误。
或者,代替处理单个命令,我们可以在程序的大部分时间内将流本身重定向到文件

use warnings;
use strict;
use feature 'say';

# Save filehandles ('dup' them) so to be able to reopen later

open my $saveout, ">&STDOUT"  or die "Can't dup STDOUT: $!";
open my $saveerr, ">&STDERR"  or die "Can't dup STDERR: $!";#]]

my ($outf, $errf) = qw(stdout.txt stderr.txt);
open *STDOUT, ">", $outf or die "Can't redirect STDOUT to $outf: $!";
open *STDERR, ">", $errf or die "Can't redirect STDERR to $errf: $!";

my ($file, $servName, $targetLocation) = ...
my @cmd = ("scp", $file, $servName, $targetLocation);
system(@cmd) == 0 
    or die "system(@cmd) error: $?";  # see "system" in perldoc

# Restore standard streams when needed for normal output

open STDOUT, '>&', $saveout  or die "Can't reopen STDOUT: $!";
open STDERR, '>&', $saveerr  or die "Can't reopen STDERR: $!";

# Examine what's in the files (errors?)

我使用system而不是qx(反勾的运算符形式),因为不需要scp的输出。大部分内容在open中介绍,具体内容请搜索SO。
如果能够重新打开变量流就好了,但在这里不起作用
†这是偶数prescribed ("allowed") by POSIX

/开发人员/tty

在每个进程中,与该进程的进程组(如果有的话)相关联的控制终端的同义词。它对于那些希望无论输出如何重定向都能确保向终端写入信息或从终端阅读数据的程序或外壳过程是有用的。它也可用于要求输出文件名的应用程序。当需要键入输出并且找出当前正在使用的终端是令人厌烦的时候。
this superuser post的礼貌,其中有一个实质性的讨论。

sdnqo3pr

sdnqo3pr2#

您正在捕获文本,然后使用print $sendCommand;将其打印出来。您可以简单地删除该语句。

相关问题