Author Topic: Anyone good a AWK scripts?  (Read 2654 times)

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Anyone good a AWK scripts?
« on: July 22, 2013, 10:20:17 PM »
Have the following script that displays output info for the /etc/network/interface file.

I need to modify if device=wlanX

static mode it prints out the 4 network info IPs
dhcp mode it just prints out dhcp

in either mode I need added for it to print out after any previous info:
wpa-ssid
wpa-psk
which are required if it is a wlanX interface....

ex:
for static and wlanX interface:  192.168.0.10 255.255.255.0 192.168.0.1 192.168.0.0 SSID PW  (ip, netmask, gateway, network, SSID, network password)
for dhcp and wlanX interface:  dhcp SSID PW

Anyone good enough to modify this script for me - AWK is not my thing....

Thanks
Myk

Code: [Select]
BEGIN { start = 0;
 
    if (ARGC < 3 || ARGC > 4) {
        print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
        exit 1;
    }
 
    for (i = 2; i < ARGC; i++) {
        if (ARGV[i] == debug) {
            debug = 1;
            continue;
        }
        split(ARGV[i], arg, "=");
        if (arg[1] == "device")
            device = arg[2];
    }
 
    if (!length(device)) {
        print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
        exit 1;
    }
}
 
{
    # Look for iface line and if the interface comes with the device name
    # scan whether it is dhcp or static or manual
    # e.g. iface eth0 inet [static | dhcp | manual]
    if ($1 == "iface")  {
        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {
            if (debug)
                print $0;
            # It's a DHCP interface
            if (match($0, / dhcp/)) {
                print "dhcp";
                exit 0;
                # It's a static network interface. We want to scan the
                # addresses after the static line
            } else if (match ($0, / static/)) {
                static = 1;
                next;
            } else if (match ($0, / manual/)) {
                print "manual";
                exit 0;
            }
 
            # If it is other inteface line, switch it off
            # Go to the next line
        } else {
            static = 0;
            next;
        }
    }
 
    # At here, it means we are after the iface static line of
    # after the device we are searching for
    # Scan for the static content
    if (static) {
 
        if (debug)
            print "static - ", $0, $1;
 
        if ($1 == "address") {
            address = $2;
            gotAddr = 1;
        }
        if ($1 == "netmask") {
            netmask = $2;
            gotAddr = 1;
        }
        if ($1 == "gateway") {
            gateway = $2;
            gotAddr = 1;
        }
        if ($1 == "network") {
            network = $2;
            gotAddr = 1;
        }
    }
}
 
END {
    if (gotAddr) {
        printf("%s %s %s %s\n", address, netmask, gateway, network);
        exit 0;
    } else {
        exit 1;
    }
}

Offline smeighan

  • Developer
  • ******
  • Join Date: Mar 2013
  • Location: Highlands Ranch, Colorado
  • Posts: 1,035
  • Kudos: 11
    • Nutcracker123
Re: Anyone good a AWK scripts?
« Reply #1 on: July 22, 2013, 10:45:05 PM »
I am an awk (well, gawk) programmer since 1985. You may know AWK stands for (Aho, Weinberg, Kerneigh). It was written in 1977 the same year K&R C language was released. BTW, gawk can process ascii files faster than sed or grep. I use it all the time on 2-3 million line ascii files.

Anyways, please post a sample  /etc/network/interface file and i will modify awk script for you

thanks
sean
Sean
Littleton, CO Latest releases http://nutcracker123.com/nutcracker/releases xLights/Nutcracker Forum http://nutcracker123.com/forum/index.php Facebook [url=https://www.facebook.com/groups

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Re: Anyone good a AWK scripts?
« Reply #2 on: July 22, 2013, 11:34:31 PM »
Code: [Select]
auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0

iface wlan0 inet dhcp
  wpa-ssid "TCS"
  wpa-psk "not gonna tell ya :)"

auto eth0:0
iface eth0:0 inet static
  address 192.168.0.10
  netmask 255.255.255.0
  gateway 192.168.0.1
  network 192.168.0.0

iface eth3 inet dhcp


just need it to add the SSID and PW to wlanX interfaces only
As far as I remember the SSID and PW are single words - no spaces etc - that way I can parse the into in the PI network config GUI pages etc

Thanks Sean

Tony
« Last Edit: July 22, 2013, 11:37:43 PM by MyKroFt »

Offline smeighan

  • Developer
  • ******
  • Join Date: Mar 2013
  • Location: Highlands Ranch, Colorado
  • Posts: 1,035
  • Kudos: 11
    • Nutcracker123
Re: Anyone good a AWK scripts?
« Reply #3 on: July 22, 2013, 11:51:03 PM »
i combined two interface files to get this example

Here is the interfaces text file

Code: [Select]
# The loopback network interface
auto lo                         
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address 10.0.0.2
        netmask 255.255.255.252
        gateway 10.0.0.1
        broadcast 10.0.0.3

auto wlan0

iface wlan0 inet dhcp
wpa-driver wext
wpa-ssid MYSSID
wpa-ap-scan 1
wpa-proto RSN
wpa-pairwise CCMP
wpa-group CCMP
wpa-key-mgmt WPA-PSK
wpa-psk MY_HEX_KEY

Here is how it is run

slc01ego:/bmw/sandbox/smeighan/bmw/dashboards/webcat->gawk -f readInterfaces.awk interfaces  device=wlan0
MYSSID MY_HEX_KEY
slc01ego:/bmw/sandbox/smeighan/bmw/dashboards/webcat->gawk -f readInterfaces.awk interfaces  device=eth0
10.0.0.2 255.255.255.252 10.0.0.1


Here is modified awk script


Code: [Select]
BEGIN { start = 0;

if (ARGC < 3 || ARGC > 4) {
print "awk -f readInterfaces.awk interfaces  device=eth0 [debug]"
exit 1;
}

for (i = 2; i < ARGC; i++) {
if (ARGV[i] == debug) {
debug = 1;
continue;
}
split(ARGV[i], arg, "=");
if (arg[1] == "device")
device = arg[2];
}

if (!length(device)) {
print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
exit 1;
}
}

{
# Look for iface line and if the interface comes with the device name
# scan whether it is dhcp or static or manual
# e.g. iface eth0 inet [static | dhcp | manual]
if ($1 == "iface")  {
# Ethernet name matches - switch the line scanning on
if ($2 == device) {
if (debug)
print $0;
# It's a DHCP interface
if (match($0, / dhcp/)) {
dhcp=1;
# It's a static network interface. We want to scan the
# addresses after the static line
} else if (match ($0, / static/)) {
static = 1;
next;
} else if (match ($0, / manual/)) {
print "manual";
exit 0;
}

# If it is other inteface line, switch it off
# Go to the next line
} else {
static = 0;
next;
}
}

# At here, it means we are after the iface static line of
# after the device we are searching for
# Scan for the static content
if (static) {

if (debug)
print "static - ", $0, $1;

if ($1 == "address") {
address = $2;
gotAddr = 1;
}
if ($1 == "netmask") {
netmask = $2;
gotAddr = 1;
}
if ($1 == "gateway") {
gateway = $2;
gotAddr = 1;
}
if ($1 == "network") {
network = $2;
gotAddr = 1;
}
}

if (dhcp) {

if (debug)
print "dhcp - ", $0, $1;
if ($1 == "wpa-ssid") {
wpa_ssid = $2;
got_wpa_ssid = 1;
}

if ($1 == "wpa-psk") {
wpa_psk = $2;
got_psk = 1;
}
}
}

END {
if (gotAddr) {
printf("%s %s %s %s\n", address, netmask, gateway, network); # "static"
}
if(got_wpa_ssid)  {
printf("%s %s\n",  wpa_ssid, wpa_psk); # "dhcp"
}
else
exit 1;
}


Offline smeighan

  • Developer
  • ******
  • Join Date: Mar 2013
  • Location: Highlands Ranch, Colorado
  • Posts: 1,035
  • Kudos: 11
    • Nutcracker123
Re: Anyone good a AWK scripts?
« Reply #4 on: July 22, 2013, 11:54:33 PM »
will the password have embedded spaces? Ill make change to script to handle that if you need it

I dont see quotes on the wpa lines

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Re: Anyone good a AWK scripts?
« Reply #5 on: July 22, 2013, 11:58:05 PM »
no it dont need to spit out the quotes in the result

you have broadcast x.x.x.x
and i have a network x.x.x.x

what is the difference?

Tony

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Re: Anyone good a AWK scripts?
« Reply #6 on: July 22, 2013, 11:59:53 PM »
am wondering if my network needs to be broadcast instead

when i do a ifconfig - i dont see 192.168.0.0 anywhere, but it does list Bcast 192.168.0.255

Tony

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Re: Anyone good a AWK scripts?
« Reply #7 on: July 23, 2013, 12:03:41 AM »
Ok,

Quote
Here is how it is run

slc01ego:/bmw/sandbox/smeighan/bmw/dashboards/webcat->gawk -f readInterfaces.awk interfaces  device=wlan0
MYSSID MY_HEX_KEY
slc01ego:/bmw/sandbox/smeighan/bmw/dashboards/webcat->gawk -f readInterfaces.awk interfaces  device=eth0
10.0.0.2 255.255.255.252 10.0.0.1

if wlan0 is in dhcp mode, it needs to spit out dhcp MYSSID MYKEY
if wlan0 is in static mode, it needs to spit out IP IP IP IP MYSSID MYKEY
if eth0 dhcp it just spits out dhcp
if ech0 static then spits out IP IP IP IP

that way i can test for [1] = dhcp when parsing, if not dhcp then i know the interface is in static mode

am going to change my network to broadcast

Tony
« Last Edit: July 23, 2013, 12:05:55 AM by MyKroFt »

Offline smeighan

  • Developer
  • ******
  • Join Date: Mar 2013
  • Location: Highlands Ranch, Colorado
  • Posts: 1,035
  • Kudos: 11
    • Nutcracker123
Re: Anyone good a AWK scripts?
« Reply #8 on: July 23, 2013, 12:09:35 AM »
With your file

slc01ego:/bmw/sandbox/smeighan/bmw/dashboards/webcat->gawk -f readInterfaces.awk i2  device=eth0:0
192.168.0.10 255.255.255.0 192.168.0.1 192.168.0.0
slc01ego:/bmw/sandbox/smeighan/bmw/dashboards/webcat->gawk -f readInterfaces.awk i2  device=wlan0
"TCS" "not

Note you need to pass in the whole tokem so eth0:0 not eth0

Can you make up a file with all the combinations you want processed. ill make script match the input.

If you want to us ifconfig output as the input to this, then post what it looks like.

I can also make it parse both types of input.

Further, we can get rid of the interface as an input

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Re: Anyone good a AWK scripts?
« Reply #9 on: July 23, 2013, 05:51:36 PM »
the 2 network interfaces I will be pulling data for are eth0 and wlan0.

echo:
if configed static needs to output:  IP1 IP2 IP3 IP4
if configed dhcp needs to output:  dhcp

waln0:
if configed static needs to output:  IP1 IP2 IP3 IP4 SSID NETPW
if configed dhcp needs to output:  dhcp SSID NETPW

nothing more - nothing less

Tony

Offline David Pitts

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: Falcon, CO
  • Posts: 3,924
  • Kudos: 76
Re: Anyone good a AWK scripts?
« Reply #10 on: July 23, 2013, 07:18:04 PM »
the 2 network interfaces I will be pulling data for are eth0 and wlan0.

echo:
if configed static needs to output:  IP1 IP2 IP3 IP4
if configed dhcp needs to output:  dhcp

waln0:
if configed static needs to output:  IP1 IP2 IP3 IP4 SSID NETPW
if configed dhcp needs to output:  dhcp SSID NETPW

nothing more - nothing less

Tony

Here it is....

Code: [Select]
#!/usr/bin/awk -f
BEGIN { start = 0;
 
    if (ARGC < 3 || ARGC > 4) {
        print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
        exit 1;
    }
 
    for (i = 2; i < ARGC; i++) {
        if (ARGV[i] == debug) {
            debug = 1;
            continue;
        }
        split(ARGV[i], arg, "=");
        if (arg[1] == "device")
            device = arg[2];
    }
 
    if (!length(device)) {
        print "awk -f readInterfaces.awk <interfaces file> device=<eth device> [debug]"
        exit 1;
    }
}
 
{
    # Look for iface line and if the interface comes with the device name scan whether it is
    # dhcp or static or manual e.g. iface eth0 inet [static | dhcp | manual]
    if ($1 == "iface") {
        # Ethernet name matches - switch the line scanning on
        if ($2 == device) {
            if (debug)
                print $0;
            # It's a DHCP interface
            if (match($0, / dhcp/)) {
                printf("dhcp ");
                dhcp = 1;
                next;
                 # It's a static network interface. We want to scan the addresses after the
                # static line
            } else if (match ($0, / static/)) {
                printf("static ");
                static = 1;
                next;
            } else if (match ($0, / manual/)) {
                print "manual";
                exit 0;
            }
 
            # If it is other inteface line, switch it off Go to the next line
        } else {
            static = 0;
            dhcp = 0;
            next;
        }
    }
 
    # At here, it means we are after the iface static line of after the device we are
    # searching for Scan for the static content
    if (static || dhcp) {
 
        if (debug)
            print "static - ", $0, $1;
 
        if ($1 == "address") {
            address = $2;
            gotAddr = 1;
        }
        if ($1 == "netmask") {
            netmask = $2;
            gotAddr = 1;
        }
        if ($1 == "gateway") {
            gateway = $2;
            gotAddr = 1;
        }
        if ($1 == "network") {
            network = $2;
            gotAddr = 1;
        }
        if ($1 == "wpa-ssid") {
            ssid = $2;
            gotWPA = 1;
        }
        if ($1 == "wpa-psk") {
            psk = $2;
            gotWPA = 1;
        }
    }
}
 
END{

    if (gotAddr) {
        printf("%s %s %s %s ", address, netmask, gateway, network);
    }
    if (gotWPA) {
        printf("%s %s", ssid, psk);
    }
    printf("\n");
    exit 1;
}
PixelController, LLC
PixelController.com

Offline MyKroFt

  • Administrator
  • *****
  • Join Date: Mar 2013
  • Location: NC Montana
  • Posts: 1,418
  • Kudos: 57
Re: Anyone good a AWK scripts?
« Reply #11 on: July 24, 2013, 09:36:57 AM »
next script:

I need this script to support the wpa-ssid and wpa-psk parameters.

Again, these 2 need to be present whether in static or dhcp mode

Thanks


[attachment deleted by admin]

 

Back to top