0

I am trying to receive a file through Netcat from a Linux based server (Raspberry Pi).

On the sender side (Raspberry Pi) I run a small Python script, that just packs up some files and sends them into the pipeline. This works great, and has been tested a lot.

On the receiver side (Windows 8.1 Pro), I use Netcat to turn the incoming stream into a file. If I do this by hand in cmd.exe, it works great:

nc -l -p <port> > C:\file.gz

My file arrives as planned. However when I try to automate this process on the receiver side with a C# script (.Net 4.5), the file simply doesn't arrive. My code looks like this:

public void StartListeningToNetcat()
        {
            Process ncProcess = new Process();
            ncProcess.StartInfo.UseShellExecute = false;
            ncProcess.StartInfo.CreateNoWindow = false;
            ncProcess.StartInfo.RedirectStandardOutput = true;
            ncProcess.StartInfo.RedirectStandardInput = true;

            ncProcess.StartInfo.FileName = "cmd.exe";
            ncProcess.StartInfo.Arguments = @"C:\...\nc -l -p <port> > C:\file.gz";
            ncProcess.Start();
        }

I am aware, that running nc through cmd.exe is a detour, but calling netcat directly like this:

            ncProcess.StartInfo.FileName = "C:\...\nc.exe";
            ncProcess.StartInfo.Arguments = @"-l -p <port> > C:\file.gz";

...returns the error: ">: forward host lookup failed: h_errno 11001: HOST_NOT_FOUND". Anyway, I wouldn't mind ignoring this error and going for the "inelegant" way to call cmd.exe.

Assuming that it could be a security measure by windows to not allow applications to write incoming files on my hard drive I tried turning of my firewall and using:

            ncProcess.StartInfo.Verb = "runas";

with no success. Could somebody point me in the right direction?

1 Answer 1

1

I was just able to resolve this issue myself:

Instead of executing "cmd.exe" with the arguments to run netcat, or calling Netcat itself, I create a batch file upfront, which I can later call in my C# script.

public void StartListenToNetcat()
{
        string batchPath = CreateNetcatBatchFile();   
        Process.Start(batchPath);
}

public string CreateNetcatBatchFile()
{
        Streamwriter w = new StreamWriter("C:\runNC.bat");
        w.WriteLine("C:\...\nc -l -p 22 > C:\file.gz");
        w.Close();
        return "C:\runNC.bat";
}

Still I don't actually understand the necessity of this detour. Comments for clarifaction would be greatly appreciated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.