I have configured password less auth for my git on-premises. Now I want to first go to the ssh dir in local machine and then access the git server from there all using cmd. I am doing this whole thing using C# code. Below is my code.
ProcessStartInfo processStartInfo = null;
Process process = null;
Process main = null;
ProcessStartInfo processMainInfo = null;
processStartInfo = new ProcessStartInfo("cmd.exe","ssh swx#192.168.12.232");
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.WorkingDirectory = #"C:\Users\xyz\.ssh";
process = new Process();
process.StartInfo = processStartInfo;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("/c cd git_home && git log");
string returnValue = process.StandardOutput.ReadToEnd();
I know that /c first executes the and then terminates. I tried to use && for the whole command like this :
> ssh swx#192.168.12.232 && cd git_home && git log
but in this case only ssh swx#192.168.12.232 this part is getting executed and the other cd git_home && git log are not.
Can anyone help me here?
My main goal here is to execute two commands one after another 1st :
> ssh swx#192.168.12.232
this should get executed it will give me access to the server form the cmd and after that
> cd git_home && git log
which will get me to the git repo and give me all the logs as output in string returnValue.
or if there is some more efficient way pls suggest.
Edit
I tried to run the commands one after another below is the code:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(" C:");
sw.WriteLine(#"cd C:\Users\xyz\.ssh");
sw.WriteLine(" ssh swx#192.168.12.232");
sw.WriteLine("cd git_home");
sw.WriteLine("git log");
}
string aaa = p.StandardOutput.ReadToEnd();
}
But the string aaa returns :
Microsoft Windows [Version 10.0.19042.1023]
(c) Microsoft Corporation. All rights reserved.
D:\Kovair Projects\Adapters\KOVAIR_Git_KovairGitAdapter_2.20 - EventService-BAK\Source\TestAPP\bin> C:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE>cd C:\Users\xyz\.ssh
C:\Users\xyz\.ssh> ssh swx#192.168.12.232
C:\Users\xyz\.ssh>cd git_home
C:\Users\xyz\.ssh>git log
C:\Users\xyz\.ssh>
what can I do so that I can execute the last 2 commands in the linux server?
swx#192.168.12.232 "cd git_home ; git log" execute this command this might help.
Related
So I need to make a Angular build (ng build --prod --aot-false) for a C# project(ashx page) with the Angular folder as a part of the project.
What I have tried right now is make a bat file inside the Angular folder outside the "src" folder as follows:
test.bat:
mkdir a
ng build --prod --aot=false
mkdir b
When I execute the commands the directories "a" & "b" are created instantly but the build is never made.
To execute the process I use:
filehandler.ashx:
System.Environment.CurrentDirectory =
context.Server.MapPath("~/ZipFiles/AngularProject_test/AngularProject/");
System.Diagnostics.Process.Start("test.bat");
This thread is a bit old but maybe this will help someone else. The following snippet will run ng build from C#:
// this will be the path to your app
var workingDirectory = #"C:apps\angular-hello-world";
// location of ng command
var command = #"C:\Users\Owner\AppData\Roaming\npm\ng.cmd";
// any arguments you want to pass to ng
var arguments = "build --prod --base-href /helloworld/ --output-path=prod";
var process = new Process();
var currentDirectory = Environment.CurrentDirectory;
try
{
Environment.CurrentDirectory = workingDirectory;
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = workingDirectory;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
// wait for the app to run and grab any output/errors generated
process.WaitForExit();
var output = process.StandardOutput.ReadToEnd();
var errors = process.StandardError.ReadToEnd();
Console.WriteLine("Output: " + output);
Console.WriteLine("Errors: " + errors);
}
finally
{
Environment.CurrentDirectory = currentDirectory;
}
I tried to get the output of passed argument in the cmd.exe via c# code. But when the code string outw = cmd.StandardOutput.ReadToEnd().ToString(); executing takes more and more time. Here my command prompt arguments work perfectly.
Here is my code
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("git init && git remote add gitlab https://gitlab.company.com/test2.git && git push gitlab --delete branchname");
cmd.StandardInput.WriteLine("git remote remove gitlab");
string outw = cmd.StandardOutput.ReadToEnd().ToString();//Here my code strucks
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Where I made the mistake? I waited for more than 30 min but I did not get the output in the string outw. How could I get the output test as a string in string outw?
I have a cygwin bash scripts that works:
#!/bin/sh
cd myc
cp Stats.txt Stats.txt.cpy;
cat Stats.txt.cpy | sort -n -k1 | gawk '{sum+=$2; print $0,sum}' > Stats.txt
I want to "call" it from C#:
string cmd="myscript.sh";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(#"C:\Cygwin\bin\bash.exe");
psi.Arguments = cmd;
psi.WorkingDirectory = "C:\\cygwin\\home\\Moon";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
proc.StartInfo = psi;
proc.Start();
string error = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
this.textBox1.AppendText(error);
this.textBox1.AppendText(output);
It works fine from cygwin terminal BUT from C# I get:
Input file specified two times.
I suspect this is a pipes thing - can anyone help?
It was a path issue.
You need to set path in the script - otherwise it uses a different "non-cygwin" path and gets wrong commands.
I am trying to open Weka from cmd line, using C#. This is the code that I'm using. It's giving me an error for Weka.Start() line, and the error is : Win32 exception was unhandled. System cannot find the file specified. Please help me out. Thanks
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(#"C:\Program Files\Weka- 3-6\java -Xmx1536m -jar weka.jar");
WekaStartInfo.UseShellExecute = false;
WekaStartInfo.RedirectStandardOutput = true;
WekaStartInfo.RedirectStandardError = true;
WekaStartInfo.CreateNoWindow = false;
Process Weka = new Process();
Weka.StartInfo = WekaStartInfo;
Weka.Start();
string output = Weka.StandardOutput.ReadToEnd();
Weka.WaitForExit();
There are two options to start WEKA from a
C# application.
In the WEKA install directory there is a
batch file called RunWeka.bat. To start WEKA
using this batch file use the following
code:
ProcessStartInfo wekaStartInfo =
new ProcessStartInfo(#"c:\Program Files\Weka-3-6\runweka.bat", "default");
wekaStartInfo.WorkingDirectory = #"c:\Program Files\Weka-3-6";
wekaStartInfo.UseShellExecute = false;
wekaStartInfo.RedirectStandardOutput = true;
wekaStartInfo.RedirectStandardError = true;
wekaStartInfo.CreateNoWindow = false;
using(Process weka = new Process())
{
weka.StartInfo = wekaStartInfo;
weka.Start();
}
To start WEKA without using the batch file
use the following code:
ProcessStartInfo wekaStartInfo =
new ProcessStartInfo(#"javaw", #"-classpath . RunWeka -i .\RunWeka.ini -w .\weka.jar -c default");
wekaStartInfo.WorkingDirectory = #"c:\Program Files\Weka-3-6";
wekaStartInfo.UseShellExecute = false;
wekaStartInfo.RedirectStandardOutput = true;
wekaStartInfo.RedirectStandardError = true;
wekaStartInfo.CreateNoWindow = false;
using(Process weka = new Process())
{
weka.StartInfo = wekaStartInfo;
weka.Start();
}
In both cases you have to set the working directory.
You've probably specified incorrect or inexistent location for your process based on the error description. Check that the path specified in ProcessStartInfo is correct.
Maybe, there are unnecessary spaces in the declaration here:
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(#"C:\Program Files\Weka-3-6\java -Xmx1536m -jar weka.jar");
In the constructor of ProcessStartInfo you must either enter just the application name, or specify the arguments separate;
ProcessStartInfo WekaStartInfo = new ProcessStartInfo(
#"C:\Program Files\Weka-3-6\java.exe",
#"-Xmx1536m -jar weka.jar");
All I am trying to do is send a command that opens a model with the program.exe
Supposed to be super simple!
Ex:
"C:\Program Files (x86)\River Logic\Enterprise Optimizer 7.4 Developer\EO74.exe" "C:\PauloXLS\Constraint Sets_1.cor"
The line above works well if pasted on the command prompt window.
However, when trying to pass the same exact string on my code it gets stuck on C:\Program
string EXE = "\"" + #tbx_base_exe.Text.Trim() + "\"";
string Model = "\"" + #mdl_path.Trim()+ "\"";
string ExeModel = EXE + " " + Model;
MessageBox.Show(ExeModel);
ExecuteCommand(ExeModel);
ExeModel is showing te following line on Visual Studio:
"\"C:\\Program Files (x86)\\River Logic\\Enterprise Optimizer 7.4 Developer\\EO74.exe\" \"C:\\PauloXLS\\Constraint Sets_1.cor\""
To me looks like it is the string I need to send in to the following method:
public int ExecuteCommand(string Command)
{
int ExitCode;
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
Process.WaitForExit();
ExitCode = Process.ExitCode;
Process.Close();
return ExitCode;
}
Things I've tried:
Pass only one command at a time (works as expected), but not an option since the model file will open with another version of the software.
Tried to Trim
Tried with # with \"
Can anyone see any obvious mistake? Thanks.
It's pretty straightforward. You just create a command line object then write to it, then to execute it you read back from it using SR.ReadToEnd():
private string GETCMD()
{
string tempGETCMD = null;
Process CMDprocess = new Process();
System.Diagnostics.ProcessStartInfo StartInfo = new System.Diagnostics.ProcessStartInfo();
StartInfo.FileName = "cmd"; //starts cmd window
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.CreateNoWindow = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //required to redirect
CMDprocess.StartInfo = StartInfo;
CMDprocess.Start();
System.IO.StreamReader SR = CMDprocess.StandardOutput;
System.IO.StreamWriter SW = CMDprocess.StandardInput;
SW.WriteLine("#echo on");
SW.WriteLine("cd\\"); //the command you wish to run.....
SW.WriteLine("cd C:\\Program Files");
//insert your other commands here
SW.WriteLine("exit"); //exits command prompt window
tempGETCMD = SR.ReadToEnd(); //returns results of the command window
SW.Close();
SR.Close();
return tempGETCMD;
}
Why are you opening a command prompt (cmd.exe)? Just pass the name of the executable.