Deleting a matched line AFTER match another line - bash

I would like to have your help on this:
I have a file of thousands of lines, I need to find a section of the file and delete some lines in place:
This is the section and the lines I would like to delete are marked:
interface Vlan824
description WRES_824
vrf forwarding V211
ip address 172.17.224.2 255.255.240.0 #### Delete this ####
ip helper-address xxxx
ip helper-address xxxy
no ip redirects
no ip proxy-arp
ip verify unicast source reachable-via rx 2699
standby delay minimum 0 reload 60
standby version 2
standby 0 ip 172.17.224.1 #### Delete this ####
standby 0 priority 110
standby 0 preempt delay minimum 300 reload 300
shutdown
standby 1 ipv6 FE80::1 #### Delete this ####
standby 1 ipv6 <IPV6-PREFIX-1>0E:824::1/64 #### Delete this ####
standby 1 priority 110
standby 1 preempt delay minimum 300 reload 300
ipv6 address FE80::2 link-local #### Delete this ####
ipv6 address <IPV6-PREFIX-1>0E:824::2/64 #### Delete this ####
ipv6 nd prefix <IPV6-PREFIX-1>0E:824::/64 no-advertise #### Delete this ####
ipv6 nd managed-config-flag
ipv6 nd other-config-flag
no ipv6 redirects
ipv6 dhcp relay destination xxx
ipv6 dhcp relay destination xxx
ipv6 verify unicast source reachable-via rx URPF
bfd interval 750 min_rx 750 multiplier 3
arp timeout 300
!
This sed would delete the mentioned lines (except the third one), but I need to do it JUST in that section.
sed -i '/224.1/d; /224.2/d; /224.3/d; /:224::/d; /:824::/d' FILE.txt
I would appreciate your help.
Fer
EDIT:
To clarify what I need, if I have this file:
aaa
bbb
hhh
eeb
ccc
!
aab I need to find this section ( from aab to ggc )
hhb and delete just the eeb line
eeb
ffb
ggc
!
aac
hhc
eeb
ffc

Combine your command into a single group, and address the group with the range you want to affect.
sed -i '/interface Vlan824/,/!/{/224.1/d;/224.2/d;/224.3/d;/:224::/d;/:824::/d;}' foo.txt

Supposing the section you want to edit is the Vlan824 one (ending with the ! character), and the pattern to match to delete lines are 224.1 and FE80
sed -n '/Vlan824/,//p;/^\!/q' your-file | grep -v '224.1\|FE80'

Related

How to Change IP address in 30 seconds automatically?

I have developed a web scrape software for personal use. Now I need to change my IP address in every 30 seconds for resist block. What is the best way to change IP address in every 30 seconds?
Thank You.
Use proxy plugins that can help you do this.
If you are on UNIX system, you can try this:
Create an ip.sh file with content below, replacing eth0 by your network interface if necessary and by the 3 first numbers of your network's IP address (e.g. 192.168.1)
chmod 700 ip.sh
./ip.sh (use sudo if you're not using the administrator account)
ip.sh file:
#!/bin/bash
index=2
while :
do
ifconfig eth0 <IP>.${index} netmask 255.255.255.0 up
index=$((index+1))
if [ "$index" -gt "254" ]; then
index=2
fi
sleep 30
done

display open ports grouped by process

given a netstat output, how can i display the selected open ports grouped by process?
what i got so far:
:~# netstat -tnlp | awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}'
haproxy 636
haproxy 993
haproxy 993
haproxy 465
haproxy 465
exim4 25
apache2 80
exim4 25
apache2 443
desired output (in one line):
apache2 (80 443), exim4 (25), haproxy (465 636 993)
please note:
i have duplicated lines because they listen on different IPs, but i only need one (sort -u is ok)
if possible, id like to sort by process and then by port
the main goal is to have this single line displayed to the user on ssh logon, using motd (i got this part covered)
netstat -tnlp|awk '/25|80|443|465|636|993/ {proc=split($7,pr,"/"); port=split($4,po,":"); print pr[2], po[port]}'|sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'
try this, Later addition
sort|uniq|awk '{a[$1]=a[$1](" "$2" "$3)}END{for (i in a) printf "%s (%s),",i,a[i]}'

Formatting IP with sed

I am trying to figure out how to do the following with sed:
I got a list of IPv4 addresses and I am trying to make them all uniform in the display. So for example: 1.2.4.32 would be 001.002.004.032. 10.125.62.1 would be 010.125.062.001.
I am trying to use sed to do it because that's what I am learning right now.
I got these two, which will take any one or two digit number and append zeros at the front.
sed 's/\<[0-9][0-9]\>/0&/g' file
sed 's/\<[0-9]\>/00&/g' file
But that runs into a more practical problem in that my input file will have single or double digits numbers in other non-IP address places. Example:
host-1 1.2.3.32
So I need a way for it to look for the full IP address, which I thought could be achieved by this
sed 's/\.\<[0-9]\>/00&/g'
but not only does that ignore the case of 1.something.something.something, but also it appends the 00 at the end of 3rd octet for some reason.
echo "10.10.88.5" | sed 's/\.\<[0-9]\>/00&/g'
10.10.8800.5
Sample file:
Jumpstart Server jumo 10.20.5.126
Jumpstart Server acob 10.20.5.168
NW1 H17 Node cluster 10.10.161.87
NW1 H17 Node-1 10.10.161.8
NW1 H17 Node-2 10.10.161.9
ts-nw1 10.10.8.6
The idiomatic way of changing only parts of a line is to copy it to the hold space, remove the parts we're not interested in from the pattern space, get the hold space back and then rearrange the pattern space to replace the part we've changed with our new version.
This should work (replace -r with -E for BSD sed):
sed -r 'h # Copy pattern space to hold space
# Remove everything except IP address from pattern space
s/.*\b(([0-9]{1,3}\.){3}[0-9]{1,3})\b.*/\1/
s/([0-9])+/00&/g # Prepend '00' to each group of digits
s/[0-9]*([0-9]{3})/\1/g # Only retain last three digits of each group
G # Append hold space to pattern space
# Replace old IP with new IP
s/(.*)\n(.*)\b([0-9]{1,3}\.){3}[0-9]{1,3}\b(.*)/\2\1\4/' infile
The last step is the most complicated one. Just before it, a line looks like this (newline as \n, end of line as $):
010.020.005.126\nJumpstart Server jumo 10.20.5.126$
i.e., our new and improved IP address, a newline, then the complete old line. We now capture the underlined groups:
010.020.005.126\nJumpstart Server jumo 10.20.5.126$
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
(.*) \n (.*) \b...\b (.*)
\1 \2 \3 \4
and rearrange the line by using group 2, then groups 1 (our new IP) and 4. Notice that
There are four capture groups, but the third one is just there to help describe an IP address, we don't actually want to retain it, hence \2\1\4 in the substitution (there are no non-capturing groups in sed).
The last capturing group (after the IP address) is empty, but having it makes it possible to use this for lines that have the IP address anywhere.
This only replaces the first IP address on each line, in case there are several.
The overall output is
Jumpstart Server jumo 010.020.005.126
Jumpstart Server acob 010.020.005.168
NW1 H17 Node cluster 010.010.161.087
NW1 H17 Node-1 010.010.161.008
NW1 H17 Node-2 010.010.161.009
ts-nw1 010.010.008.006
The same as a solidly unreadable one-liner:
sed -r 'h;s/.*\b(([0-9]{1,3}\.){3}[0-9]{1,3})\b.*/\1/;s/([0-9])+/00&/g;s/[0-9]*([0-9]{3})/\1/g;G;s/(.*)\n(.*)\b([0-9]{1,3}\.){3}[0-9]{1,3}\b(.*)/\2\1\4/' infile
\b is a GNU extension. The script mostly works without it as well; using it makes sure that blah1.2.3.4blah is left alone.
$ cat 37222835.txt
Jumpstart Server jumo 10.20.5.126 10.29.23.24
Jumpstart Server acob 10.20.5.168 dig opt
Jumpstart Server reac 251.218.212.1 rel
NW1 H17 Node cluster 10.10.161.87
NW1 H17 Node-1 10.10.161.8
NW1 H17 Node-2 10.10.161.9
ts-nw1 10.10.8.6
Nw2 HW12 Node-3 192.168.0.1
cluster
Doing :
sed -n 's/\([1]\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)\{1\}\.'\
'\([1]\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)\{1\}\.'\
'\([1]\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)\{1\}\.'\
'\([1]\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5] \)/00\1\.00\2\.00\3\.00\4/g;
s/0\+\([0-9]\{3\}\)/\1/g;p' 37222835.txt
gives :
Jumpstart Server jumo 010.020.005.126 010.029.023.024
Jumpstart Server acob 010.020.005.168 dig opt
Jumpstart Server reac 251.218.212.001 rel
NW1 H17 Node cluster 010.010.161.087
NW1 H17 Node-1 010.010.161.008
NW1 H17 Node-2 010.010.161.009
ts-nw1 010.010.008.006
Nw2 HW12 Node-3 192.168.000.001
cluster
Advantage over the approach mentioned by #benjamin-w
This can replace multiple ip addresses in the same line
Disadvantage(the approach mentioned by #benjamin-w remedy this)
Had there be a word say Node-000234 it would be changed to Node-234. In fact, you could work on the second substitution command to get the desired behaviour.

Recursive text search whithin lines before the grep match in shell

I need help working with router config backup database. I need to get a list of interfaces that don't have vrf or shutdown in their configuration.
I get the list of all interfaces config passing the config file through awk '/^interface/,/!/'. This gives me the output below:
interface TenGigE0/3/0/0
description
service-policy output QOS
ipv4 mtu 1500
ipv4 address 13.24.15.3 255.255.255.252
carrier-delay up 3000 down 0
load-interval 30
dampening
!
interface TenGigE0/3/0/1
description Link To
!
interface TenGigE0/3/0/1.302
description
vrf 1671
ipv4 address 13.24.14.11 255.255.255.254
encapsulation dot1q 302
Now, i am stuck trying to exclude the interfaces that contain vrf line. What i was trying to do is to grep for vrf, and when there is a match, remove the line that contains the word "interface" above. Unfortunately with no luck. Maybe someone has a more sophisticated solution.
If you have the structured records awk can solve this problem. Given your intermediate file
2$ awk 'BEGIN{RS=ORS="!\n"} !/vrf/' interface
will print the records without "vrf"
interface TenGigE0/3/0/0
description
service-policy output QOS
ipv4 mtu 1500
ipv4 address 13.24.15.3 255.255.255.252
carrier-delay up 3000 down 0
load-interval 30
dampening
!
interface TenGigE0/3/0/1
description Link To
!

ipv6 neigh entries getting failed

I have bunch of ipv6 neigh entries which are failed:
6000::2828:2802 dev eth2 lladdr 00:1f:a0:02:0e:b2 STALE
7000::1e1e:1e01 dev eth1 FAILED
8000::1e1e:1e01 dev eth1 FAILED
4000::1414:149e dev eth2 lladdr 00:03:00:04:00:09 PERMANENT
5000::1e1e:1e01 dev eth1 FAILED
3000::a0a:a3a dev eth1 lladdr 00:03:00:03:00:09 PERMANENT
Now, When I use flush to remove these entries, it says nothing to flush. Do you guys know how to flush or remove these entries, If I delete the entry, it goes in the failed state. Can I change the time for these values, so it automatically gets removed in say 10 seconds.
They should completely disappear when you do something like
ip -6 neigh del 3000::a0a:a3a dev eth1
But much more important: those addresses are bogons. They should never be in use anywhere... Seeing them in your neighbor discovery tables means that your system thinks they are on-link, and that should not be the case.
I suggest you look at your network configuration first. Your interfaces might have the wrong prefixes of prefix-lengths configured...
Yes you can have them removed in 10 secs or whatever.
$ sysctl net.ipv6.neigh.default
...
net.ipv6.neigh.default.gc_interval = 30
net.ipv6.neigh.default.gc_stale_time = 60
...
gc_interval is seconds after which the clean-up kicks in to remove stale entries.
gc_stale_time is seconds after which the entries are marked to be stale.
You can set both these values to 10. You can override these values exclusively for eth1 under net.ipv6.neigh.eth1.
$ sysctl -w net.ipv6.neigh.eth1.gc_interval=10
$ sysctl -w net.ipv6.neigh.eth1.gc_stale_time=10

Resources