How to use sed command to replace hostname and ip adddress - linux

I am using sed command to replace the IP Address of Host from a file where IP is generating randomly. I am not getting a search pattern for a double quote("). below pattern have to replace:
Host1 = "1.1.1.1" replace with Host2="2.2.2.2" where Host1's IP address is not fixed.
I was trying to replace the below file:
cat a.txt
Host1 ="1.1.1.1"
sed -i -e 's/Host1 =*/Host =2.2.2.2/g' a.txt
cat a.txt
Host =2.2.2.2"1.1.1.1"

You can replace 1.1.1.1 by running this command, using sed too:
sed -i 's|"[^"]*"|"2.2.2.2"|' < a.txt
That way the IP address will change to 2.2.2.2 no matter what IP address it was before, as long as it is stored between double quotes (") in your file.

Related

Regex to match IP addresses but ignore localhost

So I have this script that does something with IPs allocated to my OS (GNU/Linux) that I get from running ifconfig. It works fine, however, I was wondering if I could filter out loopback/localhost IP (127.0.0.1) in the same regex expression [I assume every server within my cluster has said IP and I don't need to do anything with it in my script.]
What my script uses is:
ifconfig | awk '/(([0-9]{1,3}\.){3})/ {print}' |sed -e "s/.*addr\://g" -e "s/\s.*//g"
I get results like:
> ifconfig | awk '/(([0-9]{1,3}\.){3})/ {print}' |sed -e "s/.*addr\://g" -e "s/\s.*//g"
172.16.0.1
127.0.0.1
I know it might be a stupid question, but could I filter any IP that starts with 127 in my first regex?
I could try changing awk for grep, somethin like:
> ifconfig |egrep -o "addr\:(([0-9]{1,3}\.){3}[0-9]{1,3})" |sed -e "s/.*addr\://g"
but if I try to negate (?!127) at the beginning, bash will interpret it as !127 which would just throw me something from the history.
I mean, I could just run another grep at the end of the oneliner like grep -v "127.0.0.1", but I just wanted to avoid greping something already greped. Not that anything is wrong with that, just trying to know little more and be more efficient, I guess.
With only one grep without sed or awk:
# ip a|grep -oP "inet \K[0-9.]*(?=.*[^ ][^l][^o]$)"
192.168.1.31
172.16.5.31
You can just add a clause to match the 127.0.0.1 and exclude it by adding the next as below. This way Awk ignores doing any action on the lines containing this pattern.
.. | awk '/127.0.0.1/{next}/(([0-9]{1,3}\.){3})/{print}' | ..

Print a certain line in linux or options for nslookup

I'm very new to linux and bash. I'm trying to find a domain name for an ip address. When I use nslookup I have a bunch of lines like this
nslookup 204.228.150.3
Output
Server: 198.60.22.2
Address: 198.60.22.2#53
Non-authoritative answer:
3.150.228.204.in-addr.arpa name = www.computerhope.com.
Authoritative answers can be found from:
150.228.204.in-addr.arpa nameserver = ns.xmission.com.
150.228.204.in-addr.arpa nameserver = ns1.xmission.com.
150.228.204.in-addr.arpa nameserver = ns2.xmission.com.
ns.xmission.com Internet address = 166.70.254.2
ns1.xmission.com Internet address = 204.228.159.2
ns2.xmission.com Internet address = 207.135.133.2
I only want to print www.computerhope.com in the second line. How do I do this?
Also I tried to use host command as well. It looks cleaner.
206.153.126.75.in-addr.arpa domain name pointer www.cyberciti.biz.
How do I only print www.cyberciti.biz when I use host command ?
PLease help
$ host 75.126.153.202 |sed -e 's/.* //'
www.cyberciti.biz.
nslookup 204.228.150.3 | \
grep "in-addr.arpa" | \
cut -d '=' -f 2 | \
tr -d '[:blank:]'
With grep you find the line containing the address. With the cut command you split the line by the "=" sign. With tr you remove any remaining spaces.
The command does not have any error handling in case the address is unknown.
If you would provide more information on what you want to achieve maybe other solutions would come up. :-)
Another way to do this is to pipe the host command to awk:
$ host 75.126.153.202 | awk '{print $NF}'
NF is a built-in awk variable which prints the last column of the output of the host command, which in this case is the domain name.
You can also pipe nslookup to awk:
$ nslookup 75.126.153.202 | awk '$2 ~ /^name$/{print $NF}'
awk returns the last column of the line containing the name in its 2nd column.

Reverse IP address format with sed

I have a txt file with a list of ip addresses against domain names. eg;
1.1.168.192 example1.example1.net
2.1.168.192 example2.example2.net
3.1.168.192 example3.example3.net
.....
12.1.168.192 example12.example12.net
I can't get my sed command to change the output to;
192.168.1.1 example1.example1.net
192.168.1.2 example2.example2.net
192.168.1.3 example3.example3.net
....
192.168.1.12 example12.example12.net
sed command i'm using is
sed -r 's/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/\4.\3.\2.\1/'
using it as
cat filename | sed -r 's/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/\4.\3.\2.\1/'
The only problem is that you've included an anchor $ in your pattern, which tries to match the end of each line but fails. You just need to remove it:
$ sed -r 's/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/\4.\3.\2.\1/' file
192.168.1.1 example1.example1.net
192.168.1.2 example2.example2.net
192.168.1.3 example3.example3.net
Note that I'm passing the file name as an argument to sed, thereby avoiding a useless use of cat.
awk version
awk '{split(".",i,$1);printf "%d.%d.%d.%d %s\n",i[4],i[3],i[2],i[1],$2}' YourFile
$ sed -r 's/([^.]+)(\.[^.]+)(\.[^.]+)\.([^ ]+)/\4\3\2.\1/' file
192.168.1.1 example1.example1.net
192.168.1.2 example2.example2.net
192.168.1.3 example3.example3.net
192.168.1.12 example12.example12.net

Using Bash to Extract IP Addresses From Each Line of Log File

Is there a way using bash /sed/awk to extract IP addresses from each line of log file to show IP host conversations or connection attempts?
Example of log file:
*Teardown TCP connection -1948864210 for Node14:110.98.8.41 to Net_N:10.98.35.28 duration 0:02:01 bytes 0 SYN Timeout
Built outbound TCP connection -1948863670 for Net11:10.10.2.5 (10.10.2.5 to Net01:10.9.15.2 (10.9.15.2)
Deny tcp src Node22:10.128.4.201/2254 dst outside:10.198.2.1/5560 by access-group "111"*
Required output, listing IP conversation/connection attempt:
110.98.8.41 10.98.35.28
10.10.2.5 10.9.15.2
10.128.4.201 10.198.2.1
I have tried using grep to strip out the IPs:
cat log.file | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | sort | uniq
But the output just lists single IP addresses and not line-by-line IP conversations
Any help is appreciated..
To print IPs next to each other, try the below command:
cat first | grep -o '[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}' | awk 'NR%2{printf $0"\t";next;}1'
You could do this:
octet='\<(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?)\>'
ip="$octet\\.$octet\\.$octet\\.$octet"
grep -Eo "$ip" file | paste - -
Or, use a wheel that's already been invented
perl -MRegexp::Common -lne '$,=" "; print /$RE{net}{IPv4}/g' file

bash + how to capture IP address from line

I have many configuration files ,
the line that start with LINE word have IP address
My target to read the line that start with LINE word from the file and print only the IP address
The problem is that IP address can be in any field in the line so I can’t capture the IP according to field number
example
grep LINE file1.txt
LINE /home/Ariate/run.pl "Voda STS 4 Test - " "102841" && ssh 17.77.170.130 -p 2022
grep LINE file2.txt
LINE /home/Ariate/run.pl 137.77.170.30 "Voda STS 4 Test - " "102841" && ssh ACTIVE
please advice how to capture the IP address from the line ( solution can be also with perl one liner )
expected results
echo $IP_FROM_LINE
17.77.170.130
echo $IP_FROM_LINE
137.77.170.30
perl -MRegexp::Common=net -lne 'print $1 if /^LINE.*\b($RE{net}{IPv4})/'
Using this grep -oE:
grep -oE '\d+\.\d+\.\d+\.\d+' file
17.77.170.130
137.77.170.30
OR else:
grep -oP '\d+\.\d+\.\d+\.\d+' file
The following will get you the desired IP addresses:
grep -oP '^LINE.*\b\K\d+\.\d+\.\d+\.\d+' file
To place the result in a variable as request, you'll need to iterate of the results as follows:
grep -oP '^LINE.*\b\K\d+\.\d+\.\d+\.\d+' file |
while read IP_FROM_LINE ; do
echo $IP_FROM_LINE
done
grep -oE '[0-9]{2,3}(\.[0-9]{2,3}){3}'
matches
17.77.170.130
137.77.170.30
or
grep -oP '\d{2}(\.\d{2}){3}'
if your grep supports -P option.
both of them works with the data you have given.
But if you want really worried of what to be matched, use
grep -Eo '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
which would match excat ip addresses.

Resources