The next C# method invokes rasdial VPN-Name name *. * means enter password from keyboard. But I want to pass the password to the process from C# application.
public bool connect(UserCredentials userCredentials)
{
// "Dial" to VPN
var psi = new ProcessStartInfo("rasdial")//("cmd")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
//CreateNoWindow = true,
UseShellExecute = false, // Unusable if Streams redirected
Arguments = $"{InterfaceName} {userCredentials.name} *"
};
var proc = new Process {
StartInfo = psi
//,EnableRaisingEvents = true
};
proc.Start();
// PASS PASSWORD VIA STDIN. Does not work. Keyboard input FORCED!
//proc.WaitForInputIdle();
proc.StandardInput.WriteLine(userCredentials.pass);
proc.WaitForExit();
return true;// proc.ExitCode == 0; // Can throws "Not exited yet"
}
Is there a way to pass the password to rasdial?
I can invoke rasdial VPN-Name login passwd. Such method is security lack. DEPRECATED. Any schoolboy can monitor process's args.
One workaround
static Process runProcRasdial()
{
ProcessStartInfo psi = new ProcessStartInfo("cmd") {
RedirectStandardInput=true,
RedirectStandardOutput=false,
UseShellExecute=false
};
var proc = new Process(){
StartInfo = psi,
EnableRaisingEvents = true,
};
proc.Start();
proc.StandardInput.WriteLine("rasdial rapa /disconnect");
proc.StandardInput.WriteLine("rasdial rapa Ivan paswd!");
//proc.Exited += Proc_Exited1;
proc.StandardInput.WriteLine("exit");
proc.WaitForExit();
return proc;
}
If pack paswd! to SecureString if could look like something normal.
Related
I have written a console application to call the AZcopy tool programmatically, but I was not able to call azcopy tool. Can you please redirect me the workable source ? I have downloaded azcopy_windows_amd64_10.12.1 and put azcopy.exe into C:\Windows\System32\azcopy.exe location
static void Main(string[] args)
{
string strCmdText = #"AzCopy.exe sync ""D:\temp"" ""https://myhubforazcopy.blob.core.windows.net/myhubforazcopy1?sp=racwdl&st=2021-09-05T17:19:11Z&se=2021-09-06T01:19:11Z&spr=https&sv=2020-08-04&sr=c&sig=MAraJ0PxqJMDdYuWzrOUEwYda%2BkXEukP%2Fs%3D"" --destination-delete=true";
CallProcess(strCmdText);
}
public static void CallProcess(string strCmdText)
{
//C:\Windows\System32\azcopy.exe
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"C:\Windows\System32\cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = strCmdText,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = #"D:\AzCopy\bin\Debug\"
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
Starting cmd with the program you want to execute as the argument doesn't actually runt that program, you'll need to pass the entire argument (your program) after the /c flag, like so:
string strCmdText = #"/c AzCopy.exe sync ""D:\temp"" ...
Make sure that AzCopy is in your PATH or specify the precise path to it
But it would be better if you just directly run AzCopy instead of starting a command prompt which then starts AzCopy, this would be done like so:
// Notice how the 'azcopy' at the start is gone
var arguments = #"""D:\temp"" ""https://myhubforazcopy.blob ..."
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"C:\Windows\System32\azcopy.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = #"D:\AzCopy\bin\Debug\"
};
Solution :
static void Main(string[] args)
{
string strCmdText = #"/c azcopy.exe sync ""D:\temp"" ""https://myhubforazcopy.blob.core.windows.net/myhubforazcopy1?sp=racwdl&st=2021-09-05T17:19:11Z&se=2021-09-06T01:19:11Z&spr=https&sv=2020-08-04&sr=c&sig=MAraJ0PxqJMDdYuWzrOUEwYda%2BkXEukP%2Fs%3D"" --delete-destination=true";
CallProcess(strCmdText);
}
public static void CallProcess(string strCmdText)
{
var process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
Arguments = strCmdText,
UseShellExecute = false,
CreateNoWindow = false,
WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
};
process.StartInfo = startInfo;
process.Start();
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
}
When I am running relog command from command prompt, I am getting the output without any issue.
Now when I am trying to run command using C# code, I am not getting any output. What could be the issue?
public static string Bash()
{
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
WorkingDirectory = #"C:\blgs",
Arguments = "relog.exe sample.blg",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
I have an exe which I can run in console like below
util.exe argument1
I need to invoke this from a CSharp application which I can do like below
private string Command(string arg)
{
var p = new Process
{
StartInfo =
{
FileName = "util.exe",
Arguments = arg,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
UseShellExecute = false
}
};
var stdOutput = new StringBuilder();
p.OutputDataReceived += (sender, args) => stdOutput.AppendLine(args.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
return stdOutput.ToString();
}
var result = Command(argument1) //call
However there is an issue. util.exe authenticates user using the Windows logged in credentials. In the application I need to execute the commands as a different user(Like below)
util.exe login funcationaluser fuucntioanluserpwd
util.exe argument1
What is the right way for doing this? Can I reuse the process?
Edit: Please note the username and password is specific to util.exe , not a system username/password
I often need to launch external processes so I wrote a convenient method to easily do that. One of these processes needs to trigger UAC in order to ask the user for their permission. I did some research and found that setting the Verb property of the ProcessStartInfo object to runas in addition to setting the UseShellExecute to true should do the trick.
private static void StartProcess(string fileName, string arguments, bool elevated)
{
var start = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
Arguments = arguments,
FileName = fileName
};
if (elevated)
{
start.Verb = "runas";
start.UseShellExecute = true;
}
int exitCode = 0;
using (var proc = new Process { StartInfo = start })
{
proc.Start();
proc.WaitForExit();
exitCode = proc.ExitCode;
}
if (exitCode != 0)
{
var message = string.Format(
"Error {0} executing {1} {2}",
exitCode,
start.FileName,
start.Arguments);
throw new InvalidOperationException(message);
}
}
However, the Verb property is not available in netcore therefore I can't figure out how to achieve the same result. Any suggestion?
I wrote an app in C# (WPF) which takes remote hosts data (using Psexec).
The app requires you to be with high privileges (Administrator).
I have this kind of code in my app:
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "psexec.exe",
Arguments = "\\\\" + ip + " ipconfig",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
if (!proc.WaitForExit(60000))
proc.Kill();
output_error = proc.StandardError.ReadToEnd();
output_stan = proc.StandardOutput.ReadToEnd();
If i'm running the app from Visual Studio (In debug mode), i get an output, but when i'm running the app from the exe file the standard redirected output which is just empty.
Does anyone has a possible solution for this?
*The output which is redirected as an error is a standrad psexec output which says basiclly that the command worked just find (error 0).
Thx.
From MSDN:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// To avoid deadlocks, always read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
In particular note how you shouldn't wait before reading to the end of the stream otherwise you might be getting deadlocks.
I have modified your code to do it this way, and the following works fine for me:
static void Main(string[] args)
{
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "ping.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
string output_error = proc.StandardError.ReadToEnd();
string output_stan = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Trace.TraceInformation(output_stan);
}