Function required to sort the data horizontally and properly - awk

I'm trying to sort the data that I have but unfortunately It's not working as expected.
Data
interface Vlan198
description *****T3*****
no ip redirects
ip address 0.0.0.0/29
no ipv6 redirects
hsrp 198
ip 172.1.71.129
interface Vlan1004
no ip redirects
no ipv6 redirects
interface Vlan184
description **** Reserved for T2 ****
no ip redirects
ip address 1.1.1.1/26
no ipv6 redirects
hsrp 184
ip 1.1.1.2
interface Vlan750
no ip redirects
ip address 2.2.2.2/24
no ipv6 redirects
hsrp 188
ip 2.2.2.3
interface Vlan183
description **** Reserved for T1 ****
no ip redirects
ip address 3.3.3.3/26
no ipv6 redirects
hsrp 183
ip 3.3.3.4
As you can see in above data, I'm trying to print the data horizontally using below command but the data in not printing as expected because some of the paragraph contains 3,4,7 and I want this to follow 7 line sorting or highest line sorting.
Output of Above Data
interface Vlan198, description *****DC-New backup server installation*****, no ip redirects, ip address 172.1.71.130/29, no ipv6 redirects, hsrp 198, ip 172.1.71.129
interface Vlan1004, no ip redirects, no ipv6 redirects,interface Vlan184, description **** Reserved for Tetration Implementation2 ****, no ip redirects, ip address 172.1.122.195/26
no ipv6 redirects, hsrp 184, ip 172.1.122.193,interface Vlan750, no ip redirects, ip address 172.1.113.2/24, no ipv6 redirects
hsrp 188, ip 172.1.113.1,interface Vlan183, description **** Reserved for Tetration Implementation1 ****, no ip redirects, ip address 172.1.122.131/26, no ipv6 redirects
hsrp 183, ip 172.1.122.129,
Command that I'm using
cat test2 | grep -i 'interface\|description\|ip address\|hsrp\|ip' | awk -v RS='[,\n]' '{printf "%s%s",$0,(NR%7?",":"\n")}'
Expected Output

Using GNU sed
$ sed -Ez 's/\n +/, /g;s/\n\n/\n/g' input_file
interface Vlan198, description *****T3*****, no ip redirects, ip address 0.0.0.0/29, no ipv6 redirects, hsrp 198, ip 172.1.71.129
interface Vlan1004, no ip redirects, no ipv6 redirects
interface Vlan184, description **** Reserved for T2 ****, no ip redirects, ip address 1.1.1.1/26, no ipv6 redirects, hsrp 184, ip 1.1.1.2
interface Vlan750, no ip redirects, ip address 2.2.2.2/24, no ipv6 redirects, hsrp 188, ip 2.2.2.3
interface Vlan183, description **** Reserved for T1 ****, no ip redirects, ip address 3.3.3.3/26, no ipv6 redirects, hsrp 183, ip 3.3.3.4

With awk. First awk removes all leading, trailing and multiple consecutive spaces. The second formats each section into a separate line.
awk '{$1=$1}1' file | awk 'BEGIN{FS=RS; OFS=","; ORS=RS; RS="\n\n|\n$"} {$1=$1}1'
Output:
interface Vlan198,description *****T3*****,no ip redirects,ip address 0.0.0.0/29,no ipv6 redirects,hsrp 198,ip 172.1.71.129
interface Vlan1004,no ip redirects,no ipv6 redirects
interface Vlan184,description **** Reserved for T2 ****,no ip redirects,ip address 1.1.1.1/26,no ipv6 redirects,hsrp 184,ip 1.1.1.2
interface Vlan750,no ip redirects,ip address 2.2.2.2/24,no ipv6 redirects,hsrp 188,ip 2.2.2.3
interface Vlan183,description **** Reserved for T1 ****,no ip redirects,ip address 3.3.3.3/26,no ipv6 redirects,hsrp 183,ip 3.3.3.4
See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Assumptions:
all blocks start with `interface' as the 1st field in the line
we are to display all lines within a block (ie, we are not filtering out, or ignoring, any lines)
One awk idea:
awk '
function print_line() { if (line) print line; line="" }
$1=="interface" { print_line() } # start of a new block so flush last block
{ $1=$1 # strip leading spaces from 1st field
if ($0) # if line is not empty then ...
line=line (line ? ", " : "") $0 # append to "line"
}
END { print_line() } # flush last set of data
' test2
This generates:
interface Vlan198, description *****T3*****, no ip redirects, ip address 0.0.0.0/29, no ipv6 redirects, hsrp 198, ip 172.1.71.129
interface Vlan1004, no ip redirects, no ipv6 redirects
interface Vlan184, description **** Reserved for T2 ****, no ip redirects, ip address 1.1.1.1/26, no ipv6 redirects, hsrp 184, ip 1.1.1.2
interface Vlan750, no ip redirects, ip address 2.2.2.2/24, no ipv6 redirects, hsrp 188, ip 2.2.2.3
interface Vlan183, description **** Reserved for T1 ****, no ip redirects, ip address 3.3.3.3/26, no ipv6 redirects, hsrp 183, ip 3.3.3.4

Related

HTTPS setup problem with Virtual IP address

We are setting up for the HTTP configuration for our project with the httpd
But our physical IP address and the virtual IP address is not the same
Where the physical IP address is system IP address and virtual IP address is added in DNS configuration.
We need to setup httpd with the virtual IP address. not with the system IP address.
How can I do this without changing the DNS entry?
Please let me know the configurations for the httpd.
thank you.
You only need to bind http on port 80 (and 443 if you are not off-loading the secure layer).
Every request reaching your apache will be served according to the configuration (plain, virtualhost, etc).

What's different in ::1 and ::ffff:127.0.0.1

i'm on using express and making my website.
but calling API on different port, assigned different ip.
both used req.ip
localhost:3000 (Origin) -> ::1 / localhost:8080 (Other server) -> ::ffff:127.0.0.1
::1 is the IPv6 loopback address
::ffff:127.0.0.1 is the IPv4 loopback address, written as an IPv6 address
Apparently your software uses IPv6 sockets internally, so both IPv4 and IPv6 are handled with the IPv6 implementation. This is done more and more because it makes it easier to write software that supports both.
IPv6 addresses are shown using their recommended representation, and IPv4 addresses are shown as IPv4-mapped IPv6 addresses with the corresponding special notation.
So the difference you are seeing is between an IPv6 connection and an IPv4 connection. Both are valid ways to connect to localhost.

Apache2/httpd not logging ip address

Apache 2 logging client ip address as -. It seems like the header does not carry the client ip address. how to change this?

DKIM TrustedHosts file issue

Can anyone tell me why this works.
# OPENDKIM TRUSTED HOSTS
# To use this file, uncomment the #ExternalIgnoreList and/or the InternalHosts
# option in /etc/opendkim.conf then restart OpenDKIM. Additional hosts
# may be added on separate lines (IP addresses, hostnames, or CIDR ranges).
# The localhost IP (127.0.0.1) should always be the first entry in this file.
127.0.0.1
137.99.0.0/16
::1
#host.example.com
#192.168.1.0/24
Even though documentation says "hostnames" are allowed this fails to have Postfix sign the email.
# OPENDKIM TRUSTED HOSTS
# To use this file, uncomment the #ExternalIgnoreList and/or the #InternalHosts
# option in /etc/opendkim.conf then restart OpenDKIM. Additional hosts
# may be added on separate lines (IP addresses, hostnames, or CIDR ranges).
# The localhost IP (127.0.0.1) should always be the first entry in this file.
127.0.0.1
#137.99.0.0/16
::1
appmail.uconn.edu
#host.example.com
#192.168.1.0/24
I send emails from T-bird with an IMAP account and FROM= alf02013#appmail.uconn.edu
thank you.
-Angelo
Make sure your file that looks like this:
127.0.0.1
hostname1.example1.com
example1.com
hostname1.example2.com
example2.com
The TrustedHosts file tells OpenDKIM who to let use your keys. Because it's referenced by the ExternalIgnoreList directive in your conf file, OpenDKIM will ignore this list of hosts when verifying incoming mail and because it's also referenced by the InternalHosts directive, this same list of hosts will be considered "internal," and OpenDKIM will sign their outgoing mail.
IMPORTANT: Make sure you list the IP address for localhost (127.0.0.1) in the TrustedHosts file or OpenDKIM won't sign mail sent from this server. If you have multiple servers on the same network that relay mail through this server and you want to sign their mail as well, they must be listed in the TrustedHosts file. Put each entry on its own line. An entry can be a hostname, domain name (e.g. "example.com"), IP address, an IPv6 address (including an IPv4 mapped address), or a CIDR-style IP specification (e.g. "192.168.1.0/24?).

Apache logs -- what is difference between %a and %h?

%a is "Remote IP-address" and %h is "Remote host", but when I test it, both print out the same IP addres. What's the difference?
sample log output for log format "%a %h:
192.168.1.2 192.168.1.2
192.168.1.2 192.168.1.2
192.168.1.2 192.168.1.2
The remote host value tries to perform a DNS lookup on the IP address to give you a hostname, if the resolveHosts attribute is set to True. If not, remote host just returns the remote IP address.
%h is the host name and %a is the IP address. %h will only show the host name if apache is doing name resolution. This can signficantly slow down your server and is generally not recommended.
The following description of %h is taken directly from the Apache documentation :-
This is the IP address of the client (remote host) which made the request to the server. If HostnameLookups is set to On, then the server will try to determine the hostname and log it in place of the IP address. However, this configuration is not recommended since it can significantly slow the server. Instead, it is best to use a log post-processor such as logresolve to determine the hostnames. The IP address reported here is not necessarily the address of the machine at which the user is sitting. If a proxy server exists between the user and the server, this address will be the address of the proxy, rather than the originating machine.