does this regex combination match to an ipv4 address [duplicate] - c

This question already has answers here:
Extracing IP addresses as whole words with POSIX BRE/ERE regex
(2 answers)
Closed 1 year ago.
im having a hard time trying to create and find a good ipv4 regex match but all i can find is this:
(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})
does this regex combination match to something like xxx.xxx.xxx.xxx or xx.xxx.x.x etc? I'm trying to make a connection to a web server by pinging via domain name to get the ip but when i try it, the code returns that there is not match. here is the ping command btw:
PING google.com (172.217.1.14): 56 data bytes

256.1.1.1 9.900.0.0 etc. are not valid, this parses those out w/o back-tracking (to prevent RE-DoS):
egrep '^((0|1[0-9]{0,2}|2([0-4][0-9]{0,1}|5[0-5]{0,1}|[6-9]){0,1}|[3-9][0-9]{0,1})\.){3}(0|1[0-9]{0,2}|2([0-4][0-9]{0,1}|5[0-5]{0,1}|[6-9]){0,1}|[3-9][0-9]{0,1})$'

Related

Process two strings per line, data and process in single bash script

I have a working solution already with a while read IFS processing a csv file, but I'd like to have it all in a single bash script as the input data never changes.
The data is a list of ip addresses and names;
10.0.0.1,server1
10.0.0.2,server2
172.16.0.1,server3
192.168.0.1,server4
The process itself will run a ping/curl/wget as required, all the while echoing out which server and test it is doing.
I can run the IP list on its own in the same file using a list function and reading the items, but then I don't get the server friendly name echoed out. So my question is, how should I approach this? I was thinking create the data array then parse in to a read somehow and split the tokens, but not sure how. Thought about writing the data out to a temp file then reading it in again and deleting the tmp file afterwards, but this seems messy. Any pointers appreciated.
In terms of a working solution (if someone wanted to provide instead of just advising), the output of the above data could just be echoed out like this;
Testing: $server, IP address: $ip, test 1.
Then I will just sub the tests as required.
Thanks
If you want to include your data directly in a script instead of reading it from a separate file, and you're already using a loop to read the existing data, the easiest way is probably just copy and pasting the data file's contents into a heredoc that the loop reads instead:
#!/usr/bin/env bash
declare -i testno=1 # Make testno an integer variable that uses arithmetic expansion
while IFS=, read -r ip server; do
echo "Testing: $server, IP address: $ip, test $testno"
testno+=1
done <<EOF
10.0.0.1,server1
10.0.0.2,server2
172.16.0.1,server3
192.168.0.1,server4
EOF
which will display
Testing: server1, IP address: 10.0.0.1, test 1
Testing: server2, IP address: 10.0.0.2, test 2
Testing: server3, IP address: 172.16.0.1, test 3
Testing: server4, IP address: 192.168.0.1, test 4

How to change a user password? [duplicate]

This question already has an answer here:
How to add, delete edit username from /etc/passwd [duplicate]
(1 answer)
Closed 6 years ago.
I m playing with /etc/passwd in my C program.
I want to change a user password. are there a standard linux functions that do a such functions ?
Method 1: system("passwd <parameters>");
Method 2: *pwent() function family and also putpwent().
The question is possibly a duplicate.
Linux stores the password in the /etc/shadow file. The second column (delimited by : character) in this file against the user name shows the hashed password.
It's best advised to not touch this file and cause harm. If you must, you can use the vipw utility for editing

omnetpp.ini File Explanation

# ping app (host[0] pinged by others)
*.host[0].numPingApps = 0
*.host[*].numPingApps = 2
*.host[*].pingApp[*].destAddr = "host[0]"
**.pingApp[0].startTime = uniform(1s,5s)
**.pingApp[1].startTime = 5s+uniform(1s,5s)
**.pingApp[*].printPing = true
The above is a snippet of an omnetpp.ini file for initializing simulations (Currently I'm trying to simulate a WSN "Wireless Sensor Network". Hosts represent sensor nodes. I want to know exactly what does these two lines mean:
*.host[0].numPingApps = 0
*.host[*].numPingApps = 2
Does it mean that host[0] does not ping any of the others, and all the other hosts ping host[0] twice? Can someone please explain.
What if I want a packet (or cMessage) to travel from host[0] to host[n]? Is there any packages from INET I can use? (like using pingApp does the pinging to a certain host)
The meaning of wildcard symbol in omnetpp.ini is explained in OMNeT++ Manual.
There is a rule that:
if a parameter name matches several wildcard-patterns, the first
matching occurrence is used.
The two lines you are asking means: host[0] does not have any pingApp, all other hosts have 2 pingApp's.
As far as the second question is concerned: to generate a traffic from one host to another you can use any of TCPApp, or UDPApp, for example UDPBasicApp.

Netcat Socket Programming

I am facing some issues to solve a problem. A server program is giving 4 random unsigned integer numbers everytime I connect to it. My job is to add the 4 numbers and send back the sum. But the time is restricted. So if I want to use a calculator to do the job, it is not possible. Thus I have to take resort to scripting. I want to connect to the the server using netcat, retrieve the 16 bytes, pipe the data to my C program(which will parse the data into 4 unsigned int variables and print out the sum) and again redirect this program output to netcat
netcat <server> <port> | myProg | netcat <server> <port>
But the tcp socket netcat is opening second time is not the previous socket. So a different set of 4 numbers would be presented this time, which defeats the entire effort. My question is: Is it possible by any means(using netcat) to use the previous socket (opened by netcat) to pump the calculated sum back to the server?
I dont want to use C to do socket programming as it is very tough for me.
Also I know
echo 3<>/dev/tcp/server/port
cat <&3 | ./myprog | more >&3
probably will solve my purpose (correct me if I am wrong). But I would like to do it the netcat way. Thanks in advance.
Use socat instead of netcat.
socat can spawn the command itself, with input and output both redirected (you don't use shell redirection, let socat handle it)
socat TCP:server,port SYSTEM:myProg

How to write on a separate terminal using C in Linux? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to invoke another terminal for output programmatically in C in Linux
I am programming a client-server application and I want to create a debug window.
On the server side I want to print the incoming and outgoing communication on a separate terminal.
I am able to spawn a terminal through gnome-terminal but how to write on it and not on other terminals.
Unless you for some reason really need to print to a terminal, I wouldn't bother, not just for a debug printout.
I would have the server print to a log file (remembering to flush it appropriately often) and then use tail -f in another terminal to follow it. This has the added benefit of giving me a record of what the server debug-printed that I can examine at leisure.
Combining idea of #ibid idea to what you want. Write to log file and than execute:
xterm -e tail "-f" log_file
This will span xterm , which executes "tai -f log file" command.
The "correct" answer to this question is that you can write to /dev/ttyNUM... if you know the right tty number.
But that's only technical correctness, you should do something else. What you're trying to do is wrong.

Resources