# v_hostipinfo

## PURPOSE

V_HOSTIPINFO get host name and internet connection information

## SYNOPSIS

function [ipinfo,tt]=v_hostipinfo(m,t)

## DESCRIPTION

    V_HOSTIPINFO get host name and internet connection information
      Usage:    hostname   = v_hostipinfo;        % computer name
                ip_address = v_hostipinfo('i');   % IP address

      Inputs:   m  mode input selects the information to output (see below)
                t  optional output from an "ipconfig -all" command

      Outputs:  ipinfo   cell array with one row per IP connection. The number
                         of columns is detemined by the m input (see below).
                         If only a single output value is selected by the m input, then
                         ipinfo will be a string rather than a cell array.
                tt       output from the "ipconfig -all" command

      The "m" input selects whch IP connections to supply information about
      (rows of IPINFO) and what information to supply (columns of IPINFO).

      Row selection: A   include all connections
                     L   Local Internet Connection [default if m non-empty]
                     B   Bluetooth Connection
                     P   PPP connection (e.g. a VPn connection)

      Col selection: h   hostname (same for all connections) [default if m is empty]
                     c   Connection type code: L=local, B=bluetooth, P=PPP or ?=other
                     t   Connection type (e.g. "Ethernet" or "PPP")
                     n   Connection name (e.g. "Local Area Connection")
                     m   MAC address (e.g. "00-53-45-00-00-00"
                     i   IP address (e.g. "192.168.1.1") [default if m non-empty]

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [ipinfo,tt]=v_hostipinfo(m,t)
    0002 %V_HOSTIPINFO get host name and internet connection information
    0003 %  Usage:    hostname   = v_hostipinfo;        % computer name
    0004 %            ip_address = v_hostipinfo('i');   % IP address
    0005 %
    0006 %  Inputs:   m  mode input selects the information to output (see below)
    0007 %            t  optional output from an "ipconfig -all" command
    0008 %
    0009 %  Outputs:  ipinfo   cell array with one row per IP connection. The number
    0010 %                     of columns is detemined by the m input (see below).
    0011 %                     If only a single output value is selected by the m input, then
    0012 %                     ipinfo will be a string rather than a cell array.
    0013 %            tt       output from the "ipconfig -all" command
    0014 %
    0015 %  The "m" input selects whch IP connections to supply information about
    0016 %  (rows of IPINFO) and what information to supply (columns of IPINFO).
    0017 %
    0018 %  Row selection: A   include all connections
    0019 %                 L   Local Internet Connection [default if m non-empty]
    0020 %                 B   Bluetooth Connection
    0021 %                 P   PPP connection (e.g. a VPn connection)
    0022 %
    0023 %  Col selection: h   hostname (same for all connections) [default if m is empty]
    0024 %                 c   Connection type code: L=local, B=bluetooth, P=PPP or ?=other
    0025 %                 t   Connection type (e.g. "Ethernet" or "PPP")
    0026 %                 n   Connection name (e.g. "Local Area Connection")
    0027 %                 m   MAC address (e.g. "00-53-45-00-00-00"
    0028 %                 i   IP address (e.g. "192.168.1.1") [default if m non-empty]
    0029
    0030 %      Copyright (C) Mike Brookes 2012
    0031 %      Version: $Id: v_hostipinfo.m 10865 2018-09-21 17:22:45Z dmb $
    0032 %
    0033 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0034 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0035 %
    0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0037 %   This program is free software; you can redistribute it and/or modify
    0038 %   it under the terms of the GNU Lesser General Public License as published by
    0039 %   the Free Software Foundation; either version 3 of the License, or
    0040 %   (at your option) any later version.
    0041 %
    0042 %   This program is distributed in the hope that it will be useful,
    0043 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0044 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0045 %   GNU Lesser General Public License for more details.
    0046 %
    0047 %   You can obtain a copy of the GNU Lesser General Public License from
    0048 %   https://www.gnu.org/licenses/ .
    0049 %
    0050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0051
    0052 if nargin<2
    0053     t=evalc('!ipconfig -all');
    0054     if nargin<1
    0055         m='';
    0056     end
    0057 end
    0058 tt=t;
    0059 if ~numel(m)
    0060     m='Lh';
    0061 end
    0062 host=regexp(t,'Host Name[ .]*:\s*([\w-]*)','tokens','once'); % get host name
    0063 if ~numel(m)
    0064     ipinfo=host{1};
    0065 else
    0066     [iad,ad]=regexp(t,'\n(\w*) adapter ([ \w]*):','start','tokens'); % get adapters
    0067     [iadx,adx]=regexp(t,'\n(\w* adapter [ \w]*):','start','tokens'); % get entire adapter names
    0068     [imac,mac]=regexp(t,'Physical Address[ .]*:\s*(\S*)','start','tokens'); % get MAC addresses
    0069     [iip,ip]=regexp(t,'IP Address[ .]*:\s*(\S*)','start','tokens'); % get IP addresses
    0070     nad=numel(iad);  % number of adapters listed in ipconfig
    0071     iad(nad+1)=length(t); % add an extra fake adapter at the end of the list
    0072     fty='hctnmi'; % possible output fields
    0073     nfty=numel(fty);    % number of fields
    0074     ipinfo=cell(nad,nfty); % cell array to store the fields for each adapter
    0075     nr=nad;
    0076     aty='LBP';   % types of adapter
    0077     naty=numel(aty);
    0078     assx={'Ethernet adapter Local Area Connection'; 'Ethernet adapter Bluetooth Network Connection' ; 'PPP adapter '};
    0079     for i=1:nad
    0080         ipinfo{i,1}=host{1};
    0081         ipinfo(i,3:4)=ad{i};
    0082         ix=find((imac>iad(i)) & (imac<iad(i+1)),1);
    0083         if numel(ix)
    0084             ipinfo{i,5}=mac{ix}{1};
    0085         else
    0086             ipinfo{i,5}='';
    0087         end
    0088         ix=find((iip>iad(i)) & (iip<iad(i+1)),1);
    0089         if numel(ix)
    0090             ipinfo{i,6}=ip{ix}{1};
    0091         else
    0092             ipinfo{i,6}='';
    0093         end
    0094         jx=cell(length(assx),1);
    0095         for j=1:length(assx)
    0096             jx{j}=strfind(adx{i}{1},assx{j});    % compare adapter strings with adapter types
    0097         end
    0098         jx=find(~cellfun('isempty',jx));
    0099         if numel(jx)
    0100             ipinfo{i,2}=aty(jx);
    0101         else
    0102             ipinfo{i,2}='?';
    0103         end
    0104     end
    0105     % Select the requested adapters from the rows of ipinfo{}
    0106     if ~any(m=='A')
    0107         ix=(1:naty)*(aty(ones(numel(m),1),:)'==m(ones(naty,1),:));
    0108         ix=ix(ix>0);
    0109         nr=numel(ix);
    0110         if ~nr
    0111             ix=1;  % default to 'L' = Local Internet
    0112         end
    0113         jx=ix;      % jx will hold first match
    0114         for i=1:numel(ix)
    0115             a=strmatch(aty(ix(i)),ipinfo(:,2));
    0116             if numel(a)
    0117                 jx(i)=a(1);
    0118             else
    0119                 jx(i)=0;
    0120             end
    0121         end
    0122         jx=jx(jx>0);
    0123         ipinfo=ipinfo(jx,:);
    0124     end
    0125     % Select the requested fields from the columns of ipinfo{}
    0126     ix=(1:nfty)*(fty(ones(numel(m),1),:)'==m(ones(nfty,1),:));
    0127     ix=ix(ix>0);
    0128     nc=numel(ix);
    0129     if ~nc
    0130         ix=6;  % default to 'i' = IP address
    0131     end
    0132     ipinfo=ipinfo(:,ix);
    0133     if nr<2 && nc<2
    0134         ipinfo=ipinfo{1};  % just output a string if only one element requested
    0135     end
    0136 end