Saturday, December 17, 2016

Yesterday we were talking about serial port and the pyserial library. Today we review the vSPC.py library. This is a virtual serial port concentrator that makes the use of VMware telnet extensions.
Its requirements are simple.
 A Linux VM w/Python 2/3
a vSPC.py script installed on the VM.
configure it to start on boot
enable local firewall rules to permit 13370/tcp, 13371/tcp and a range of ports for the targets.
This is enough to configure a virtual serial port concentrator host VM.
To configure the target, we add a serial port to the VM with the following settings:
Type : Network
Direction : Server
Port URI: vSPC.py
Use Virtual Serial Port Concentrator : <checked>
vSPC URI: telnet://vspc.local.lan:13370
Yield CPU on Poll : <checked>

To enumerate the connections from the target, we simply issue
locally : .vSPC.py localhost
remote : vSPC.py vspc.local.lan
The advantages of a serial port concentrator over statically mapped ones include :

It handles dynamic port assignment which can be queried. Static port assignments cannot be queried. The access to the target is never lost.
You no longer need the VMware console access and authorization.
The static ports are mapped onto the ESXi server which requires firewall to be opened for ESXi but vSPC runs on the local VM.
vSPC works on any Linux flavors.

The mechanism is based on RFC2217 - compatible telnet server.  The proxy which  operates as a serial port concentrator has internal connections to several virtual machines in a datacenter and an external connection to a remote system. The concentrator provides a remote system with access to multiple virtual machines that act as serial port servers.
 This is a many to one mapping.
When a remote system makes a connection request to the target on the ESX server, one virtual machine acts as a virtual serial port server and another acts as a client  The proxy listens for connection requests from the remote system and forwards it to the target This is true in the opposite direction as well. The proxy forwards both ways.
#codingexercise
        static int GetKthSmallest(int[,] A, int row, int col, int k)
        {
            int r = 0;
            int c = 0;
            int maxr = 0;
            int maxc = 0;
            int prev = -1;
            if (k <= 0 || k < prev) return -1;
            int count = 0;
            while (count < k)
            {
                // compare right and bottom and increment r or c
                // the idea is that the k largest elements will be a contiguous block 
                // and the next element for inclusion will be either to the right of this block or 
                // at the bottom of this block
                // we just need to propagate the front on all rows one cell at a time.
                if (count + 1 == k) {  return A[r, c]; }

                int rselected = r * col + c; // position of selected at bottom
                int cselected = r * col + c; // position of selected to right
                int rmax = Int16.MaxValue;  // max value on top or at left
                for (int y = 0; y <= c ; y++)
                {
                    if (r >= row - 1) break;
                    var candidate = A[r+1,y];
                    if (r < row - 1 && candidate < rmax &&
                        (prev == -1 || candidate > A[prev/col, prev%col]))
                    {
                        rmax = A[r + 1, y];
                        rselected = (r + 1) * col + y;
                    }
                    if (maxr >= row - 1) break;
                    candidate = A[maxr + 1, y];
                    if (maxr < row - 1 && A[maxr + 1, y] < rmax &&
                        (prev == -1 || candidate > A[prev/col, prev%col]))
                    {
                        rmax = A[maxr + 1, y];
                        rselected = (rmax + 1) * col + y;
                    }
                }
                for (int x = 0; x <= r; x++)
                {
                    if (c >= col - 1) break;
                    var candidate = A[r, c + 1];
                    if (c < col - 1 && candidate < rmax &&
                        (prev == -1 || candidate > A[prev/col, prev%col]))
                    {
                        rmax = A[r, c + 1];
                        rselected = r * col + c + 1;
                    }
                    if (maxc >= col - 1) break;
                    candidate = A[r, maxc + 1];
                    if (maxc < col - 1 && candidate < rmax &&
                        (prev == -1 || candidate > A[prev/col, prev%col]))
                    {
                        rmax = A[r, maxc + 1];
                        rselected = r * col + maxc + 1;
                    }
                }
                prev = r * col + c;
                r = rselected / col;
                c = rselected % col;
                if (r > maxr) maxr = r;
                if (c > maxc) maxc = c;
                count++;
            }
            return A[r, c];
        }
    class Program
    {
        static void Main(string[] args)
        {
            var A = new int [4, 4] {{10, 13, 15, 19 }, {21, 23, 24, 27}, {29, 31, 33, 37}, {41, 43, 45, 47}};
            for (int k = 1; k < 17; k++)
            {
                Console.WriteLine("K={0},Value={1}", k, GetKthSmallest(A, 4, 4, k));
            }
       }
    }

K=1,Value=10
K=2,Value=13
K=3,Value=15
K=4,Value=19
K=5,Value=21
K=6,Value=23
K=7,Value=24
K=8,Value=27
K=9,Value=29
K=10,Value=31
K=11,Value=33
K=12,Value=37
K=13,Value=41
K=14,Value=43
K=15,Value=45
K=16,Value=47

No comments:

Post a Comment