Regex.Replace for IP address - regex

I've lifted a piece of code from somewhere on the internet it looks like this
ip = Regex.Replace(ip, #"^(?<Prefix>(\d{1,3}\.){3})\d{1,3}$", "${Prefix}*");
What it does is takes an IP address and replaces the last section with a asterisk. For example 192.168.0.1 would become 192.168.0.*
I'm useless with RegEx, I've tried to understand what the above is actually doing, but not having any success.
What I'm after is 2 more Regex.Replace code so that 192.168.0.1 becomes
192.168.*.*
192.*.*.*
Can anyone help me?

192.168.*.* = ip = Regex.Replace(ip, #"^(?<Prefix>(\d{1,3}\.){2})\d{1,3}\.\d{1,3}$", "${Prefix}*.*");
192.*.*.* = ip = Regex.Replace(ip, #"^(?<Prefix>(\d{1,3}\.))\d{1,3}\.\d{1,3}\.\d{1,3}$", "${Prefix}*.*.*");
Give that a shot, see what happens.

Related

How to assign Static IP based on MAC Address using powershell/script in Windows?

I would like to write up a script to assign static IP based on mac addresses, as I am having trouble with "USB to ethernet" adapters lose it's IP settings and assign to different interface Names.
I am running on windows 10 environment and have found a wmi script online that I think might work.
Code I am using:
wmic nicconfig where macaddress="0F:98:90:D6:42:92" call EnableStatic ("192.168.1.1"), ("255.255.255.0")
Error output:
"Invalid format.
Hint: = [, ]."
Thanks
Something like this
$netAdapter = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.MACAddress -eq '0F:98:90:D6:42:92'}
$netAdapter.EnableStatic("192.168.1.1", "255.255.255.0")

Getting Mac Address in Groovy

Just want to ask if there's a way of getting your local machine's Mac address as String.
I need to save my local machine's mac address into a domain class. I tried using this code to get my Mac address:
String address = "ifconfig".execute().text()
But this line it also returns a lot of details about my ip address, all I need to get is the Mac Address which is found after the substring "HWaddr". I'm thinking if i could extract this substring using regex but I am not sure how to do it.
You can get the MAC address for an interface using java.net.NetworkInterface. Note that it is possible to have multiple hardware network interfaces, so it's possible to have more than one MAC address. In addition, most machines will have at least one interface without a hardware address: the loopback interface.
This will get a list of all the MAC addresses as Strings, including nulls for interfaces without a MAC address:
import java.net.NetworkInterface
def macs = NetworkInterface.networkInterfaces.collect { iface ->
iface.hardwareAddress?.encodeHex().toString()
}
You can use the NetworkInterface to get this information, especially the getHardwareAddress() method which return a byte array

Pinging from a C/C++ program

I want to write a C or C++ program, that given an IP address, Pings it and then performs further action based on whether the Ping was successful or not.
How to do this?
Have a blast at The Ping Page, which has a link to full source on the original Unix ping(8).
EDIT I saw after I posted, you are on Ubuntu. However someone searching this question may still find these links helpful for Windows.
Ping: Raw Sockets Method: http://tangentsoft.net/wskfaq/examples/rawping.html
Implementing Internet Pings Using Icmp.dll: http://support.microsoft.com/default.aspx?scid=kb;en-us;170591
IcmpSendEcho Function: http://msdn.microsoft.com/en-us/library/aa366050%28VS.85%29.aspx
Ping for Windows: http://www.codeproject.com/KB/IP/winping.aspx
This post is old but I think the following link will help future people lookign for a good explanation on how to create a Ping request.
Deconstructing Ping with C and NodeJS
#include <iostream>
using namespace std;
int main() {
int x = system("ping -c1 -s1 8.8.8.8 > /dev/null 2>&1");
if (x==0){
cout<<"success";
}else{
cout<<"failed";
}
replace 8.8.8.8 from your IP Address

Problem with converting IP addresses in C++

I need to find the IP address of my node in my code. Currently, I have this line:this line:
IPv4Address addr = nb_ipv4->GetAddress (maininterface , 0);
In this line, I expect to get the IP address for my main interface, but instead I see this error:
error:conversion from 'ns3::Ipv4InterfaceAddress' to non-scalar type 'ns3::Ipv4Address'
Can anyone help me to solve this error?
I think that must be tagged as C++, and (if it is The ns-3 network simulator) you must do this:
IPv4Address addr = nb_ipv4->GetAddress(maininterface).GetLocal();

getaddrinfo returns always 11001 (host not found)

Although the searched FQDN appears in etc\hosts file.
Any idea?
Thanks a lot!
Since I don't have code, here's a guess:
getaddrinfo("http://www.example.com");
Doesn't work; getaddrinfo takes a hostname not a URL.