Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

Thursday, October 5, 2017

VirtualBox: Accessing shared folder from Ubuntu guest

Preconditions

  • VirtualBox is installed.
  • Ubuntu 16.04 is running in VirtualBox.

Instruction

1. Create shared folder. Click settings → Shared Folders → Click 'Add a new shared folder' button.

Shared folders

2. Specify name and path to the folder you want to make shared.

Add shared folder

3. In VirtualBox click 'Devices' → Insert Guest Additions CD image → Run with sudo permissions VBoxLinuxAdditions.run program from mounted disk.

VBoxLinuxAdditions.run

4. Make folder

sudo mkdir /home/{user_name}/shared-folder 

5. Mount the folder with the command:

sudo mount -t vboxsf {folder_name_from_step_2} /home/{user_name}/shared-folder

6. Done.

Thursday, September 21, 2017

Creating local network with internet access using VirtualBox and Ubuntu 16.04

Preconditions

VirtualBox is installed. Download link.
Ubuntu 16.04 server is downloaded. Download link.

Creating a virtual machine

1. Run VirtualBox and click 'New' button.
2. Specify virtual machine name.

Setting name and operating system.

3. Set RAM (Depending on your purpose it could be any value).

Memory size setup.

4. Select 'Create a virtual hard drive now'.

Hard drive setup.

5. Select VDI.

Hard drive file type setup.

6. Select 'Dynamically allocated'.

Storage on physical hard drive.

7. VirtualBox tries to store the image of the hard drive on disk C, so I recommend to change that and save the image on another logical hard drive.

File location and size.

8. Click 'Create' button.

Installing Ubuntu 16.04 server

1. Select the virtual machine and click 'Settings' button on the left top corner of the window.
2. Open 'Storage' settings → for IDE controller select download Ubuntu 16.04 image.

Storage settings.

3. Make sure that network settings have 'Attached to' set as NAT. We need this for ubuntu installation, in other mode operation apt-get might not work (see Troubleshooting section for more info).

Network settings.

4. Start the virtual machine and follow Ubuntu installation instructions, they are pretty straightforward.

Ubuntu 16.04 installation.


Configuring the virtual machine to work on the local network with internet access.

Now we are ready to configure our virtual machine. To do that follow instructions below:
1. Select the virtual machine and click 'Settings' button on the left top corner of the window.
2. Open 'Network' settings → in 'Attached to' select 'Bridged Adapter'.

Network settings. Bridged apapter.

3. Boot the server.
4. Open /etc/network/interfaces with any text editor.

 sudo nano /etc/network/interfaces

5. Set Ip address and DNS server for the server using the following configuration (NOTE: change IP address in case you are configuring multiple servers).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto <your network>
iface <your network> inet static
        address 192.168.1.2
        netmask 255.255.255.0
        gateway 192.168.1.1
        dns-nameservers 75.75.75.75 75.75.76.76
        dns-search local

Troubleshooting

1) If apt-get command does not work due to IPv6 issue. Please follow instructions below to fix it.
https://askubuntu.com/a/787491/733897 

Sunday, August 28, 2016

Get IP address by MAC address in C#. Updating ARP table.

Recently I have written article about getting IP address of connected to the network device using it MAC address, you can find it using the following link Get IP address by MAC address in C#. It seemed to me that the solution was good until I figured out that it requires some time for ARP table being updated.
To reproduce such situation you just need to reboot your computer or execute command:
arp -d

Run cmd.exe as administrator, type command text and hit enter. Now the code provided in the previous article does not work as expected.

Fixing the issue


The device should be on the same sub-network as running computer. So we need to ping all the IP addresses int the sub-network, e.g. (192.168.1.1 - 192.168.1.255) to update the ARP table. I have updated IpFinder class with necessary code.

IpFinder.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SftpDownloader.Core
{
    public class IpFinder
    {
        public string FindIpAddressByMacAddress(string macAddress, string currentIpAddress)
        {
            Parallel.ForEach(GetListOfSubnetIps(currentIpAddress), delegate (string s)
            {
                DeviceScanner.IsHostAccessible(s);
            });

            var arpEntities = new ArpHelper().GetArpResult();
            var ip = arpEntities.FirstOrDefault(
                a => string.Equals(
                    a.MacAddress,
                    macAddress,
                    StringComparison.CurrentCultureIgnoreCase))?.Ip;

            return ip;
        }

        private List<string> GetListOfSubnetIps(string currentIp)
        {
            var a = currentIp.Split('.');
            var lst = new List<string>();

            for (int i = 1; i <= 255; i++)
            {
                lst.Add($"{a[0]}.{a[1]}.{a[2]}.{i}");
            }

            lst.Remove(currentIp);

            return lst;
        }
    }
}

DeviceScanner.cs


using System.Net.NetworkInformation;

namespace SftpDownloader.Core
{
    public class DeviceScanner
    {
        public static bool IsHostAccessible(string hostNameOrAddress)
        {
            Ping ping = new Ping();
            PingReply reply = ping.Send(hostNameOrAddress, 1000);

            return reply != null && reply.Status == IPStatus.Success;
        }
    }
}

Monday, August 15, 2016

Get IP address by MAC address in C#

Sometimes it happens that you need to access device in the network by IP address. But unfortunately, it is not every time possible, e.g the device does not have static IP. In such case you can use device's MAC address to find it in the network.

Address Resolution Protocol 

Displays and modifies entries in the Address Resolution Protocol (ARP) cache, which contains one or more tables that are used to store IP addresses and their resolved Ethernet or Token Ring physical addresses. There is a separate table for each Ethernet or Token Ring network adapter installed on your computer.
So we are going to use following arp-command to get all the entries and filter them by specified MAC address.

arp -a

For more option use following link ARP Documentation.

.NET implementation


ArpEntity.cs


namespace ArpExample
{
    public class ArpEntity
    {
        public string Ip { get; set; }

        public string MacAddress { get; set; }

        public string Type { get; set; }
    }
}

ArpHelper.cs


using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;

namespace ArpExample
{
    public class ArpHelper
    {
        public List<ArpEntity> GetArpResult()
        {
            var p = Process.Start(new ProcessStartInfo("arp", "-a")
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true
            });

            var output = p?.StandardOutput.ReadToEnd();
            p?.Close();

            return ParseArpResult(output);
        }

        private List<ArpEntity> ParseArpResult(string output)
        {
            var lines = output.Split('\n').Where(l => !string.IsNullOrWhiteSpace(l));

            var result =
                (from line in lines
                 select Regex.Split(line, @"\s+")
                    .Where(i => !string.IsNullOrWhiteSpace(i)).ToList()
                    into items
                 where items.Count == 3
                 select new ArpEntity()
                 {
                     Ip = items[0],
                     MacAddress = items[1],
                     Type = items[2]
                 });

            return result.ToList();
        }
    }
}

IpFinder.cs


using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ArpExample
{
    public class IpFinder
    {
        public string FindIpAddressByMacAddress(string macAddress)
        {
            List<ArpEntity> arpEntities = new ArpHelper().GetArpResult();

            return arpEntities.FirstOrDefault(a => a.MacAddress == macAddress)?.Ip;
        }
    }
}

Troubleshooting 

Recently I figured out that the solution described in the article sometimes might fail. I wrote the article with the fix. Please find it using the link Get IP address by MAC address in C#. Updating ARP table