Converting IP to Binary and Back!

This week on “I learn a weird, semi-useless skill” brings you how I learn to convert 32 bit IP addresses into their binary counter part and back. If I asked you if you have ever or would ever need this knowledge, you’d probably say no. But the learning must continue!

Firstly, lets pick an IP address, this can be any random combination of 4 pairs of 3 digits between 0 and 255. For this guide, I’ll be using 176.42.6.213. For no apparent reason.

So to convert this number into binary, I use a simple table method. You can see the table below:

 128 64 32 16 8 4 2 1

This table makes it very simple to work out how to convert each of the four octets in an IP address to binary.

We go through each of the numbers in the table above and see if the column number can be subtracted from the first octet of the 32-bit address. So our first step would be, can we subtract 128 from 176?

The answer is YES so I would put a “1” in the 128 column, and our remainder from the above sum is “48“. Your table should look similar to the one below.

 128 64 32 16 8 4 2 1 1

Now we can do the next step using our remainder of “48“. Can we subtract 64 from 48?

The answer is NO so I would put a “0” in the 64 column. When the answer is no, then the remainder stays the same, so the next sum would be, can we subtract 32 from 48? This goes on until you are at the end of the table and your remainder should be 0.

The finished table for the 176 part is shown below:

 128 64 32 16 8 4 2 1 1 0 1 1 0 0 0 0

Once you have done this for all 4 octets in the IP address, you should end up with 4 tables filled with 1’s and 0’s. The full binary number for our IP address is:

10110000.00101010.00000110.11010101

Converting Binary to the 32-bit IP address is quite a lot simpler. Once again we use the same table but this time we put the binary number into the table and read which numbers have a 1 and which numbers have a 0 in the column.

For example, plugging the binary of “176” into the table gives us the below result:

 128 64 32 16 8 4 2 1 1 0 1 1 0 0 0 0

From this we can see that the following columns have a 1 assigned to them:

• 128
• 32
• 16

Now all we have to do is add these numbers together to get the first octet of the 32-bit address, which is “176“. Now do this process for the other 3 parts of the binary address and you will get back the original IP address of “176.42.6.213

I would suggest doing this exercise multiple times to really nail it if you need to. Who knows, if you and your company are really sad and nerdy, this might be a nice party trick. Or a particularly cringey 15 minutes.

I think I explained this well enough. If not, please comment and let me know what parts I could improve or add. Enjoy!

P.S You have also want to use this form I created to do this activity: Converting 32-Bit Address to Binary and Back

In this update, you’ll see how I added two or three new scripts to the environment and also changed how most of the scripts work.

One new script that I add was a script that would take CSV data and add the users to a group in Active Directory. You can see this below:

```function Get-CSVAddToGroup{
<#
.SYNOPSIS
This function allows you to add usernames from a CSV to a specified Active Directory group.
It needs confirmation for each addition to a group.

.EXAMPLE
Get-CheckCSVAddToGroup -Groupname "all rossendale staff" -path c:\testing\test.csv

#>
[cmdletbinding()]
param(
[parameter(Mandatory=\$true)]
[string]\$groupname,

[parameter(mandatory=\$true)]
[string]\$path
)

\$csvformembership = Import-Csv -Path \$path

foreach (\$b in \$csvformembership){
\$name = \$b.user
}
}```

Another script I added was my script for sending messages to remote computers on the network. You can see this below:

```function Send-Message{

function send-messagewithname{
\$name = Read-Host "Enter a name"
\$msg = Read-Host "enter a message"

Write-Host "\$name doesn't exist!"
pause
}else{
Invoke-WmiMethod -Path win32_process -Name create -ArgumentList "msg * \$msg" -ComputerName "\$name"
}
}

function send-messagewithip{
\$ip = Read-Host "enter an IP"
\$msg = Read-Host "enter a message"
write-host "\$ip doesn't exist"
pause
}else{
Invoke-WmiMethod -Path win32_process -Name create -ArgumentList "msg * \$msg" -ComputerName "\$ip"
}
}

do {\$nameorip = Read-Host "Do you want to use IP or Name (I or N)?"} while (("i","n") -notcontains \$nameorip)
if (\$nameorip -eq "n"){
send-messagewithname
}else {
send-messagewithip
}
}```

Finally, my last script that was added to my custom PowerShell environment allowed me to list all the computers on the network with a specific operating system. For example “2012” or “Professional”. The code for this is below:

```function list-OSVersonComputers{
<#
.SYNOPSIS
This function allows you to list all of the computers with the requested OS version.

.EXAMPLE
List-OSVersionComputers -OSVersion 2012

.EXAMPLE
List-OSVersionComputers -OSVersion standard
#>
[cmdletbinding()]
param(
[parameter(mandatory=\$true)]
[string]\$OSVersion
)

\$OSVersion2 = '*' + (\$OSVersion) + '*'

Get-ADComputer -Filter {OperatingSystem -like \$OSVersion2} -Property * | Format-Table name,operatingsystem,lastlogondate -Wrap -AutoSize
}```

I also added help menus to most of my scripts to make them more professional.

First of all, make sure that your switch has the most recent firmware installed. That’s just a good rule of thumb if you are experiencing issues. This can be done by downloading the latest firmware from here… You can see where to install the new firmware in the screenshot below:

You want to start this process on your non-passive switch.

Next we want to go to the physical stacking section under the management folder. Here you need to enable stacking mode and reboot the switch for this to take affect. When you have rebooted the switch, go back to the physical stacking section and make the details closely resemble the ones in the screenshot below:

Now you can done that, you want to configure a single IP to manage this stack from. To do this go to the single IP settings under the virtual stacking folder. Here, you want to configure your switch to how the one in the screenshot is configured below:

Just if you needed to know, I have connected my switches together through port 20 on each and used this cable.

This is what my two D-Link switches stacked together looked like, mind the messy cabling:

Hope this was useful to someone! Enjoy!