wg show: Add json output
PATCH 1/1 wg show: Add json output
This patch adds a new option to wg show called json. When specified it'll output the WG interface in json output.
It works with both given an interface or all.
# wg show wg1 json
[
{
"interface": "wg1",
"public_key": "Yh0kKjoqnJsxbCsTkQ/3uncEhdqa+EtJXCYcVzMdugs=",
"private_key": "MKVTz8NCXL0uaE77MJfgIWSSy1AfqSmLFRx16oxmrmk=",
"port": "51831",
"fwmark": null,
"peers": [
{
"peer": "GzY59HlXkCkfXl9uSkEFTHzOtBsxQFKu3KWGFH5P9Qc=",
"preshared_key": null,
"endpoint": "172.16.3.104:51834",
"allowed_ips": [
"10.0.3.0/24", "224.0.0.0/8", "172.16.0.0/16"
],
"latest_handshake": 1581909869,
"transfer": {
"received": 3949216,
"sent": 4531428
},
"persistent_keepalive": 30
}
]
}
]Note however, that this will print out the private key.
--- src/show.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-)
src/show.c +72 -3
@@ -202,7 +202,62 @@ static char *bytes(uint64_t b)static const char *COMMAND_NAME ;static void show_usage (void ) { -fprintf (stderr,"Usage: %s %s { <interface> | all | interfaces } [public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump]\n" ,PROG_NAME ,COMMAND_NAME ); +fprintf (stderr,"Usage: %s %s { <interface> | all | interfaces } [public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump | json]\n" ,PROG_NAME ,COMMAND_NAME ); +} + +static void json_print (struct wgdevice *device) +{ +struct wgpeer *peer; +struct wgallowedip *allowedip; +terminal_printf (TERMINAL_RESET ); +terminal_printf (" {\n" ); +terminal_printf (" \"interface\": \"%s\",\n" , device->name); +terminal_printf (" \"public_key\": \"%s\",\n" ,key (device->public_key)); +terminal_printf (" \"private_key\": \"%s\",\n" ,key (device->private_key)); +terminal_printf (" \"port\": \"%d\",\n" , device->listen_port); +if (device->fwmark) +terminal_printf (" \"fwmark\": \"0x%x\",\n" , device->fwmark); +else +terminal_printf (" \"fwmark\": null,\n" ); +terminal_printf (" \"peers\": [\n" ); +sort_peers (device); +for_each_wgpeer (device, peer) { +terminal_printf (" {\n" ); +terminal_printf (" \"peer\": \"%s\",\n" ,key (peer->public_key)); +if (peer->flags &WGPEER_HAS_PRESHARED_KEY ) +terminal_printf (" \"preshared_key\": \"%s\",\n" ,key (peer->preshared_key)); +else +terminal_printf (" \"preshared_key\": null,\n" ); +if (peer->endpoint.addr.sa_family ==AF_INET || peer->endpoint.addr.sa_family ==AF_INET6 ) +terminal_printf (" \"endpoint\": \"%s\",\n" ,endpoint (&peer->endpoint.addr)); +else +terminal_printf (" \"endpoint\": null,\n" ); +terminal_printf (" \"allowed_ips\": [\n" ); +if (peer->first_allowedip) { +terminal_printf (" " ); +for_each_wgallowedip (peer, allowedip) +terminal_printf ("\"%s/%u\"%s" ,ip (allowedip), allowedip->cidr, allowedip->next_allowedip ?", " :"\n" ); + } +terminal_printf (" ],\n" ); +if (peer->last_handshake_time.tv_sec) +terminal_printf (" \"latest_handshake\": %ld,\n" , peer->last_handshake_time.tv_sec); +else +terminal_printf (" \"latest_handshake\": null,\n" ); +terminal_printf (" \"transfer\": {\n" ); +terminal_printf (" \"received\": %ld,\n" , peer->rx_bytes); +terminal_printf (" \"sent\": %ld\n" , peer->tx_bytes); +terminal_printf (" },\n" ); +if (peer->persistent_keepalive_interval) +terminal_printf (" \"persistent_keepalive\": %d\n" , peer->persistent_keepalive_interval); +else +terminal_printf (" \"persistent_keepalive\": null\n" ); +if (peer->next_peer) +terminal_printf (" },\n" ); +else +terminal_printf (" }\n" ); + } +terminal_printf (" ]\n" ); +terminal_printf (" }" ); }static void pretty_print (struct wgdevice *device)
@@ -396,6 +451,8 @@ int show_main(int argc, char *argv[]) } ret = !!*interfaces; interface = interfaces; +if (argc ==3 && !strcmp (argv[2 ],"json" )) +terminal_printf ("[\n" );for (size_t len =0 ; (len =strlen (interface)); interface += len +1 ) {struct wgdevice *device =NULL ;
@@ -404,7 +461,13 @@ int show_main(int argc, char *argv[])continue ; }if (argc ==3 ) { -if (!ugly_print (device, argv[2 ], true)) { +if (!strcmp (argv[2 ],"json" )) { +json_print (device); +if (strlen (interface + len +1 )) +terminal_printf (",\n" ); +else +terminal_printf ("\n" ); + }else if (!ugly_print (device, argv[2 ], true)) { ret =1 ;free_wgdevice (device);break ;
@@ -417,6 +480,8 @@ int show_main(int argc, char *argv[])free_wgdevice (device); ret =0 ; } +if (argc ==3 && !strcmp (argv[2 ],"json" )) +terminal_printf ("]\n" );free (interfaces); }else if (!strcmp (argv[1 ],"interfaces" )) {char *interfaces, *interface;
@@ -444,7 +509,11 @@ int show_main(int argc, char *argv[])return 1 ; }if (argc ==3 ) { -if (!ugly_print (device, argv[2 ], false)) +if (!strcmp (argv[2 ],"json" )) { +terminal_printf ("[\n" ); +json_print (device); +terminal_printf ("\n]\n" ); + }else if (!ugly_print (device, argv[2 ], false)) ret =1 ; }else pretty_print (device);
Signature · 6 lines
2.22.0 _______________________________________________ WireGuard mailing list [email protected] https://lists.zx2c4.com/mailman/listinfo/wireguard
Someone asked about this in the IRC channel, so quickly wrote up a patch to add it. No pressure if you do/don't want to merge it :)
Matt
Quoted 177 lines · Matthew Oliver
This patch adds a new option to
wg showcalled json. When specified it'll output the WG interface in json output.It works with both given an interface or all.
# wg show wg1 json [ { "interface": "wg1", "public_key": "Yh0kKjoqnJsxbCsTkQ/3uncEhdqa+EtJXCYcVzMdugs=", "private_key": "MKVTz8NCXL0uaE77MJfgIWSSy1AfqSmLFRx16oxmrmk=", "port": "51831", "fwmark": null, "peers": [ { "peer": "GzY59HlXkCkfXl9uSkEFTHzOtBsxQFKu3KWGFH5P9Qc=", "preshared_key": null, "endpoint": "172.16.3.104:51834", "allowed_ips": [ "10.0.3.0/24", "224.0.0.0/8", "172.16.0.0/16" ], "latest_handshake": 1581909869, "transfer": { "received": 3949216, "sent": 4531428 }, "persistent_keepalive": 30 } ] } ]Note however, that this will print out the private key.
Signed-off-by Matthew Oliver <[email protected]>--- src/show.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-)src/show.c +0
-1@@ -202,7 +202,62 @@ static char *bytes(uint64_t b)static const char *COMMAND_NAME ;static void show_usage (void ) { - fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces }[public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump]\n",
PROG_NAME,COMMAND_NAME);
+ fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces }
[public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump | json]\n",PROG_NAME,COMMAND_NAME);src/show.c +10
-0+} + +static void json_print (struct wgdevice *device) +{ +struct wgpeer *peer; +struct wgallowedip *allowedip; +terminal_printf (TERMINAL_RESET ); +terminal_printf (" {\n" ); +terminal_printf (" \"interface\": \"%s\",\n" , device->name); + terminal_printf(" \"public_key\": \"%s\",\n",
key(device->public_key));
+ terminal_printf(" \"private_key\": \"%s\",\n",key(device->private_key));src/show.c +3
-0+terminal_printf (" \"port\": \"%d\",\n" , device->listen_port); +if (device->fwmark) + terminal_printf(" \"fwmark\": \"0x%x\",\n",
device->fwmark);src/show.c +7
-0+else +terminal_printf (" \"fwmark\": null,\n" ); +terminal_printf (" \"peers\": [\n" ); +sort_peers (device); +for_each_wgpeer (device, peer) { +terminal_printf (" {\n" ); + terminal_printf(" \"peer\": \"%s\",\n",
key(peer->public_key));src/show.c +2
-0+if (peer->flags &WGPEER_HAS_PRESHARED_KEY ) + terminal_printf(" \"preshared_key\":\"%s\",\n",
key(peer->preshared_key));src/show.c +2
-0+else + terminal_printf(" \"preshared_key\":null,\n");
+ if (peer->endpoint.addr.sa_family==AF_INET||peer->endpoint.addr.sa_family==AF_INET6)
+ terminal_printf(" \"endpoint\":
\"%s\",\n",endpoint(&peer->endpoint.addr));src/show.c +7
-0+else +terminal_printf (" \"endpoint\": null,\n" ); +terminal_printf (" \"allowed_ips\": [\n" ); +if (peer->first_allowedip) { +terminal_printf (" " ); +for_each_wgallowedip (peer, allowedip) + terminal_printf("\"%s/%u\"%s",ip(allowedip),
allowedip->cidr,allowedip->next_allowedip? ", " : "\n");src/show.c +4
-0+ } +terminal_printf (" ],\n" ); +if (peer->last_handshake_time.tv_sec) + terminal_printf(" \"latest_handshake\":%ld,\n",
peer->last_handshake_time.tv_sec);src/show.c +2
-0+else + terminal_printf(" \"latest_handshake\":null,\n");
src/show.c +2
-0+terminal_printf (" \"transfer\": {\n" ); + terminal_printf(" \"received\": %ld,\n",
peer->rx_bytes);
+ terminal_printf(" \"sent\": %ld\n",peer->tx_bytes);src/show.c +22
-1+terminal_printf (" },\n" ); +if (peer->persistent_keepalive_interval) +terminal_printf (" \"persistent_keepalive\": %d\n" , peer->persistent_keepalive_interval); +else +terminal_printf (" \"persistent_keepalive\": null\n" ); +if (peer->next_peer) +terminal_printf (" },\n" ); +else +terminal_printf (" }\n" ); + } +terminal_printf (" ]\n" ); +terminal_printf (" }" ); }static void pretty_print (struct wgdevice *device)@@ -396,6 +451,8 @@ int show_main(int argc, char *argv[]) } ret = !!*interfaces; interface = interfaces; +if (argc ==3 && !strcmp (argv[2 ],"json" )) +terminal_printf ("[\n" );for (size_t len =0 ; (len =strlen (interface)); interface += len +1 ) {struct wgdevice *device =NULL ;@@ -404,7 +461,13 @@ int show_main(int argc, char *argv[])continue ; }if (argc ==3 ) { -if (!ugly_print (device, argv[2 ], true)) { +if (!strcmp (argv[2 ],"json" )) { +json_print (device); +if (strlen (interface + len +1 )) +terminal_printf (",\n" ); +else +terminal_printf ("\n" ); + }else if (!ugly_print (device, argv[2 ], true)) { ret =1 ;free_wgdevice (device);break;src/show.c +7
-2@@ -417,6 +480,8 @@ int show_main(int argc, char *argv[])free_wgdevice (device); ret =0 ; } +if (argc ==3 && !strcmp (argv[2 ],"json" )) +terminal_printf ("]\n" );free (interfaces); }else if (!strcmp (argv[1 ],"interfaces" )) {char *interfaces, *interface;@@ -444,7 +509,11 @@ int show_main(int argc, char *argv[])return 1 ; }if (argc ==3 ) { -if (!ugly_print (device, argv[2 ], false)) +if (!strcmp (argv[2 ],"json" )) { +terminal_printf ("[\n" ); +json_print (device); +terminal_printf ("\n]\n" ); + }else if (!ugly_print (device, argv[2 ], false)) ret =1 ; }else pretty_print (device); --
Quoted 3 lines
2.22.0
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
wg(8) is a unix text utility.
However, we have a script in contrib for accomplishing this same thing:
https://git.zx2c4.com/wireguard-tools/tree/contrib/json
Quoted 186 lines · On Sun, Feb 23, 2020, 11:36 Matthew Oliver <[email protected]> wrote:
Someone asked about this in the IRC channel, so quickly wrote up a patch to add it. No pressure if you do/don't want to merge it :)
Matt
Quoted 179 lines · Matthew Oliver
This patch adds a new option to
wg showcalled json. When specified it'll output the WG interface in json output.It works with both given an interface or all.
# wg show wg1 json [ { "interface": "wg1", "public_key": "Yh0kKjoqnJsxbCsTkQ/3uncEhdqa+EtJXCYcVzMdugs=", "private_key": "MKVTz8NCXL0uaE77MJfgIWSSy1AfqSmLFRx16oxmrmk=", "port": "51831", "fwmark": null, "peers": [ { "peer": "GzY59HlXkCkfXl9uSkEFTHzOtBsxQFKu3KWGFH5P9Qc=", "preshared_key": null, "endpoint": "172.16.3.104:51834", "allowed_ips": [ "10.0.3.0/24", "224.0.0.0/8", "172.16.0.0/16" ], "latest_handshake": 1581909869, "transfer": { "received": 3949216, "sent": 4531428 }, "persistent_keepalive": 30 } ] } ]Note however, that this will print out the private key.
Signed-off-by Matthew Oliver <[email protected]>--- src/show.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-)src/show.c +0
-1@@ -202,7 +202,62 @@ static char *bytes(uint64_t b)static const char *COMMAND_NAME ;static void show_usage (void ) { - fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces }[public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump]\n",
PROG_NAME,COMMAND_NAME);
+ fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces }
[public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump | json]\n",PROG_NAME,COMMAND_NAME);src/show.c +10
-0+} + +static void json_print (struct wgdevice *device) +{ +struct wgpeer *peer; +struct wgallowedip *allowedip; +terminal_printf (TERMINAL_RESET ); +terminal_printf (" {\n" ); +terminal_printf (" \"interface\": \"%s\",\n" , device->name); + terminal_printf(" \"public_key\": \"%s\",\n",
key(device->public_key));
+ terminal_printf(" \"private_key\": \"%s\",\n",key(device->private_key));src/show.c +3
-0+terminal_printf (" \"port\": \"%d\",\n" , device->listen_port); +if (device->fwmark) + terminal_printf(" \"fwmark\": \"0x%x\",\n",
device->fwmark);src/show.c +7
-0+else +terminal_printf (" \"fwmark\": null,\n" ); +terminal_printf (" \"peers\": [\n" ); +sort_peers (device); +for_each_wgpeer (device, peer) { +terminal_printf (" {\n" ); + terminal_printf(" \"peer\": \"%s\",\n",
key(peer->public_key));src/show.c +2
-0+if (peer->flags &WGPEER_HAS_PRESHARED_KEY ) + terminal_printf(" \"preshared_key\":\"%s\",\n",
key(peer->preshared_key));src/show.c +2
-0+else + terminal_printf(" \"preshared_key\":null,\n");
+ if (peer->endpoint.addr.sa_family==AF_INET||peer->endpoint.addr.sa_family==AF_INET6)
+ terminal_printf(" \"endpoint\":
\"%s\",\n",endpoint(&peer->endpoint.addr));src/show.c +7
-0+else +terminal_printf (" \"endpoint\": null,\n" ); +terminal_printf (" \"allowed_ips\": [\n" ); +if (peer->first_allowedip) { +terminal_printf (" " ); +for_each_wgallowedip (peer, allowedip) + terminal_printf("\"%s/%u\"%s",ip(allowedip),
allowedip->cidr,allowedip->next_allowedip? ", " : "\n");src/show.c +4
-0+ } +terminal_printf (" ],\n" ); +if (peer->last_handshake_time.tv_sec) + terminal_printf(" \"latest_handshake\":%ld,\n",
peer->last_handshake_time.tv_sec);src/show.c +2
-0+else + terminal_printf(" \"latest_handshake\":null,\n");
src/show.c +2
-0+terminal_printf (" \"transfer\": {\n" ); + terminal_printf(" \"received\": %ld,\n",
peer->rx_bytes);
+ terminal_printf(" \"sent\": %ld\n",peer->tx_bytes);src/show.c +22
-1+terminal_printf (" },\n" ); +if (peer->persistent_keepalive_interval) +terminal_printf (" \"persistent_keepalive\": %d\n" , peer->persistent_keepalive_interval); +else +terminal_printf (" \"persistent_keepalive\": null\n" ); +if (peer->next_peer) +terminal_printf (" },\n" ); +else +terminal_printf (" }\n" ); + } +terminal_printf (" ]\n" ); +terminal_printf (" }" ); }static void pretty_print (struct wgdevice *device)@@ -396,6 +451,8 @@ int show_main(int argc, char *argv[]) } ret = !!*interfaces; interface = interfaces; +if (argc ==3 && !strcmp (argv[2 ],"json" )) +terminal_printf ("[\n" );for (size_t len =0 ; (len =strlen (interface)); interface += len +1 ) {struct wgdevice *device =NULL ;@@ -404,7 +461,13 @@ int show_main(int argc, char *argv[])continue ; }if (argc ==3 ) { -if (!ugly_print (device, argv[2 ], true)) { +if (!strcmp (argv[2 ],"json" )) { +json_print (device); +if (strlen (interface + len +1 )) +terminal_printf (",\n" ); +else +terminal_printf ("\n" ); + }else if (!ugly_print (device, argv[2 ], true)) { ret =1 ;free_wgdevice (device);break;src/show.c +7
-2@@ -417,6 +480,8 @@ int show_main(int argc, char *argv[])free_wgdevice (device); ret =0 ; } +if (argc ==3 && !strcmp (argv[2 ],"json" )) +terminal_printf ("]\n" );free (interfaces); }else if (!strcmp (argv[1 ],"interfaces" )) {char *interfaces, *interface;@@ -444,7 +509,11 @@ int show_main(int argc, char *argv[])return 1 ; }if (argc ==3 ) { -if (!ugly_print (device, argv[2 ], false)) +if (!strcmp (argv[2 ],"json" )) { +terminal_printf ("[\n" ); +json_print (device); +terminal_printf ("\n]\n" ); + }else if (!ugly_print (device, argv[2 ], false)) ret =1 ; }else pretty_print (device); --2.22.0
Quoted 5 lines
Quoted 1 line
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
Quoted 1 line · Jason A. Donenfeld
wg(8) is a unix text utility.
ip(8) supports --json since 2017.
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
Quoted 2 lines · Matthew Oliver
Someone asked about this in the IRC channel, so quickly wrote up a patch to add it. No pressure if you do/don't want to merge it :)
If you have wireguard-tools package installed then it should be possible to use the incldued wg-json script to do the same
sudo /usr/share/wireguard-tools/examples/json/wg-json
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
Quoted 12 lines · Arti Zirk
Quoted 2 lines · Matthew Oliver
Someone asked about this in the IRC channel, so quickly wrote up a patch to add it. No pressure if you do/don't want to merge it :)
If you have wireguard-tools package installed then it should be possible to use the incldued wg-json script to do the same
sudo
/usr/share/wireguard-tools/examples/json/wg-json
When I built my tools in python on top of the wg command I was please to see a machine interface, but find the \t separated list of values odd to find in a modern app.
I think it would be a good addition to have the JSON output without the need to use a contrib script. Many text commands output in JSON or XML for ease of interfacing.
The JSON would also provide a nice way to add more attributes in the future.
Barry
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard
Hi Barry,
Please read the reply below with a smile, it is just friendly sarcasm underlining my personal view.
Quoted 6 lines · Barry Scott
Quoted 6 lines · Arti Zirk
Quoted 2 lines · Matthew Oliver
Someone asked about this in the IRC channel, so quickly wrote up a patch to add it. No pressure if you do/don't want to merge it :)
Quoted 9 lines
Quoted 4 lines
If you have wireguard-tools package installed then it should be possible to use the incldued wg-json script to do the same
sudo
/usr/share/wireguard-tools/examples/json/wg-jsonWhen I built my tools in python on top of the wg command I was please to see a machine interface, but find the \t separated list of values odd to find in a modern app.
... and it doesn't support dark-mode for the matter! Neither has it autoupdate script!
And no PDF documentation generated by doxygen! And no JNI !
May be it is just a simple & easy, robust tool that does one thing and does it good and fast?
Quoted 4 lines
I think it would be a good addition to have the JSON output without the need to use a contrib script. Many text commands output in JSON or XML for ease of interfacing.
So, sarcasm aside, I really think keeping it simple is better.
If you need JSON for some reason, use the script in contrib and patch if it doesn't work as needed, or suggest another script or way, without strapping a kitchen sink to the core.
And for tighter integration, use the Netlink API (uapi/wireguard.h) directly.
Quoted 3 lines
The JSON would also provide a nice way to add more attributes in the future.
Adding "more attributes" is definitely not on the todo list, AFAIK, and given that in the remote case of adding a secondary output format (json) it may need to be backward compatible with the default text output, this is not a reason.
In the sense that if we add a plugin for Alexa to explain a wg output, auto-magically translating it of course, there is a high chance of it being only a secondary priority (to the simple text output).
Cheers,
Kalin.
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard