When I SSH to another server thare are some blurbs of text that always outputs when you log in. (wheather its SSH or just logging in to its own session)
"Authentification banner" is what it prints out every time i either scp a file over or SSH into it.
My code iterates thru a list of servers and sends a file, each time it does that it outputs a lot of text id like to suppress.
This code loops thru each server printing out what its doing.
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;
}
But then it comes out like this:
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.
====================================================
I havent tried much, i saw a few forums that mention taking the output into a file and then delete it but idk if thatll work for my situation.
NOTE This answer assumes that on the system in question the
ssh/scpmessages go toSTDERRstream (or perhaps even directly to/dev/tty)†, like they do on some systems I test with -- thus the question.If not, then ikegami's answer of course takes care of it: just don't print the captured
STDOUT. But even in that case, I also think that all ways shown here are better for capturing output (except for the one involving the shell), specially when both streams are needed.These prints can be suppressed by configuring the server, or perhaps via a
.hushloginfile, but then that clearly depends on the server management.Otherwise, yes you can redirect standard streams to files or, better yet, to variables, what makes the overall management easier.
Using IPC::Run
This mighty and rounded library allows great control over the external processes it runs; it provides almost a mini shell.
Or using the far simpler, and very handy Capture::Tiny
Here output can be merged using
capture_merged. Working with this library is also clearly superior to builtins (qx,system, pipe-open).In both cases then inspect
$outand$errvariables, what is far less cut-and-dry as error messages depend on your system. For some errors the library routinesdie/croakbut for some others they don't but merely print toSTDERR. It is probably more reliable to use other tools that libraries provide for detecting errors.The
ssh/scp"normal" (non-error) messages may print to eitherSTDERRorSTDOUTstream, or may even go directly to/dev/tty,† so can be mixed with error messages.Given that the intent seems to be to intersperse these
scpcommands with other prints then I'd recommend either of these two ways over the others below.Another option, which I consider least satisfactory overall, is to use the shell to redirect output in the command itself, either to separate files
or, perhaps for convenience, both streams can go to one file
Then inspect files for errors and remove if there is nothing remarkable. Or, shell redirections can be used like in the question but to capture all output
Then examine the (possibly multiline) variable for errors.
Or, instead of dealing with individual commands we can redirect streams themselves to files for a duration of a larger part of the program
I use system instead of qx (operator form of backticks) since there is no need for output from
scp. Most of this is covered in open, and search SO for specifics.It'd be nice to be able to reopen streams to variables but that doesn't work here
† This is even prescribed ("allowed") by POSIX
Courtesy of this superuser post, which has a substiantial discussion.