简体   繁体   中英

Copying files within Remote unix server using Renci SSH.NET

I am trying to copy files from one folder to another within the remote unix server. I am using a web application in C# and using Renci SSH.Net for the process. When I using the Cp command , in the readline additional space is getting added and I am getting error as

cp: cannot stat `folder1/folder_two/fol_three/changecolumn.txt': No such file or directory

I have used the below code:

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;
        }

The result is having the data as below:

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 ~]$ 

There is too much blank space added in between, but I am passing the information without the spaces in between. How to fix this? Thanks

Here:

sendCommand("cp / folder1...

Notice the space after the first / .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM