My daughters first social media post

https://www.instagram.com/reel/Cu9_8n5JYJA/?utm_source=ig_web_button_share_sheet&igshid=MzRlODBiNWFlZA==

Parsing XML and Converting to JSON using Golang

Introduction:

Recently I started to learn Golang for solving an interesting engineering problem and finally solved it successfully. Hence thought I can share the problem and solution with others who may also be interested to know. If this help’s someone that would be great.

Problem

Here is the sample XML which we are interested to decode using Golang,

<?xml version="1.0" encoding="utf-8"?>
<testreport id="Product Monitoring" name="Product Monitoring">
    <testclass id="Product Installation State" name="PRODUCT_INST_STATE">
        <testcase id="Product Installation State" name="PRODUCT_INST_STATE" tracker="INT">
            <result>Pass</result>
            <message>Script completed with no issues</message>
            <durationInSecs>32</durationInSecs>
        </testcase>
    </testclass>
    <testclass id="Product Windows Service State" name="PRODUCT_WIN_STATE">
        <testcase id="Product Windows Service State" name="PRODUCT_WIN_STATE" tracker="INT">
            <result>Pass</result>
            <message>Service running with no issues</message>
            <durationInSecs>80</durationInSecs>
        </testcase>
    </testclass>
    <testclass id="Product UI State" name="PRODUCT_UI_STATE">
        <testcase id="Product UI State" name="PRODUCT_UI_STATE" tracker="INT">
            <result>Pass</result>
            <message>Product UI state ON with no issues</message>
            <durationInSecs>7</durationInSecs>
        </testcase>
    </testclass>
</testreport>

How to solve it

The first and foremost we had to create struct’s to Unmarshal the xml to object. Here we have testreport as a root xml node along with testclass and testcase as sub node’s inside it. Here is how we can define structs for each node in child-to-parent hierarchy.

type testcase struct {
		Id             string `xml:"id,attr"`
		Name           string `xml:"name,attr"`
		Tracker        string `xml:"tracker,attr"`
		Result         string `xml:"result"`
		Message        string `xml:"message"`
		DurationInSecs string `xml:"durationInSecs"`
	}
	type testclass struct {
		Id       string   `xml:"id,attr"`
		Name     string   `xml:"name,attr"`
		TestCase testcase `xml:"testcase"`
	}
type testreport struct {
		Id        string      `xml:"id,attr"`
		Name      string      `xml:"name,attr"`
		TestClass []testclass `xml:"testclass"`
	}

Opening the xml in golang, xml file or any file for that matter can be opened using os.Open API available in “OS” package. Quick example on opening the xml file.

package main
import (
	"fmt"
	"io/ioutil"
	"os"
)
func main() {
	xmlFileName := "session.xml"
	xmlFile, err := os.Open(xmlFileName)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Successfully Opened file : ", xmlFileName)
	byteData, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("Overall file content :\n %s \n", byteData)
}

And the final part is Unmarshal the xml using the golang xml library and converting it to JSON. Golang provides xml support via package “encoding/xml” and JSON support via “encoding/json”.

Here is the final code putting together.

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	type testcase struct {
		Id             string `xml:"id,attr"`
		Name           string `xml:"name,attr"`
		Tracker        string `xml:"tracker,attr"`
		Result         string `xml:"result"`
		Message        string `xml:"message"`
		DurationInSecs string `xml:"durationInSecs"`
	}
	type testclass struct {
		Id       string   `xml:"id,attr"`
		Name     string   `xml:"name,attr"`
		TestCase testcase `xml:"testcase"`
	}
	type testreport struct {
		Id        string      `xml:"id,attr"`
		Name      string      `xml:"name,attr"`
		TestClass []testclass `xml:"testclass"`
	}

	xmlFileName := "autosession.xml"
	xmlFile, err := os.Open(xmlFileName)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Successfully Opened file", xmlFileName)
	byteData, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		fmt.Println(err)
	}
	v := testreport{}
	err = xml.Unmarshal(byteData, &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	result, err := json.Marshal(v)
	if nil != err {
		fmt.Println("Error marshalling to JSON", err)
		return
	}
	fmt.Printf("%s\n", result)
}

Here is the final input and output generated using code,

{
    "Id": "Product Monitoring",
    "Name": "Product Monitoring",
    "TestClass": [
        {
            "Id": "Product Installation State",
            "Name": "PRODUCT_INST_STATE",
            "TestCase": {
                "Id": "Product Installation State",
                "Name": "PRODUCT_INST_STATE",
                "Tracker": "INT",
                "Result": "Pass",
                "Message": "Script completed with no issues",
                "DurationInSecs": "32"
            }
        },
        {
            "Id": "Product Windows Service State",
            "Name": "PRODUCT_WIN_STATE",
            "TestCase": {
                "Id": "Product Windows Service State",
                "Name": "PRODUCT_WIN_STATE",
                "Tracker": "INT",
                "Result": "Pass",
                "Message": "Service running with no issues",
                "DurationInSecs": "80"
            }
        },
        {
            "Id": "Product UI State",
            "Name": "PRODUCT_UI_STATE",
            "TestCase": {
                "Id": "Product UI State",
                "Name": "PRODUCT_UI_STATE",
                "Tracker": "INT",
                "Result": "Pass",
                "Message": "Product UI state ON with no issues",
                "DurationInSecs": "7"
            }
        }
    ]
}

Conclusion

As newbie to Golang am really surprised how fast we can prototype something using Golang in short span of time. The tools required are already available and can make our life so much easy for modern programming challenges.

Will see you again, thanks

Creating Snapshot of same Volume/Drive Multiple Times Using VSHADOW/DISKSHADOW

Introduction:

In this blog am going to demonstrate how to create multiple snapshots of same volume/Disk/Drive in Windows environment using VSHADOW/DISKSHADOW.

How to Do this?

In order to perform multiple snapshot operations it is must to have VSS tool’s like VSHADOW(in Windows 2003 R2, SP2 and Windows 2008 Standard except Windows 2008 R2) or DISKSHADOW( only Windows 2008 R2).

So we have to write a DOS batch script or DOS command script to accomplish the multiple snapshot creation. Here am going to demonstarte how we can do it using the DOS script. Construct a DOS for loop based on number of times we want to take shadowCopy(Snapshot). Like

FOR /L %%A IN (0 1 32) DO echo “Something”

here something will be printed in Console 32 times.

Using VSHADOW

Find the following DOS batch script to execute it multiple times,  just copy the following lines into a BAT file. Example ( ShadowCopy32.BAT)

@echo OFF
FOR /L %%A IN (1 1 32) DO VSHADOW -p -nw E:

Here we are trying to take Snapshot/ShadowCopy for E drive.

-p Persistent snapshot/ShadowCopy

-nw – No Writer option.

How to do in Windows 2008 R2( using DISKSHADOW):

In windows 2008 R2 we can do it using DISKSHADOW when we are using diskshadow we can only insert script. So for this we need a Two BAT files usually one for running the for loop and another one will be  set of commands DISHSHADOW has to execute.  But am going to demonstrate how to do this using only one BAT file but purly using DOS commands.

Copy and paste the following lines into a BAT file example( ShadowCpy32.BAT) and run it.

@echo OFF
echo set context persistent >> ExecuteScript.cmd
echo add volume E: >> ExecuteScript.cmd
echo create >> ExecuteScript.cmd
echo reset >> ExecuteScript.cmd
FOR /L %%A IN (1 1 32) DO diskshadow /s ExecuteScript.cmd
del ExecuteScript.cmd

Set Context Persistent – Setting the context as persistent

Add Volume E: – Adding the base volume E: to take Snapshot/Shadow Copy

Create – Starts the ShadowCopy process

reset – Between every successive ShadowCopy process a reset is must.

Here we are creating a ExecuteScript.cmd file dynamically and this will be executed by DISKSHADOW 32 time once if it is completed we can delete the file.

Conclusion: 

Feel free to comment and conduct if you have any queries.

Showing Message from Silent Installer Using InstallAnywhere

Introduction:

In this blog, I am going to demonstrate how to show a Message when we trigger the silent installer in InstallAnywhere.

Purpose of the Blog:

When we execute the Installer in silent mode using (-i silent) the installer will not allow you to show any sort of messages to the user. In some cases we might need to display some warning messages to the user in order to notify them about some failure. So this blog will explain you how to show such a Message to the user even at the silent installation.

How to Do It:

In this example am going to show a Warning Message to the user when un Installation is completed.

Adding the Action Items

Add the action item for Modify Text File single file option. And select the Create New option. And enter the name of the file should be created. The following image shows the actual change.

Add the action Item Execute Script to execute the Script file created using Modify Single File.

Delete the Script file created.

The Final Action Item List looks like in the following screen shot.

How To Test It:

This can be tested using the following command while triggering the un installer. Uninstaller.exe -i silent

Conclusion:

A silent installer is always a silent installer. It is not possible to change the silent installer functionality but when needed we can break it according to our needs.

Thanks

Getting List of Host Names in the Subnet – Windows

Getting List of Host Names in the Subnet – Windows

Introduction:

Identifying host name associated with IP Address in Windows is simple work. So here am going to demonstrate how to get multiple host names which are available in the same subnet.

How we can do it:

Usually we can get the Host name associated with the IP address using the following command.

NBTSTAT -a [Computer IP Address]

The Above given command will return the HOST NAME associated with the IP address given.

Mixing it With DOS Batch Script:

By Mixing this command with DOS Batch script we can get multiple Host names available in the SubNet.

For Loop in DOS Script:

FOR /L %%varaible IN ( START STEP END) DO  COMMAND

%%Variable is the Name of the variable

START- Start value for the FOR Loop

STEP- Step value for the FOR Loop

END- End value for the FOR Loop

COMMAND- Command to execute multiple times

Putting it Together:

Save the following code in a file with .BAT extension


FOR /L %%A IN (0 1 255) DO NBTSTAT -A "192.168.1.%%A">>File.txt

Running the program

So now run the bat file and see the File.txt in the current directory of Bat file it will contain details of the Host names in the SubNet