-1

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

venkat14
  • 583
  • 3
  • 12
  • 34
  • My guess would be the issue is with `uploadedfileName` and `uploadedfileExt`, which you haven't supplied the source for – Neil Jan 15 '19 at 13:05
  • uploadedfileName and uploadedfileExt contains data as - changecolumnname.txt – venkat14 Jan 15 '19 at 13:09
  • Yes, but does it? Your error message suggests this is not the case. Try doing `var s = $"cp / folder1/folder_two/fol_three/{uploadedfileName}{uploadedfileExt} /Target1/folder1/folder_two/target/"` and see what you get. – Neil Jan 15 '19 at 13:10
  • BTW if you want to copy one stream to another you can do `stream1.CopyTo(stream2)` – Neil Jan 15 '19 at 13:12
  • should I add the host name at the front of the initial folder1 ? – venkat14 Jan 15 '19 at 13:12
  • Your errors all relate to that extra space after the first slash. If you have a different problem, perhaps that should be a different question. – Neil Jan 15 '19 at 13:15
  • If you believe that the problem is not with `uploadedfileName` and `uploadedfileExt` variables, show us [mcve] - with literal example of `cp` command, without any variables. – Martin Prikryl Jan 15 '19 at 13:43

1 Answers1

1

Here:

sendCommand("cp / folder1...

Notice the space after the first /.

Neil
  • 11,059
  • 3
  • 31
  • 56
  • 1
    im flagging the question as a typo – Cleptus Jan 15 '19 at 13:09
  • That space i have removed. In the output which I have posted, you can see the space as < cp / folder1/folder_two/fol_three/changecolumnn ame.txt /wwabc1/Target1 After the changecolumnn and ame there is a space, which I have not passed. – venkat14 Jan 15 '19 at 13:10
  • This is not a typo issue – venkat14 Jan 15 '19 at 13:11
  • The spaces there all result of you using "shell" instead of "exec" channels - what I have discouraged you from using in my previous answer to your other question https://stackoverflow.com/q/54194139/850848 - And it's actually most likely not related to your immediate problem - you can see that in the actual error message, where the file name does not contain the spaces: `cp: cannot stat 'folder1/folder_two/fol_three//changecolumnname.txt': No such file or directory`. – Martin Prikryl Jan 15 '19 at 13:47
  • It looks like your file name is invalid - hence your next problem (after fixing the one suggested by @Neil) is still most likely (another) typo. – Martin Prikryl Jan 15 '19 at 13:47