3

I am using plink in windows to connect to a network device and capture the output to a file. That part works great.

Here is an example of the command line I am putting in a batch program.

plink.exe -v -l [username] -pw [password] [ip address] -m "c:\empty.txt" < "c:\commands.txt" > "c:\command_output.txt"

command_output.txt only contains the output from the ssh session.

I want to add some error handling in case it cannot connect for example. I can't figure out how to get the connection messages to go to a file. They go to the command window only. I have tried using start /c cmd.exe from another batch program and redirectiong that output to another log file. Tried using 2>&1. I get nothing. It always just goes to the command window.

Here is and example of what I want to capture.

Looking up host "xxx.xxx.xxx.xxx"
Connecting to xxx.xxx.xxx.xxx port 22
Failed to connect to xxx.xxx.xxx.xxx: Network error: Connection timed out
Network error: Connection timed out
FATAL ERROR: Network error: Connection timed out
  • I got it to work. Just had to get the right combination. I put the plink command in a separate batch program and use "call" to start it from a different batch program, redirected the output to a file and had to use "2>&1" to include STDERR. Kind of a kludgy mess but it works. – Don Thompson Feb 17 '16 at 02:18
  • 1
    You do not need the second batch file. But note that the order of redirection matters. This is correct: `> "c:\command_output.txt" 2>&1`, but this won't work: `2>&1 > "c:\command_output.txt"` (as it first redirects the error output to the default standard output, what is console at that moment yet) – Martin Prikryl Feb 17 '16 at 07:07

1 Answers1

1

So, to sum up use the command below. I'm incorporating @Martin Prikryl's suggestion into an answer so this shows as answered.

plink.exe -v -l [username] -pw [password] [ip address] -m "c:\empty.txt" < "c:\commands.txt" > "c:\command_output.txt" 2>&1
WinTakeAll
  • 258
  • 1
  • 9
  • @Don, is this working for you without the need for an extra batch file? If so please consider marking the answer "Accepted". [Here's why](http://meta.stackexchange.com/q/5234/317538) that's worthwhile. – WinTakeAll Feb 22 '16 at 23:35