-1

I'm trying to pull information off a switch using plink. The problem is it keeps trying to insert a "press space to continue" break that I don't seem to be able to turn off. I know that this break is inserted based on the number of rows in the terminal; a manual putty session changes the number of lines scrolled based on window height. How can I change plink's behavior to present enough rows for the queries I'm running to complete without wanting to insert breaks?

FTBC
  • 11
  • 4

2 Answers2

0

To automate commands, you should run your SSH client without terminal emulation in the first place. This way, you won't have to deal with problems like this.

Plink (similarly to OpenSSH ssh client) does not use the terminal emulation, when you specify the command on Plink's command-line:

plink username@host less file.txt

This way, the less command (and similar) would behave like simple cat, as there's no terminal.


Some obscure SSH servers, like those running on some switches, do not support the SSH "exec" channel used by Plink for execute the command specified on its command-line.

If that's your case, you are probably passing the command to Plink using input redirection like:

echo less file.txt | plink username@host 

In this case, Plink uses terminal emulation. So less will paginate the output.

To stop Plink from using terminal emulation, use -T switch:

echo less file.txt | plink username@host -T
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I've tried the -T switch and got the same result. Here's the command I just ran: `plink username@host -pw password -T "command"` And it pauses after ~20 lines. (edit: just started using stackoverflow, the text field is...less than intuitive. So excuse my lack of line breaks) – FTBC Nov 09 '18 at 15:33
  • If you run your command in PuTTY in a maximized window, when does the command pause? – Martin Prikryl Nov 09 '18 at 15:41
  • Roughly after the screen fills. I think it's actually an additional two lines. Which is why I was looking for a way to tell it plink has 9000 rows since it doesn't seem to care if it use the -T switch. – FTBC Nov 09 '18 at 15:51
  • If you use `-T` switch, Plink won't even tell the remote side that it has any number of lines. Decent command will then do not paginate. Maybe your SSH server or command falls back to some hard-coded number of lines instead [what is a bug imo]. In such case, you can try to create a stored session in PuTTY with a default window size set to 9000 rows (if even possible) and use that stored session in Plink (with `-t` switch [not `-T`] to force terminal emulation) - like `plink session -t command`. Though I'm not even sure, if windows size setting has any effect on Plink. – Martin Prikryl Nov 09 '18 at 15:57
  • If not, you are out of luck imo. - You would have to use another command-line/scriptable SSH client that can behave like it has a window of set size. – Martin Prikryl Nov 09 '18 at 15:58
  • Or you would have to simulate key presses - e.g. using input redirection like `(echo. & echo. & echo .) | plink username@host command` – Martin Prikryl Nov 09 '18 at 16:03
  • The really odd thing is this same command worked yesterday. I'm e-mailed someone in our NOC looking for ideas on the switch side. I'll see if I can figure out the PuTTy session. – FTBC Nov 09 '18 at 16:09
  • So I figured out that I can use "term length 0" in the switch to fix that problem, now the issue I have is getting plink to run more than one command on the switch. – FTBC Nov 12 '18 at 14:40
  • OK, so maybe you can change your question to show your findings and ask about passing multiple commands instead. – Martin Prikryl Nov 12 '18 at 15:28
0

The best solution I've found to this is to send the commands to plink out of a text file like this:

plink username@host -pw password < commands.txt

One last issue I encountered is that doing it this way caused it to output a normal session, including the splash message which added a lot of lines to my very carefully-crafted query results. In my situation this was relatively easy to work around as rows with the data I want start with integers and other rows all begin with mixed characters, so I could just build in a simple "if %A equ +%A" check to weed out the useless information.

For those interested, this is what my single-line command looks like:

for /f "tokens=1,2,3,7" %A in ('plink user@host -pw pass ^< commands.txt') do @if %A equ +%A for /f "tokens=2 %E in ('plink user@host -pw pass "command using %C from first command"') do @if not %D == %E @echo %B %C %D %E >> outputfile.txt

It pulls a list and filters it as described above, then uses each row of the list to run a second command to compare a specific bit of information. If a mismatch is found, the relevant information is dropped in the output file.

Now if I could just figure out how to build in variable prompts that I can use inline. Windows 10 processes those differently and they don't work they way they did in previous versions.

(yes this would be easy in a batch file, but ridiculous security policies prevent me running batch files that would make my job much easier. So I build stuff like this monstrosity.)

FTBC
  • 11
  • 4