I'm using Renci SshNet to access a file via SSH from a C# program that I'm writing.
Using data from my app I create a line in a text file as below.
sshclient.Connect();
string data1 = textBox1.Text + ":";
string data2 = textBox2.Text + ":";
string ip = "${SSH_CLIENT%% *}" + ":";
string data = ("\necho " + ip + data1 + data2 " >> file.txt");
var cmd = sshclient.CreateCommand(data);
cmd.Execute();
sshclient.Disconnect();
The above code results in a line being added to the text file when I start my app as below, which is exactly what I want.
123.456.789.000:data1:data2:
I would like to delete the line from the text file when my app closes, but so far I'm not having much luck.
Below is what I've tried...
sshclient.Connect();
textBox3.Text = new WebClient().DownloadString("http://icanhazip.com");
string ip = textBox3.Text;
string del = ("sed -i '/" + ip + "/ d' file.txt");
var cmd = sshclient.CreateCommand(del);
cmd.Execute();
sshclient.Disconnect();
The textbox does show the ip correctly when it's read from the web and added to the textbox.
But if I use the command above, the line is not deleted unless I manually type the ip into the textbox.
I have also tried
string del = ("sed -i '/${SSH_CLIENT%% *}/ d' file.txt");
What am I doing wrong?
It seems the IP had a space at the end which I couldn't see in the text box.
Related
I need to execute Postman collection from within my C# method.
But instead of the data file, I need to pass the data directly from the method output (as List).
Here is my code:
public StringBuilder RunPostmanCall(string collectionPath, string executionFolder, string environmentPath, List<string> inputFilePath = null)
{
StringBuilder runOutputBuilder = new StringBuilder();
string runOutput = null;
ProcessStartInfo psiNpm = new ProcessStartInfo
{
FileName = "cmd",
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false
};
Process pNpmRun = Process.Start(psiNpm);
pNpmRun.OutputDataReceived += (sender, a) => runOutputBuilder.AppendLine(a.Data);
Console.WriteLine(" - Install Newman ...");
pNpmRun.StandardInput.WriteLine($"npm install -g newman");
Console.WriteLine(" - Execute Postman Script ...");
string value = $"newman run " +
$"\"" + collectionPath + "\" " +
$"--folder \"" + executionFolder + "\" " +
$"--environment \"" + environmentPath + "\" " +
$"-d \"" + inputFilePath + "\" " +
$"--disable-unicode";
pNpmRun.StandardInput.WriteLine(value);
pNpmRun.BeginOutputReadLine();
pNpmRun.StandardInput.WriteLine("exit 0");
I'm getting the following error:
bin\Debug>newman run "../../api/postman_audit.json" --folder "SearchIndex" --environment "../../api/postman_environment.json" -d "System.Collections.Generic.List`1[System.String]" --disable-unicode
I could save the output into the file, and then just use that file location in the command-line. But I would like to avoid creating a file, and read data directly from the memory.
unfortunately with -d you can only refer to a file in your filesystem.
If you are not willing to write this data directly into a file, i would suggest to set the values directly as a global variable from commandline.
Try to add this parameter to your newman run command
--global-var key=value
You can add your data into a string and add it as a global variable. You can parse it in your pre-request or test-script normally.
I would like to use gammu to send text messages with address and message, but I have a problem with the gammu parameters. If I start only the program it runs (string cmd1 = "c:\\G133\\bin\\gammu.exe ";). After adding parameters it gives this failure:
System.ComponentModel.Win32Exception' occurred in System.dll
Additional information: The system cannot find the file specified:
Code:
string[] sms = File.ReadAllLines(#"C:\\temp\\test.txt");
string address = sms[0];
string message = sms[1];
string cmd1 = #"C:\G133\bin\gammu.exe --sendsms TEXT" + " " +
"\"" + address + "\" -text " + " " + "\"" + message + "\"";
System.Diagnostics.Process.Start(cmd1);
Can anyone help me? Thanks in advance.
The output looks well:
Console.WriteLine(cmd1); - result
C:\G133\bin\gammu.exe --sendsms TEXT +12121234567 -text "Hello"
You need to call the overload of Start method which takes two parameters:
First one: the file to run;
Second one: the parameters
And it will looks like:
string app = #"path\to\your\target\app";
string prms = "your parameters";
System.Diagnostics.Process.Start(app, prms);
You should split the application and the arguments:
Process.Start(#"C:\G133\bin\gammu.exe", "--sendsms TEXT +12121234567 -text \"Hello\"");
I am trying to delete and rebuild an encrypted password file. I am able to create the file and add usernames and encrypted passwords if I run the commands manually on the server within a command window. But I get nothing if I try to run this code from within my C# web api controller. The delete line works, but the first Process.Start line fails, and it never makes it to my while loop.
System.IO.StreamReader pass2 = new System.IO.StreamReader(pass);
File.Delete("C:\\inetpub\\wwwroot\\password\passMD5.txt");
Process.Start("C:\\inetpub\\htpasswd.exe -bc C:\\inetpub\\wwwroot\\password\\passMD5.txt sm88555 sm88999");
while ((line = pass2.ReadLine()) != null)
{
un = line.Substring(0, 6);
pw = line.Substring(7, 6);
Process.Start("C:\\inetpub\\htpasswd.exe -b C:\\inetpub\\wwwroot\\password\\passMD5.txt " + un + " " + pw);
}
pass2.Close();
When I copy paste the javaw.exe -arguments to console it works but, when I launch it like this it doesn't work.
string directory = "C:\\Users\\Can\\AppData\\Roaming\\.minecraft";
string java = #"C:\windows\system32\javaw.exe";
string javaLocation = "C:\\Program Files\\Java\\jre7\\bin\\javaw.exe";
string RAM = "1G";
string username = "namehere";
string token = "--session token:"+tokenGenerated;
string version = "1.6.2";
string launch = "-Xmx" + RAM + " -Djava.library.path={0}\\versions\\1.6.2\\1.6.2-natives-7453523379463 -cp {0}\\libraries\\net\\sf\\jopt-simple\\jopt-simple\\4.5\\jopt-simple-4.5.jar;{0}\\libraries\\com\\paulscode\\codecjorbis\\20101023\\codecjorbis-20101023.jar;{0}\\libraries\\com\\paulscode\\codecwav\\20101023\\codecwav-20101023.jar;{0}\\libraries\\com\\paulscode\\libraryjavasound\\20101123\\libraryjavasound-20101123.jar;{0}\\libraries\\com\\paulscode\\librarylwjglopenal\\20100824\\librarylwjglopenal-20100824.jar;{0}\\libraries\\com\\paulscode\\soundsystem\\20120107\\soundsystem-20120107.jar;{0}\\libraries\\argo\\argo\\2.25_fixed\\argo-2.25_fixed.jar;{0}\\libraries\\org\\bouncycastle\\bcprov-jdk15on\\1.47\\bcprov-jdk15on-1.47.jar;{0}\\libraries\\com\\google\\guava\\guava\\14.0\\guava-14.0.jar;{0}\\libraries\\org\\apache\\commons\\commons-lang3\\3.1\\commons-lang3-3.1.jar;{0}\\libraries\\commons-io\\commons-io\\2.4\\commons-io-2.4.jar;{0}\\libraries\\net\\java\\jinput\\jinput\\2.0.5\\jinput-2.0.5.jar;{0}\\libraries\\net\\java\\jutils\\jutils\\1.0.0\\jutils-1.0.0.jar;{0}\\libraries\\com\\google\\code\\gson\\gson\\2.2.2\\gson-2.2.2.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl\\2.9.0\\lwjgl-2.9.0.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl_util\\2.9.0\\lwjgl_util-2.9.0.jar;{0}\\versions\\1.6.2\\1.6.2.jar net.minecraft.client.main.Main --username " + username + " " + token + " --version " + version + " --gameDir {0} --assetsDir {0}\\assets";
launch = String.Format(launch, directory);
string text = launch;
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
Directory.SetCurrentDirectory(#"C:\windows\system32\");
Process.Start("javaw.exe",
Path.Combine(launch));
What am I doing wrong?
Why do you need to call Path.Combine if your whole path is in one string?
Assuming your javaw.exe is actually in C:\windows\system32\, Process.Start("java.exe", launch); should work as intended.
Source - Path on MSDN: http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Just use Java's real location instead of "javaw" like "c:\programfiles\Java\jre7\bin\javaw.exe"
Env: .NET 3.5 Visual Studio 2008 SP1, on Win XP SP3, Python 2.7. Corporate image, no admin rights.
In C# 3.5, I want to pass a parameter to a command-line program, and this parameter contains newlines. It works in Python 2.7 but not in C#.
When body contains newline, c# truncates the result, but python passes it correctly.
Python code
cmd = self.app_path + ' email -Subject "' + subject + '" -From "' + address_from + '" -To "' + address_to +'" -Body "' + body +'"'
cmd_result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
C# code:
string Command = "<path to executable>";
string arguments = " email -From " + FromAddress + " -To " + ToAddress + " -Subject \"" + SubjectLine + "\" -Body \"" + emailBody + "\" ";
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(Command, arguments);
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
start.CreateNoWindow = false;
start.UseShellExecute = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);
Any idea?
Update: body in the python example and emailBody in the c# example contain the same string, as strings are represented in each language.
Update: noticed the command wasn't terminated correctly in the python code. I added + '"' at the end of line 2. The code ran as before.
Also, as you can tell, the app called sends the body (emailBody) parameter content as an email body.
Sorry but I think it is the process you call that does not take the arguments correctly; it is not the C# that is involved, and as a proof of that:
Create a new windows form application
Make sure that the Main signature looks like this:
static void Main(string[] args)
Make sure that the call of your Main Form (named Form1) looks like this:
if (args==null)
{
Application.Run(new Form1());
}
else
{
Application.Run(new Form1(args));
}
Add a textbox to your form, with the multiline property set to true
Add a button to your form
Run the application once
Then Put this in the code of your button1_Click function (replace TheNameOfTheCurrentApp):
string Command = "TheNameOfTheCurrentApp";
string arguments = textBox1.Text;
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(Command, arguments);
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
start.CreateNoWindow = true;
start.UseShellExecute = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);
Add this constructor to your form:
public Form1(string[] args)
{
InitializeComponent();
textBox1.Text = string.Join(" ", args);
}
Then run your app, insert muliple lines in your textbox and click the button. The same app will appear with the textbox correctly filled with the line breaks