1

Using plink in cmd to ssh my MacOS and ls the dir's. However, the results are in a single column. Because the list is long but the names are short, I'm trying to display the results in 3 or 4 columns like ls does by default in terminal. I can't get it to do this when using plink.

Few options I've tried:

plink.exe -ssh -l <username> -pw <password> <IP> "ls /Users/Dir/"
plink.exe -ssh -l <username> -pw <password> <IP> "ls -c /Users/Dir/"
plink.exe -ssh -l <username> -pw <password> <IP> "ls /Users/Dir/ > Results.txt"
plink.exe -ssh -l <username> -pw <password> <IP> "column Results.txt"

Current results using plink with CMD

Folder 1
folder 2
folder 3
folder 4
folder 5
folder 6

Looking to get this result using plink with CMD

folder 1    folder 2    folder 3
folder 4    folder 5    folder 6
folder 7    folder 8    folder 9
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
rcmpayne
  • 163
  • 2
  • 15

2 Answers2

1

When you use commands in a session without a terminal, they tend to produce output in machine-readable deterministic format.

For ls, that means one file per line, no coloring, etc.

When you execute a command using plink (or OpenSSH ssh) from its command-line, it does not allocate a pseudo terminal for the session. So the ls (and other commands) behave like you experience.

If you want the commands to behave like in the terminal, add -t switch to its command line to force the use of the pseudo terminal.

plink.exe -t -ssh -l <username> -pw <password> <IP> "ls /Users/Dir/"

Though if your goal is to parse the output, do not do that, as the format might change with a change to the environment and your code will break. You can also get lot of other side effects, like ANSI escape codes, pagination, etc. You better stick with the default format.


More generic question about executing commands with Plink:
Script via Plink in .bat behaves differently

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

Found a way around this by using 'pr' from winGnu. Switched the plink command to output the results to a file on the PC and pr to column it in cmd.

mode con:cols=180 lines=100
echo. > Results.txt
plink.exe -t -ssh -l <username> -pw <pass> <IP> "ls /Users/Dir/" > Results.txt
pr -3 -t -w180 Results.txt
del Results.txt
rcmpayne
  • 163
  • 2
  • 15