问题
I'm logging into to a remote ssh session using plink.exe
to perform certain tasks using a batch script. Getting the output of these commands in a log file as well on the screen is very important for me.
I tried using usual batch way i.e. plink servername -m cmd.txt>logfile.log
way but the problem with this is that it won't display it on the Windows terminal that the batch script is running on.
Then I found the -sshlog
option of Plink. This does the work, i.e. I can get the output but on screen and in a log file, but this results in output as follows:
00000f90 56 4c 41 4e 2a 2a 0d 0a 20 65 6e 63 61 70 73 75 VLAN**.. encapsu 00000fa0 6c 61 74 69 6f 6e 20 64 6f 74 31 51 20 34 30 34 lation dot1Q 404 00000fb0 0d 0a 20 69 70 20 61 64 64 72 65 73 73 20 31 30 .. ip address 10 00000fc0 2e 37 31 2e 31 39 31 2e 31 34 35 20 32 35 35 2e .71.191.145 255.
My actual output starts at "VLAN**.. encapsu" in the text above The output has these "00000010 74 65 72 ... "bla bla characters which I do not want. Plus the main output (that would be displayed if i was using Plink interactively is "word-wrapped" and looks horrible which makes it very difficult to understand for a general user
Is there any way to prevent Plink output unwanted 'sshlog' characters in the log file? or Is there any other way to get the output on screen and log fail simultaneously in a Plink/PuTTY session inside a batch script?
I tried both -sshlog
and -sshrawlog
but same output. Also tried -sessionlog
as per the documentation but it does not work!
I tried also > file.txt
but it gave an empty file!
Expected results:
encapsulation dot1Q 404
ip address 10.71.191.145
回答1:
There's no command-line switch in Plink to log only the readable output.
You would have to configure "Printable output" logging in Windows Registry prior to running Plink.
But there are other options.
See Displaying Windows command prompt output and redirecting it to a file.
So this should do what you need:
powershell "plink -batch servername -m cmd.txt | tee logfile.log"
来源:https://stackoverflow.com/questions/57227976/both-display-and-save-plink-output