You can process the openvpn
command like this:
var scripting = virtualssh.StartScripting();
scripting.DetectPrompt();
scripting.SendCommand("sudo openvpn ~/vpn/file.ovpn");
var completed = ScriptEvent.FromString("Initialization Sequence Completed");
var response = scripting.ReadUntil(completed, ScriptEvent.Prompt, ScriptEvent.Timeout);
if (scripting.LastResult.IsEventMatched(completed))
{
Console.WriteLine("Operation completed successfully. Parsing IP address from:");
Console.WriteLine(response);
IPAddress ipAddress;
if (!TryParseIpAddressFromResponse(response, out ipAddress))
{
Console.WriteLine("Cannot parse IP address.");
}
else
{
Console.WriteLine("Obtained IP address is: {0}", ipAddress);
}
}
else
{
Console.WriteLine("Operation failed:");
Console.WriteLine(response);
}
Then parse the IP address from received response
as you like. It can be done fore example like this (but I am not sure how you want to handle the 10.10.16.48/23
value):
private static bool TryParseIpAddressFromResponse(string response, out IPAddress ipAddress)
{
string find = "net_addr_v4_add: ";
int idx1 = response.IndexOf(find) + find.Length;
int idx2 = response.IndexOf('/', idx1);
if (idx1 < find.Length || idx2 < idx1)
{
ipAddress = null;
return false;
}
return IPAddress.TryParse(response.Substring(idx1, idx2 - idx1), out ipAddress);
}