Plink returning unwanted characters via C#

隐身守侯 提交于 2019-12-06 12:13:14

Those are ANSI escape codes.

While using the --color=never may help, the root problem is that, the way you run Plink (feeding the command using a standard input), you make it use an interactive session. Most systems nowadays are configured to use a fancy formatting for directory listing (and other stuff) for interactive sessions to be more human friendly.

While in your case, no human is involved, so you should use a non-interactive session.

There are two ways to do that:

  • Either provide the command on Plink command-line (instead of feeding it using a standard input), like:

    plink.exe -ssh username@example.com -pw password "ls -l ... | grep ... | sort"
    

    This syntax implicitly forces a non-interactive mode.

  • Or use the -T switch to explicitly force the non-interactive mode:

    plink.exe -ssh username@example.com -pw password -T
    

    This syntax allows you to keep feeding the command using the standard input.

Using the non-interactive session (on a properly configured system) you avoid all the troubles of the interactive sessions (not just the --color). So it's a way more generic solution.


If this does not work for you, it must be because your server is misconfigured and aliases the ls to use the --color even for non-interactive sessions.

If you cannot fix the server, I suggest you use both non-interactive session (to avoid other possible troubles) and the --color=never switch. Possibly better solution than using the --color=never is using the unalias ls.

Thank You user2864740 ! I added --color=never and those damn characters disappeared.

            private const string GetFileNames = "ls --color=never -l */informatica/tgtdynamicparams*.out  " +
                                        "| grep vaulttest " +
                                        "| grep 'Sep  1' " +
                                        "| awk '{print $9}' " +
                                        "| sort";
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!