-1

Problem: I am using C# .net platform to SFTP a file to remote host with a key file/.pem file and no password.

C#.net source code:

ProcessStartInfo p = new ProcessStartInfo
{
    FileName = "sh",
    Arguments = "upload.sh " + file
};
p.RedirectStandardOutput = true;
p.UseShellExecute = false;
p.CreateNoWindow = true;
Process proc1 = new Process();
proc1.StartInfo = p;
proc1.Start();
string result = proc1.StandardOutput.ReadToEnd();
log.InfoFormat(result);

upload.sh:

sftp -i testsftp-key testsftp@sftp.xxx-xxx.com
put  filename

here

  1. testsftp-key :.pem filename(key file),
  2. testsftp :username,
  3. sftp.xxx-xxx.com :host address.
  4. filename :file to be uploaded

File is not getting uploaded when exe is executed by rrot user/cronjob. Executing using non-root user like pi uploads the file. Permissions are 777 for all.

Error:

permission denied

How to solve this permission issue?

Sayan Sen
  • 1,654
  • 3
  • 16
  • 26
  • 1
    why are you not able to run the command what are the errors and what is the output – BugFinder Nov 18 '19 at 13:34
  • Is it possible that the upload.sh file is not located in the working directory of your application? – Longoon12000 Nov 18 '19 at 13:47
  • @Longoon12000 no it is located in the working dir. it runs when i run it manually. but not with cronjob – Sayan Sen Nov 18 '19 at 13:48
  • @BugFinder no errors. just the file wont upload – Sayan Sen Nov 18 '19 at 13:49
  • @SayanSen If you run something from cronjob it's not assuming that the working dir is where your app is, you have to set the working dir yourself (working dir **!=** directory where the program is located). Can you please post your cronjob line that is supposed to run the application? – Longoon12000 Nov 18 '19 at 13:51
  • @reboot sh /etc/init.d/connectcapture.sh >/home/pi/logs/cronlog 2>&1 – Sayan Sen Nov 18 '19 at 13:55
  • connectcapture.sh contains : cd /home/pi/ConnectCapture mono /home/pi/ConnectCapture/cometconnect.exe exit 0 – Sayan Sen Nov 18 '19 at 13:56
  • That seems fine as you're changing into the correct directory in your connectcapture.sh file. You're not getting any output in your cronlog file? – Longoon12000 Nov 18 '19 at 13:58
  • im not sure why you would have a c# app to run in a cronjob to just to trigger a couple of shell commands. why not just submit the script. However, crontab doesnt have your path variable .. this seems like a normal unix crontab issue – BugFinder Nov 18 '19 at 13:58
  • @BugFinder `crontab doesnt have your path variable` do you mean that it wouldn't find `sh`? @SayanSen can you try doing `@reboot /bin/sh /etc/init.d/connectcapture.sh [...]` instead? – Longoon12000 Nov 18 '19 at 15:08
  • there is often some element of path, but not your user path.. Easiest way crontab a `set > /tmp/x` and compare it .. – BugFinder Nov 18 '19 at 15:11
  • Then how may i do sftp to a host using username and pem file in c#? this sftp will be a part of a bigger program thats why. @BugFinder – Sayan Sen Nov 18 '19 at 19:33
  • @SayanSen did you do the crontab set thing I suggested, have you tried putting full paths in to all items.... – BugFinder Nov 19 '19 at 08:22

1 Answers1

0

I found an answer to this:

ProcessStartInfo p = new ProcessStartInfo
{
    FileName = "sh",
    Arguments = "/home/pi/../upload.sh " + file
};
p.RedirectStandardOutput = true;
p.RedirectStandardError = true;
p.UseShellExecute = false;
p.CreateNoWindow = false;
Process proc1 = new Process();
proc1.StartInfo = p;
proc1.Start();
string result = "";
using (System.IO.StreamReader output = proc1.StandardOutput) 
{
    result = output.ReadToEnd();
}
string error = "";
using (System.IO.StreamReader output = proc1.StandardError)
{
    error = output.ReadToEnd();
}
log.InfoFormat("SFTP result: {0}", result);
log.InfoFormat("{0}", error);

I am capturing the redirected stdout and stderr using streamreader. Now i can see the script upload.sh being run successfully and uploading the file to the remote server as well. I took help from here: How to capture Shell command output in C#?

Sayan Sen
  • 1,654
  • 3
  • 16
  • 26