使用Renci SSH.NET在远程unix服务器中复制文件

时间:2019-01-15 13:01:10

标签: c# ssh.net

我正在尝试将文件从远程UNIX服务器中的一个文件夹复制到另一个文件夹。我在C#中使用Web应用程序,并在此过程中使用Renci SSH.Net。当我使用Cp命令时,在阅读行中添加了额外的空间,并且出现错误

  

cp:无法统计`folder1 / folder_two / fol_three / changecolumn.txt':没有此类文件   或目录

我使用了以下代码:

SshClient sshclient = new SshClient("hostname", "username", "pwd");
sshclient.Connect();
ShellStream stream = sshclient.CreateShellStream("cmsd", 80, 24, 800, 600, 1024);
sendCommand("sudo su - wwabc1", stream).ToString();
sendCommand("whoami", stream).ToString();
sendCommand("cp / folder1/folder_two/fol_three/" + uploadedfileName + uploadedfileExt + " /Target1/folder1/folder_two/target/", stream).ToString();

public StringBuilder sendCommand(string customCMD, ShellStream stream)
        {
            StringBuilder answer;
            var reader = new StreamReader(stream);
            var writer = new StreamWriter(stream);
            writer.AutoFlush = true;
            WriteStream(customCMD, writer, stream);
            answer = ReadStream(reader);
            return answer;
        }

        private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
        {
            writer.WriteLine(cmd);
            while (stream.Length == 0)
            {
                Thread.Sleep(500);
            }
        }

        private StringBuilder ReadStream(StreamReader reader)
        {
            StringBuilder result = new StringBuilder();

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                result.AppendLine(line);
            }
            return result;
        }

结果具有如下数据:

cp / folder1/folder_two/fol_three/changecolumn
< cp / folder1/folder_two/fol_three/changecolumnn                         ame.txt  /wwabc1/Target1

</test_files/changecolumnname.txt  /wwabc1/Target1/                         folder1/folder_two/target/
cp: omitting directory `/'
cp: cannot stat `folder1/folder_two/fol_three//changecolumnname.txt': No such file or directory
[wwabc1@host ~]$ 
[wwabc1@host ~]$ 

之间添加了太多空白,但是我传递的信息之间没有空格。如何解决这个问题?谢谢

1 个答案:

答案 0 :(得分:1)

这里:

sendCommand("cp / folder1...

注意第一个/之后的空格。