Windows batch scripting: SSH with Plink showing strange sequences in output

落爺英雄遲暮 提交于 2021-02-08 07:26:32

问题


I am scripting some command line operations for collecting and parsing specific network metrics from a Palo Alto 5060 firewall. I am using Plink and Windows batch scripting.

@echo off
"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password < "C:\path\to\commands.txt >> "C:\path\to\output.txt"

The content of the commands.txt is simple at the moment.

show interface ethernet1/1

I cannot get this to work. My output.txt has the following results:

Last login: Tue Nov 24 15:43:13 2015 from localhost

show interface ethernet1/1Welcome user.
user@pa5060> show 
[Kuser@pa5060> show interface 
[Kuser@pa5060> show interface ethernet1/1

This isn't the proper output and the entry of the commands confuses me. Has anyone seen something like this? There is a login banner on this device if that is relevant.


回答1:


I'd guess you are missing a new-line at the end of the command.txt, so the command is not submitted.


As for the repeated prompt and the [K sequence:
This is simply because the remote side expects an interactive terminal on your end, and sends ANSI escape sequences to pretty-print an output.

Each line likely starts with the CR (carriage return) character that would cause the interactive terminal to overwrite the previous line. But this does not work, when you redirect the output to a file. Though if you print the file on a terminal (cmd.exe) using type output, you will probably get only the last line.

To make Plink not enable the interactive terminal, use the -T command-line switch:

"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password -T < "C:\path\to\commands.txt >> "C:\path\to\output.txt"

Though even better is to specify the command on PLink command line

"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password show interface ethernet1/1 >> "C:\path\to\output.txt"

or using -m switch:

"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password -m "C:\path\to\commands.txt >> "C:\path\to\output.txt"

The difference is that the commands specified this way are automatically executed in a non-interactive terminal and mainly in an "exec" channel in a more controlled way, then in the "shell" channel you are using when redirecting the input. So you get rid of the "Last login:" message as well as the command prompt (user@pa5060>) and such.



来源:https://stackoverflow.com/questions/33904171/windows-batch-scripting-ssh-with-plink-showing-strange-sequences-in-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!