'Replacing {text} in .txt or array with different text
Consider having the following strings in a .txt file
127.0.0.1
127.0.0.2
127.0.0.3
Or in an array:
{"127.0.0.1","127.0.0.2","127.0.0.3"}
And consider having the following strings into a different .txt file:
curl -sS -L https://github.com/vulmon/Vulmap/archive/master.tar.gz -o vulmap.tgz && echo y | sshpass -p 'ubuntu' scp -q ./vulmap.tgz [email protected]: && echo y | sshpass -p '{password}' ssh {username}@{ip} "mkdir -p ~/tmp-vulmap && cd ~/tmp-vulmap && tar -xzf ../vulmap.tgz && rm ../vulmap.tgz && cd ./Vulmap-master/Vulmap-Linux && python3 vulmap-linux.py"
curl -sS -L https://github.com/D0cT0r-inf0s3c/Praus/archive/master.tar.gz -o praus.tgz && echo y | sshpass -p 'ubuntu' scp -q ./praus.tgz [email protected]: && echo y | sshpass -p '{password}' ssh {username}@{ip} "mkdir -p ~/tmp-praus && cd ~/tmp-praus && tar -xzf ../praus.tgz && rm ../praus.tgz && cd ./Praus-master && (echo; echo 2; echo 1; echo 8; echo; echo 3; echo; echo 1;) | sudo bash praus.sh"
curl -L https://github.com/speed47/spectre-meltdown-checker/archive/master.tar.gz -o spectre-meltdown-checker.tgz && sshpass -p '{password}' scp -q ./spectre-meltdown-checker.tgz {username}@{ip}: && sshpass -p '{password}' ssh {username}@{password} "mkdir -p ~/tmp-BeRoot && cd ~/tmp-spectre-meltdown-checker && tar -xzf ../spectre-meltdown-checker.tgz && rm ../spectre-meltdown-checker.tgz && cd ./spectre-meltdown-checker-master && ./spectre-meltdown-checker.sh"
Is there a possibility that I can replace the strings {ip} with the IP's? For the username and password an argument by user input will be parsed through. If the {ip} will work then I will find out those two as well.
Conditions:
- The code will be ran parallel so it might be handy to loop through the first .txt file and save those in a variable if necessary.
Solution 1:[1]
I have found the solution to this issue:
public class Test
{
public static void Main()
{
string replace = @"{ip}";
//Hosts & programs
string whosts = @"/hosts/windows.txt";
string lhosts = @"/hosts/linux.txt";
string defaultprofile = "programs.config";
//IP Locations
string llocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + lhosts;
string wlocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + whosts;
//string[] ips = { "127.0.0.1", "127.0.0.2", "127.0.0.3", "127.0.0.4", "127.0.0.5", "127.0.0.6", "127.0.0.7", "127.0.0.8", "127.0.0.9", "127.0.0.10", "127.0.0.0" };
//Read winips and linips
List<string> winhosts = File.ReadAllLines(wlocation).ToList();
List<string> linhosts = File.ReadAllLines(wlocation).ToList();
ParallelOptions? options = new() { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = CancellationToken.None };
//Check if windows.txt contains any hosts
if (winhosts.Any())
{
string[] lines = File.ReadAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"/example/" + defaultprofile);
List<string> winprograms = lines.SkipWhile(x => x != "WINVULNSCAN:") // skips everything before
.Skip(1) // and <rs:data> itself
.TakeWhile(x => x != "LINVULNSCAN:") // and take up to </rs:data>
.ToList(); //Converts it to a List
Parallel.ForEach(winhosts, host =>
{
Parallel.ForEach(winprograms, line =>
{
string? result = line.Replace(replace, host);
Console.WriteLine(result + Environment.NewLine);
});
});
}
//check if linux.txt contains any hosts
if (linhosts.Any())
{
string[] lines = File.ReadAllLines(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"/example/" + defaultprofile);
List<string> linprograms = lines.SkipWhile(x => x != "LINVULNSCAN:") // skips everything before
.Skip(1) // and <rs:data> itself
.TakeWhile(x => x != "OTHERVULNSCAN:") // and take up to </rs:data>
.ToList(); //Converts it to a List
Parallel.ForEach(linhosts, host =>
{
Parallel.ForEach(linprograms, line =>
{
string? result = line.Replace(replace, host);
Console.WriteLine(result + Environment.NewLine);
});
});
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Leonardo van de Weteringh |