1

How can I create an output file from ARP in json format?

I have a raspberry pi running openremote2 and want to format an output file that openremote understands - here is an example

{
  'command':'ELAPSE',
  'value':53680,
  'mute':1,
  'shuffle':0,
  'repeat':0,
  'play':1,
  'volume':0,
  'TotalTime':0,
  'favstatus':0
}

so the arp command looks like this:-

sudo arp-scan 192.168.0.5-192.168.0.5

and the output look like this:-

Interface: eth0, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.8.1 with 1 hosts (http://www.nta-monitor.com/tools/arp-scan/)
1 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.8.1: 1 hosts scanned in 1.684 seconds (0.59 hosts/sec). 0 responded

when the device is powered up I get this:-

Interface: eth0, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.8.1 with 1 hosts (http://www.nta-monitor.com/tools/arp-scan/)
192.168.0.5     08:eb:74:9e:00:f4       (Unknown)
2 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.8.1: 1 hosts scanned in 1.405 seconds (0.71 hosts/sec). 1 responded

The plan here is to create an output file that either contains or doesn't contain the IP which denotes whether the device is on the network or not IE-powered up.

  • The most used OS is Raspbian which is Debian based. Most of the other OS use the Linux kernel (the obvious exception being Windows 10 and riscOS). Your question does not appear to be specific to the Raspberry Pi. – joan May 16 '16 at 18:14
  • While I notice some standard tools are starting to add json output as an option in new versions, I think you'll probably have to parse this yourself. Someone may have some suggestions about the simplest possibilities for that. – goldilocks May 16 '16 at 18:20

2 Answers2

1

If you really want JSON-formatted output, you can use awk's printf command.

You want a lot of ' single-quote characters in your json-formatted output so it's easier to define the printf format string outside of the actual awk script, using -v to set an awk variable called fmt.

sudo arp-scan 192.168.0.5-192.168.0.5 | 
    awk -F'\t' -v fmt="{\n 'ip': '%s',\n 'mac': %'s',\n 'vendor': '%s'\n}\n" \
        '$2 ~ /([0-9a-f][0-9a-f]:){5}/ {printf fmt,  $1, $2, $3}'

arp-scan's output is TAB-delimited, so this awk script checks that the second field ($2) looks like a MAC address. If it does, it pretty-prints the fields using printf. If it doesn't, it doesn't get printed.

Output (based on your example):

{
 'ip': '192.168.0.5',
 'mac': '08:eb:74:9e:00:f4',
 'vendor': '(Unknown)'
}

If you just want the IP addresss in plain text, it's a lot simpler:

sudo arp-scan 192.168.0.5-192.168.0.5 | 
    awk -F'\t' '$2 ~ /([0-9a-f][0-9a-f]:){5}/ {print $1}'

Output:

192.168.0.5

(or nothing if the IP doesn't respond)

cas
  • 78,579
0

You give a JSONish example at the star of your question, but really don't ask for that in the summary. Just a file that does or doesn't contain something.

In a way

sudo arp-scan 192.168.0.5-192.168.0.5  | tail -n +2 | head -n -2 | head -1 > out.txt

can do that

If that's not what you want, please edit your question to add an actual example of how you want it to look

infixed
  • 887