Need help comparing extracted data and outputting to file - c#

New to C#, and having trouble finding ways to compare data so far collected from conf file, and outputting it to either text or CSV.
I so far have the skeleton of data extraction code from said conf file, however as I'm new to C# and coding overall, I'm having trouble understanding how to reference that data or compare it.
So far have tried File.WriteAllLiness and defining a variable, but not sure which element to parse, or at which point in the code I should introduce it.
Nothing to hide really, so here's the full output so far:
namespace CompareVal
{
class Program
{
static void Main(string[] args)
{
var lines = File.ReadAllLines(#"D:\*\*\Cleanup\Script Project\Test-Raw-Conf.txt");
var ipAddresses = GetIPAddresses(lines);
var routes = GetRoutes(lines);
var ipRules = GetIPRules(lines);
Console.WriteLine ();
}
static Dictionary<string, string[]> GetIPAddresses(string[] lines)
{
var result = new Dictionary<string, string[]>();
foreach (var line in lines)
{
if (!line.StartsWith("add IPAddress"))
{
continue;
}
Match match;
if (line.Contains("Address=\""))
{
match = Regex.Match(line, "add IPAddress (.*?) Address=\"(.*?)\"");
}
else
{
match = Regex.Match(line, "add IPAddress (.*?) Address=(.*?)$");
}
var name = match.Groups[1].Value;
var value = match.Groups[2].Value;
var items = value.Replace(" ", "").Split(',');
result.Add(name, items);
}
return result;
}
static List<Route> GetRoutes(string[] lines)
{
var result = new List<Route>();
string currentRoutingTable = null;
foreach (var line in lines)
{
if (line.StartsWith("cc RoutingTable"))
{
currentRoutingTable = line.Split(' ')[2].Trim();
}
if (line == "cc .." && currentRoutingTable != null)
{
currentRoutingTable = null;
}
if (line.StartsWith(" add Route"))
{
var #interface = Regex.Match(line, "Interface=(.*?) ").Groups[1].Value;
var gateway = Regex.Match(line, "Gateway=(.*?) ").Groups[1].Value;
var network = Regex.Match(line, "Network=(.*?) ").Groups[1].Value;
result.Add(new Route
{
RoutingTable = currentRoutingTable,
Interface = #interface,
Gateway = gateway,
Network = network
});
}
}
return result;
}
static List<IPRule> GetIPRules(string[] lines)
{
var result = new List<IPRule>();
string currentIPRuleSet = null;
foreach (var line in lines)
{
if (line.StartsWith("cc IPRuleSet"))
{
currentIPRuleSet = line.Split(' ')[2].Trim();
}
if (line == "cc .." && currentIPRuleSet != null)
{
currentIPRuleSet = null;
}
if (line.StartsWith(" add IPRule"))
{
var rule = new IPRule
{
IPRuleSet = currentIPRuleSet,
SourceInterface = GetProperty(line, "SourceInterface"),
DestinationInterface = GetProperty(line, "DestinationInterface"),
};
if (line.Contains("SourceNetwork=\""))
{
rule.SourceNetwork = GetQuotedProperty(line, "SourceNetwork").Replace(" ", "").Split(',');
}
else
{
rule.SourceNetwork = GetProperty(line, "SourceNetwork").Replace(" ", "").Split(',');
}
if (line.Contains("DestinationNetwork=\""))
{
rule.DestinationNetwork = GetQuotedProperty(line, "DestinationNetwork").Replace(" ", "").Split(',');
}
else
{
rule.DestinationNetwork = GetProperty(line, "DestinationNetwork").Replace(" ", "").Split(',');
}
result.Add(rule);
}
}
return result;
}
static string GetProperty(string input, string propertyName)
{
return Regex.Match(input, string.Format("{0}=(.*?) ", propertyName)).Groups[1].Value;
}
static string GetQuotedProperty(string input, string propertyName)
{
return Regex.Match(input, string.Format("{0}=\"(.*?)\" ", propertyName)).Groups[1].Value;
}
class Route
{
public string RoutingTable;
public string Interface;
public string Gateway;
public string Network;
}
class IPRule
{
public string IPRuleSet;
public string SourceInterface;
public string DestinationInterface;
public string[] SourceNetwork;
public string[] DestinationNetwork;
}
}
}
I'm hoping to compare values gathered by IPRule, Route and IPAddress classes, and have a method of outputting each associated value in a list. Each IPAddress is contains a unique string name, but can use any numerical IP address. The idea is to determine when the same IP has been used multiple times, regardless of IPAddress string name, and then compare this to routes, and flag when they are used in IPRules.
For reference, here are some samples of source data:
For IPAddresses, they can be formed in 1 of 2 ways - as a direct IP definition, or as a reference to another IPAddress object (or multi-reference):
add IPAddress Test Address=192.168.1.0/24
IPAddress referencing multiple other IPAddresses:
add IPAddress TestGroup Address="Test1, Test2, Test3"
For routes:
add Route Interface=if5 Gateway=if5_gw Network=Test ProxyARPInterfaces=""
And for IPRules:
add IPRule SourceInterface=if5 DestinationInterface=if3 SourceNetwork=Test1 DestinationNetwork=Test2 Service=dns-all Action=Allow
The above definitions will always follow the same pattern, so the data extraction code has been constructed to expect prefixes to each element, and sort them into their own dictionary or list.

Related

String replace with recursive dictionary values

I have config for all the root api urls in app.config file that is loaded into dictionary of key and value pair.
Dictionary<string, string> variables = new Dictionary<string, string>()
{
{ "clientRefUrl", "[clientBaseUrl]/RealtimeReferenceData"},
{ "clientRealtimeUrl", "[clientBaseUrl]/RealtimeClinicalData"},
{ "localApiUrl", "LocalApi/Generic"},
{ "integrationRootFolder", "C:\\LocalServer\\Integration"},
{ "clientBaseUrl", "https://company.com/api"},
{ "clientAuthBaseUrl", "https://auth.company.com/api"}
};
I have an api url that comes from the config file like <endpoint name="saveuser" address="[clientRefUrl]/SaveUser" />.
I want to build that url in c# code as https://company.com/api/RealtimeReferenceData/SaveUser.
I am able to do this using the following method but the problem is that client has to make sure they don't move the clientBaseUrl to the top of the list. Or, any dependent key to the top of the list.
public static string EvaluateStringWithVariables(string strExpr)
{
Dictionary<string, string> variables = new Dictionary<string, string>()
{
{ "clientRefUrl", "[clientBaseUrl]/RealtimeReferenceData"},
{ "clientRealtimeUrl", "[clientBaseUrl]/RealtimeClinicalData"},
{ "localApiUrl", "LocalApi/Generic"},
{ "integrationRootFolder", "C:\\LocalServer\\Integration"},
{ "clientBaseUrl", "https://company.com/api"},
{ "clientAuthBaseUrl", "https://auth.company.com/api"}
};
foreach (string variable in variables.Keys)
{
var pattern = #"\[" + variable + #"\]";
strExpr = Regex.Replace(strExpr, pattern, variables[variable]);
}
return strExpr;
}
Is there a better way to do the same without any restriction. I tried another solution that uses regex and recursion:
public static string EvaluateStringWithVariables(string strExpr)
{
Dictionary<string, string> variables = new Dictionary<string, string>()
{
{ "clientRefUrl", "[clientBaseUrl]/RealtimeReferenceData"},
{ "clientRealtimeUrl", "[clientBaseUrl]/RealtimeClinicalData"},
{ "localApiUrl", "LocalApi/Generic"},
{ "integrationRootFolder", "C:\\LocalServer\\Integration"},
{ "clientBaseUrl", "https://company.com/api"},
{ "clientAuthBaseUrl", "https://auth.company.com/api"}
};
Regex regEx = new Regex(#"\[(\w+)\]", RegexOptions.Compiled);
strExpr = regEx.Replace(strExpr, match =>
{
string val = String.Empty;
if (variables.TryGetValue(match.Groups[1].Value, out val))
{
return val;
}
return match.Value;
});
Match rmatch = regEx.Match(strExpr);
if (rmatch.Success)
{
return EvaluateStringWithVariables(strExpr);
}
return strExpr;
}
But, recursion didn't go well when I had to evaluate a string like:
strExpr = "[integrationRootFolder]\\myfolder\\[msg.ClientId]\\In\\[personid]_[tabid]_[documentname]_[msg.ClientId].pdf"; which keep on trying to evaluate other variables that is not part of dictionary.
I think your regex solution was quite close, you just need to check to see if you found any values in the dictionary when you did the replace and if you did then call it recursively.
public static string EvaluateStringWithVariables(string strExpr)
{
Dictionary<string, string> variables = new Dictionary<string, string>()
{
{ "clientRefUrl", "[clientBaseUrl]/RealtimeReferenceData"},
{ "clientRealtimeUrl", "[clientBaseUrl]/RealtimeClinicalData"},
{ "localApiUrl", "LocalApi/Generic"},
{ "integrationRootFolder", "C:\\LocalServer\\Integration"},
{ "clientBaseUrl", "https://company.com/api"},
{ "clientAuthBaseUrl", "https://auth.company.com/api"}
};
Regex regEx = new Regex(#"\[(\w+)\]", RegexOptions.Compiled);
bool foundMatch = false;
strExpr = regEx.Replace(strExpr, match =>
{
string val = String.Empty;
if (variables.TryGetValue(match.Groups[1].Value, out val))
{
foundMatch = true;
return val;
}
return match.Value;
});
Match rmatch = regEx.Match(strExpr);
if (rmatch.Success && foundMatch )
{
return EvaluateStringWithVariables(strExpr);
}
return strExpr;
}
There's a quick but a suboptimal solution/fix for your first approach.
string prev;
do {
prev = strExpr;
foreach (string variable in variables.Keys)
{
var pattern = #"\[" + variable + #"\]";
strExpr = Regex.Replace(strExpr, pattern, variables[variable]);
}
}
until (prev == strExpr);
It will repeat substitutions until there's no replacements in the string.
You should care about possible loops in your substitution definitions. If there's any, do-loop will never ends. Add necessary checks or limit repetition count.

Tuple contains tow list in c#

I want to create a function with c# who return for me a list of ip and port the function is like that:
public Tuple<string,int> loadSocks()
{
var listip = new List<string>();
var listprt = new List<int>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(#"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
listip.Add(ip);
listprt.Add(port);
Tuple<List<string>, List<int>> tplLst = new Tuple<List<string>, List<int>>(listip, listprt);
Tuple<string, int> tplSum = Add(tplLst);
}
return tplLst;
}
I use tuple , I add tow list in this tuple but he give me the error in Tuple tplSum = Add(tplLst); in Add(tplLst).
What I should do ?
I recommend to simplfy your code. Instead of using a tuple which often decrease readability I would create a type for your purpose.
public class BiningInfo
{
public IPAddress IpAddress { get; set;}
public int Port { get; set;}
}
public List<BiningInfo> loadSocks()
{
var result = new List<BiningInfo>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(#"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
BiningInfo bi = new BiningInfo();
bi.IpAddress = IPAddress.Parse(ip);
bi.Port = port;
}
return result;
}
I believe you are trying to return a list of tuples. Change the signature of your function to return a list. Initialize a variable to hold the result. Then add each ip/port combination to it. Finally return the result.
public List<Tuple<string,int>> loadSocks()
{
var result = new List<Tuple<string, int>>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(#"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
result.Add(new Tuple<string,int>(ip, port));
}
return result;
}
I hope I've got your point. I think the following code is what you're looking for :
public List<Tuple<string, int>> loadSocks()
{
List<Tuple<string, int>> result = new List<Tuple<string, int>>();
var input = Path.GetFullPath(Path.Combine(Application.StartupPath, "Exploit1/socks-list.txt"));
var r = new Regex(#"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})");
foreach (Match match in r.Matches(input))
{
string ip = match.Groups[1].Value;
int port = Convert.ToInt32(match.Groups[2].Value);
result.Add(new Tuple<string,int>(ip,port));
}
return result;
}

How to combine two function's for file deletion

I have two different function to handle two different types of my input text file. One text file with double quotes and one without double quotes.
I wanted to know how can i combine these two functions to a common single function where i can handle in a more efficient way
Code:
//this the function to handle text file without double quotes
public void stack1()
{
string old;
string iniPath = Application.StartupPath + "\\list.ini";
bool isDeleteSectionFound = false;
List<string> deleteCodeList = new List<string>();
using (StreamReader sr = File.OpenText(iniPath))
{
while ((old = sr.ReadLine()) != null)
{
if (old.Trim().Equals("[DELETE]"))
{
isDeleteSectionFound = true;
}
if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb = new StringBuilder();
using (StreamReader reader = File.OpenText(textBox1.Text))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var value = line.Split('\t');
bool deleteLine = value.Any(v => deleteCodeList.Any(w => v.Equals(w)));
if (!deleteLine)
{
sb.Append(line + Environment.NewLine);
}
}
}
File.WriteAllText(textBox1.Text, sb.ToString());
//return;
}
//this the function to handle text file with double quotes
public void stack()
{
string old;
string iniPath = Application.StartupPath + "\\list.ini";
bool isDeleteSectionFound = false;
List<string> deleteCodeList = new List<string>();
using (StreamReader sr = File.OpenText(iniPath))
{
while ((old = sr.ReadLine()) != null)
{
if (old.Trim().Equals("[DELETE]"))
{
isDeleteSectionFound = true;
}
if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb = new StringBuilder();
using (StreamReader reader = File.OpenText(textBox1.Text))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split('\t').Select(v => v.Trim(' ', '"'));
bool deleteLines = values.Any(v => deleteCodeList.Any(w => v.Equals(w)));
if (!deleteLines)
{
sb.Append(line + Environment.NewLine);
}
}
}
File.WriteAllText(textBox1.Text, sb.ToString());
MessageBox.Show("finish");
}
The only difference between these two functions is this line:
// stack1 function
var value = line.Split('\t');
// stack2 function
var values = line.Split('\t').Select(v => v.Trim(' ', '"'));
The simplest way would probably be to add a parameter to your method, and then add the check after the split:
public void Split(bool shouldTrimQuotes)
{
...
IEnumerable<string> value = line.Split('\t');
if (shouldTrimQuotes)
{
value = value.Select(v => v.Trim(' ', '"'));
}
...
}
In one case, you would pass true as the parameter (which will cause quotes to be trimmed), while in the second one you would pass false to indicate you don't want to trim them:
// split, but don't trim quotes before comparison
Split(shouldTrimQuotes: false);
// split, trim quotes before comparison
Split(shouldTrimQuotes: true);
You might also play a bit and try to refactor the whole thing, trying to extract smaller general pieces of code into separate methods which might make it clearer what they are doing. This is one approach, for example:
// rewrites the specified file, removing all lines matched by the predicate
public static void RemoveLinesFromFile(string filename, Func<string, bool> match)
{
var linesToKeep = File.ReadAllLines(filename)
.Where(line => match(line))
.ToList();
File.WriteAllLines(filename, linesToKeep);
}
// gets the list of "delete codes" from the specified ini file
public IList<string> GetDeleteCodeList(string iniPath)
{
return File.ReadLines(iniPath)
.SkipWhile(l => l.Trim() != "[DELETE]")
.Skip(1).ToList();
}
// removes lines from a tab-delimited file, where the specified listOfCodes contains
// at least one of the tokens inside that line
public static void RemoveLinesUsingCodeList(
string filename,
IList<string> listOfCodes,
bool shouldTrimQuotes)
{
RemoveLinesFromFile(filename, line =>
{
IEnumerable<string> tokens = line.Split('\t');
if (shouldTrimQuotes)
{
tokens = tokens.Select(v => v.Trim(' ', '"'));
}
return (tokens.Any(t => listOfCodes.Any(t.Equals)));
});
}

Parsing multi-line string in C#

I have a string that looks like this:
TYPE Email Forwarding
SIGNATURE mysig.html
COMPANY Smith Incorp
CLIENT NAME James Henries
... heaps of others ....
I need to get the values of Type, Signature, Company and Client Name. There are others but once I can find a soution on how to do these, I can do the rest. I have tried to split and trim the string but then it splits fields like CLIENT NAME or on values like Email Forwarding.
I would put all of the "key" values into a collection, and then parse the string into another collection and then compare the values of the collections.
Here is a rough outline of how you could get the values:
static void Main(string[] args)
{
//Assuming that you know all of the keys before hand
List<string> keys = new List<string>() { "TYPE", "SIGNATURE", "COMPANY", "CLIENT NAME" };
//Not sure of the origin of your string to parse. You would have to change
//this to read a file or query the DB or whatever
string multilineString =
#"TYPE Email Forwarding
SIGNATURE mysig.html
COMPANY Smith Incorp
CLIENT NAME James Henries";
//Split the string by newlines.
var lines = multilineString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
//Iterate over keys because you probably have less keys than data in the event of duplicates
foreach (var key in keys)
{
//Reduce list of lines to check based on ones that start with a given key
var filteredLines = lines.Where(l => l.Trim().StartsWith(key)).ToList();
foreach (var line in filteredLines)
{
Console.WriteLine(line.Trim().Remove(0, key.Length + 1));
}
}
Console.ReadLine();
}
That will do your job.
If it is multiple lines then you can loop through each line and call KeyValue extension method as given below:
public static class Program
{
public static void Main()
{
var value = "TYPE Email Forwarding".KeyValue();
var value1 = "CLIENT NAME James Henries".KeyValue();
}
public static KeyValuePair<string, string> KeyValue(this string rawData)
{
var splitValue = rawData.Split(new[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
KeyValuePair<string, string> returnValue;
var key = string.Empty;
var value = string.Empty;
foreach (var item in splitValue)
{
if (item.ToUpper() == item)
{
if (string.IsNullOrWhiteSpace(key))
{
key += item;
}
else
{
key += " " + item;
}
}
else
{
if (string.IsNullOrWhiteSpace(value))
{
value += item;
}
else
{
value += " " + item;
}
}
}
returnValue = new KeyValuePair<string, string>(key, value);
return returnValue;
}
}
Please note that this logic will work only when keys are all upper and the values are not all upper case. Otherwise, there is no way to identify which one is key (without having a manual track on keys) and which one is not.

Reading specific lines in a .Log file

I have a log file that I am reading into different objects. One object starts at a Line that contains the words "Announce message" and the following lines contain the data that belongs to that message. This entry stops at a line that contains the word "Disposed".
I want to read all the data from between these 2 lines that, contains certain words.
Im currently using a Dictionary because the line with "Announce message" also contains a UID but the following lines contain the data for that UID.
How would you do that?
This is what i have come up with so far.
public static void P2PLogParser(List<FileInfo> fileList)
{
foreach (FileInfo fi in fileList)
{
//Læser alle linier i csv fil
foreach (var line in File.ReadAllLines(fi.FullName))
{
string MeterUID = GetMeterUID(line);
string MimHashcode = GetMimHashcode(line);
string FirmwareUploadStatus = GetFirmwareUploadStatus(line);
string IsKnown = GetIsKnown(line);
DateTime P2PTimeStamp = GetTimestamp(line);
if (IsMeterEntry(line) && !meters.ContainsKey(MeterUID))
{
string MeterNr = GetMeterUID(line).Replace("4B414D", "");
int meternr = int.Parse(MeterNr, System.Globalization.NumberStyles.HexNumber);
meters.Add(MeterUID, new Meter()
{
MeterUID = MeterUID,
MeterNR = meternr,
P2Pmeterentry = new List<P2PMeterEntry>()
});
}
if (IsMeterEntry(line))
{
P2PMeterEntry p2pmeter = new P2PMeterEntry
{
P2PTimeStamp = P2PTimeStamp,
MimHashcode = MimHashcode,
FirmwareUploadStatus = FirmwareUploadStatus,
IsKnown = IsKnown,
P2PMetersession = new List<P2PMeterSession>()
};
if (IsNoLongerMeterEntry(line))
{
string SessionLevel = GetLevel(line);
string SessionMessage = GetSessionMessage(line);
string Context = GetSessionContext(line);
P2PMeterSession MeterSession = new P2PMeterSession
{
SessionTimeStamp = P2PTimeStamp,
SessionLevel = SessionLevel,
SessionMessage = SessionMessage,
Context = Context
};
meterSession.Add(MeterSession);
}
meters[MeterUID].P2Pmeterentry.Add(p2pmeter);
}
}
}
}
and the IsMeterEntry and IsNoLongerMeterEntry
//IsMeterSession
public static bool IsMeterEntry(string text)
{
return text.ToLower().Contains("announce message received:");
}
public static bool IsNoLongerMeterEntry(string text)
{
return text.ToLower().Contains("context - disposed");
}
Implement a simple state machine with two states: IgnoreLine (initial state) and Announce.
for each line in log
if line contains "Announce message"
read UID
create a StringBuilder
set state=Announce
else if line contains "Disposed"
store the StringBuilder's content in the dictionary[uid]
set state=IgnoreLine
else if state==Announce and line contains "certain words"
append line to StringBuilder

Categories