Skip to content

Instantly share code, notes, and snippets.

@NabilHunt
Created December 14, 2022 09:17
Show Gist options
  • Select an option

  • Save NabilHunt/1dbbc58e5ad9000c18b5939fb396a34b to your computer and use it in GitHub Desktop.

Select an option

Save NabilHunt/1dbbc58e5ad9000c18b5939fb396a34b to your computer and use it in GitHub Desktop.
/*
* parse output from 'resolvectl' or 'systemd-resolve --status'
* and outputs it in json format
*/
const { execSync } = require("child_process");
function resolvectl_parser_sync(){
const cspr = execSync("systemd-resolve --status | sed -e 's/: /=/' | sed -e 's#[\t ]##g' | awk -F= 'BEGIN{section=\"\"}{if($2!=\"\"){section=$1\" \"; print $1\" \"$2}else if($1!=\"\") {print section$1} else {section=\"\"; print section}}' ").toString();
var lines = cspr.split("\n");
var data = {};
var sub_data = {};
var section_title="";
for (var i in lines){
if(lines[i] === "")
{
data[section_title] = sub_data;
sub_data = {};
section_title = "";
} else if(lines[i].split(" ").length === 1)
{
section_title=lines[i];
} else
{
if(lines[i].split(" ")[0] in sub_data)
sub_data[lines[i].split(" ")[0]] = sub_data[lines[i].split(" ")[0]].concat(lines[i].split(" ")[1]);
else
sub_data[lines[i].split(" ")[0]] = [lines[i].split(" ")[1]];
}
}
if(section_title != ""){
data[section_title] = sub_data;
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment