transfer files from windows to linux? - c#

I'm trying to connect to a linux machine via pLink and PuTTY to transfer some .txt file. Right now I'm just trying to make the connection work. I have a small window with 2 textboxes (username,linux1) and a password box (pwbox1), and a button that when you click, it should connect you to the linux machine!
Here's my code:
private void button1_Click(object sender, RoutedEventArgs e)
{
string user = username.Text;
string passw = pwbox1.Password;
string linuxHst = linux1.Text;
ProcessStartInfo psi = new ProcessStartInfo(#"C:\Program Files (x86)\PuTTY\plink.exe", user + "#" + linuxHst + " -pw " + passw);
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.UseShellExecute = false;
psi.CreateNoWindow = false;
Process process = Process.Start(psi);
process.WaitForExit(5000);
}
The problem is that when I try this with the console application it works, without the textboxes! but I need it to work with WPF.
Can anyone please tell me what I'm doing wrong? am I missing something?

Related

Execute cmd command on windows server with web app [duplicate]

This question already has answers here:
Restart Server from ASP.NET application when AppPool is ran under LocalSystem or LocalService account
(2 answers)
Shutdown or Restart Machine In C# and ASP.NET
(1 answer)
Closed 3 years ago.
I have a web app c # hosted on IIS on a computer with windows server 2008, I ran a command on a windows server cmd through C#, but it doesn't work, I tried it locally on my computer and the command works, I don't know why it doesn't work on the computer with windows server, I use this source code,I put a log but doesn't throw any error.
protected void btnReboot_Click(object sender, EventArgs e)
{
try
{
//StartShutDown("-l");
StartShutDown("-f -r -t 5");
Log2("MNS OK");
}
catch (Exception ex)
{
Log2("MNS ERROR " + ex.ToString());
}
}
private static void StartShutDown(string param)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "cmd";
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.Arguments = "/C shutdown " + param;
Process.Start(proc);
}
You can actually capture the error output from the process that was launched by redirecting the standard error. An example would be like this:
private static void StartShutDown(string param)
{
Process p = new Process();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true; // You need to set this
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C shutdown " + param;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
string stdoutx = p.StandardOutput.ReadToEnd();
string stderrx = p.StandardError.ReadToEnd(); // here is where you get the error output string
p.WaitForExit();
Console.WriteLine("Exit code : {0}", p.ExitCode);
Console.WriteLine("Stdout : {0}", stdoutx);
Console.WriteLine("Stderr : {0}", stderrx);
}
Once you have the Stderr you can check its contents and, if it's not empty then you know an error occurred.

Command won't run in Command Prompt

When users click on a button, I want it to run the logon script(launching from server), but each computer in different servers, so I get the server name. But the netlogon.StartInfo.Arguments = slnres + #"/c \netlogon\logon.cmd"; line is not working as it should be. It should run the logon.cmd on the PC(mapping network drivers, printers, etc), and then the CMD should close.
private void MapNetwork_Click(object sender, EventArgs e)
{
Process sln = new Process();
sln.StartInfo.UseShellExecute = false;
sln.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sln.StartInfo.FileName = "cmd.exe";
sln.StartInfo.Arguments = "/c echo %logonserver%";
sln.StartInfo.RedirectStandardOutput = true;
sln.Start();
string slnres = sln.StandardOutput.ReadToEnd();
label1.Text = slnres;
Process netlogon = new Process();
netlogon.StartInfo.UseShellExecute = false;
netlogon.StartInfo.FileName = "cmd.exe";
netlogon.StartInfo.Arguments = slnres + #"/c \netlogon\logon.cmd";
netlogon.Start();
}
A couple things:
You don't need to run a command prompt to get an environment variable. You can use Environment.GetEnvironmentVariable.
Your Arguments property for your call to logon.cmd is being constructed into this:
\\myserver/c \netlogon\logon.cmd
When I think you want this:
/c \\myserver\netlogon\logon.cmd
So make sure you put slnres at the right place in your string. Your code should look like this:
private void MapNetwork_Click(object sender, EventArgs e)
{
string slnres = Environment.GetEnvironmentVariable("logonserver");
label1.Text = slnres;
Process netlogon = new Process();
netlogon.StartInfo.UseShellExecute = false;
netlogon.StartInfo.FileName = "cmd.exe";
netlogon.StartInfo.Arguments = "/c " + slnres + #"\netlogon\logon.cmd";
netlogon.Start();
}
i am a little confused about your question and i am not rly sure if i understand you correctly. some time ago i made a program where i had to run few powershell commands, so i made a class for it. redirected to your button it would look like that:
(and remember you need the fqdn to your file location => Reading File From Network Location)
using System.Diagnostics;
//class lvl scope vars
string output;
string ErrorOutput;
private void MapNetwork_Click(object sender, EventArgs e)
{
//define process arguments
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"FQDN path to your file on the server; exit";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
//start process
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
//outpunt handling
if (string.IsNullOrEmpty(ErrorOutput))
{
return output;
}
else
{
return ErrorOutput;
}
}
first of all i would check if your application is able to open the file one the shared network location. (server available? access rights to server? serer mapped?)
after that you can check if he is able to start the file locally. (does it need admin rights to run the *.cmd, *.bat file)
now you can check if your application runs it correctly.

having trouble running a batch file from my C# windows application

I am trying to run a batch file that runs another batch file from my windows application.
when i click the button, everything is frozen and i do not know why.
The nested batch file was created to build html files from RST files
I have a feeling that I am in a dead lock situation.
Thank you so much guys for your help.
I have attached a screen shot of my form and included my code as well.
private void buttonMakeHtml_Click(object sender, EventArgs e)
{
ProcessStartInfo processInfo =
new ProcessStartInfo("cmd.exe", "/c" + "\"C:\\ReadTheDocs\\makeHtml.bat\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
textBoxOutPut.Text = output;
textBoxError.Text = error;
}
Make html Screen

Execute commands from a file through putty in C#

I have below code for run a putty with authentication from C#
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo cmd = new ProcessStartInfo();
cmd.FileName = "putty.exe";
cmd.UseShellExecute = false;
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.Arguments = "-ssh " + username + host+ " -pw " + upassword;
using (Process process = Process.Start(cmd))
{
process.WaitForExit();
}
}
The above code is launching putty and authenticating the user and password.
I want to execute some commands after launching the putty. I used below code
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo cmd = new ProcessStartInfo();
cmd.FileName = "putty.exe";
cmd.UseShellExecute = false;
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.Arguments = "-ssh " + username + host+ " -pw " + upassword+ "-m "+path;;
using (Process process = Process.Start(cmd))
{
process.WaitForExit();
}
}
The path variable contains the location of the file and file contain below text in it
ssh webhostname
While executing the above code, putty is launched but during authentication putty terminated with message "Unauthorized access to this machine is prohibited"
Is there anyway we can run the commands in putty from a file?

Running a python script on C# not working

I have this code that I'm trying to run a Python script. It works correctly when I do it manually via the command prompt but if I try to do it via a button click in a C# Windows form it doesn't work.
private void btnGenerateAndrowarn_Click(object sender, EventArgs e) {
string[] filePaths = Directory.GetFiles(#"C:\Users\User1\Desktop\Android Tools\androwarn\Samples", "*.apk");
foreach (string fileName in filePaths) {
ProcessStartInfo startInfo;
Process process;
string directory = #"C:\Users\User1\Desktop\Android Tools\androwarn\";
string script = "androwarn.py";
startInfo = new ProcessStartInfo("python");
startInfo.WorkingDirectory = directory;
startInfo.Arguments = directory + script + " -i " + fileName + " -r html -v 3";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
process = new Process();
process.StartInfo = startInfo;
process.Start();
}
}
Most likely is that python is not in your %PATH%.

Categories