问题描述
通过C#使用Plink时,在结果前后都出现不需要的字符:
I am getting unwanted characters before and after my results when using Plink via C#:
命令:
ls -l */informatica/tgtdynamicparams*.out | grep vaulttest| grep 'Sep 1'|awk '{print $9}' | sort
Linux结果:
aaco/informatica/tgtdynamicparams2269885_CHECK_REF_COMPANY.out
cdph/informatica/tgtdynamicparams2225704_CDPHDRUGRECON.out
cdph/informatica/tgtdynamicparams2225704_CDPHELIGRECON.out
merh/informatica/tgtdynamicparams3454321_OPEN_TEST.out
merh/informatica/tgtdynamicparams3454322_OPEN_TEST2.out
通过Plink结果
C#:
C# via Plink result:
[01;32mcdph/informatica/tgtdynamicparams2225704_CDPHDRUGRECON.out[0m
[01;32mcdph/informatica/tgtdynamicparams2225704_CDPHELIGRECON.out[0m
[01;32mmerh/informatica/tgtdynamicparams3454321_OPEN_TEST.out[0m
[01;32mmerh/informatica/tgtdynamicparams3454322_OPEN_TEST2.out[0m
[0m[01;32maaco/informatica/tgtdynamicparams2269885_CHECK_REF_COMPANY.out[0m
我已经尝试了以下方法,但没有任何运气:
I've tried the following without any luck:
StandardOutputEncoding = Encoding.UTF8
StandardOutputEncoding = Encoding.ASCII
StandardOutputEncoding = Encoding.DEFAULT
这是我正在使用的代码.我已经收到类似问题的回复,建议使用ssh.net,但我不允许使用它必须是Plink.
Here is the code I'm using. I have seen replies for similar problems suggesting use of ssh.net but I am not permitted to use that it has to be Plink.
private string GetFileNames = @"ls -l */informatica/tgtdynamicparams*.out
| grep vaulttest| grep 'Sep 1'
| awk '{print $9}'
| sort";
GetProcessesCommands = new [] {
@"cd /src/trs/runjobs",
GetFileNames
};
Request(ServerName, UserName, Password, GetProcessesCommands);
调用此:
private void Request(string remoteHost, string userName, string password, IEnumerable<string> lstCommands)
{
try
{
var psi = new ProcessStartInfo
{
FileName = FormInformatiKill.PropFile.Plink,
Arguments = string.Format("-ssh {0}@{1} -pw {2}", userName, remoteHost, password),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8
};
var p = Process.Start(psi);
_mObjLock = new object();
_mBlnDoRead = true;
if( p == null ) return;
AsyncReadFeedback(p.StandardOutput); // start the async read of stdout
var strw = p.StandardInput;
foreach( var cmd in lstCommands )
{
strw.WriteLine( cmd ); // send commands
}
strw.WriteLine("exit"); // send exit command at the end
p.WaitForExit(); // block thread until remote operations are done
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
private void AsyncReadFeedback(StreamReader strr)
{
var trdr = new Thread(ReadFeedback);
trdr.Start(strr);
}
private void ReadFeedback(object objStreamReader)
{
var strr = (StreamReader) objStreamReader;
while (!strr.EndOfStream && _mBlnDoRead)
{
var line = strr.ReadLine();
// lock the feedback buffer (since we don't want some messy stdout/err mix string in the end)
lock (_mObjLock)
{
if( line != null ) ListOfFilesResults.Add(line.Trim());
}
}
}
我已经尝试过Trim()
.我什至花时间只是试图删除我不想要的字符,但是似乎有更多的可能性我无法为其编写代码.
I've tried Trim()
. I even spent time just trying to remove the characters I didn't want, but there seems to be more possibilities than I can code for.
推荐答案
这些都是 ANSI转义代码.
虽然使用--color=never
可能会有所帮助,但根本的问题在于,运行Plink的方式(使用标准输入提供命令)会使其使用交互式会话.如今,大多数系统都配置为使用精美的格式来进行交互式会话的目录列表(和其他内容),以使其更加人性化.
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:
-
要么在Plink命令行上提供命令(而不是使用标准输入来提供命令),例如:
Either provide the command on Plink command-line (instead of feeding it using a standard input), like:
plink.exe -ssh [email protected] -pw password "ls -l ... | grep ... | sort"
此语法隐式强制使用非交互模式.
This syntax implicitly forces a non-interactive mode.
或使用 -T
开关明确强制使用非交互模式:
Or use the -T
switch to explicitly force the non-interactive mode:
plink.exe -ssh [email protected] -pw password -T
此语法使您可以继续使用标准输入来馈送命令.
This syntax allows you to keep feeding the command using the standard input.
使用非交互式会话(在正确配置的系统上),可以避免交互式会话的所有麻烦(而不仅仅是--color
).因此,这是一种更通用的解决方案.
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.
如果这对您不起作用,那一定是因为您的服务器配置错误并且为ls
设置了别名,以便即使在非交互式会话中也可以使用--color
.
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.
如果无法修复服务器,建议您同时使用非交互式会话(以避免其他可能的麻烦)和--color=never
开关.比使用--color=never
更好的解决方案是使用unalias ls
.
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
.
这篇关于Plink通过C#返回不需要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!