Showing posts with label FreeBSD. Show all posts
Showing posts with label FreeBSD. Show all posts

Friday, 12 July 2013

Listing iptables rules


When monitoring traffic tcpdump comes is quite useful as it shows the traffic entering or leaving a server interface. However this is no guarantee that the traffic actually reaches the application.

After the network stack receives the packet its passed to iptables which then permits or denys the traffic based on its rule base. There are few tables to look at:

filter: (think ACLs)
It contains the built-in chains
INPUT (for packets from the network destined to local sockets/applications)
FORWARD (for packets being routed through the box)
OUTPUT (for locally-generated packets being sent out)

nat:
This table is consulted when a packet that creates a new connection is encountered. It consists of three built-ins:
PREROUTING (for altering packets as soon as they come in)
OUTPUT (for altering locally-generated packets before routing)
POSTROUTING (for altering packets as they are  about to go out)

mangle:
This  table  is  used  for  specialized packet alteration.  Until linux kernel 2.4.17 it had two built-in chains:
PREROUTING (for altering incoming packets before routing)
OUTPUT (for altering locally- generated  packets before routing)

Since kernel 2.4.18, three other built-in chains are also sup- ported:
INPUT (for packets coming into the box itself)
FORWARD (for altering packets being routed through the box)
POSTROUTING (for altering packets as they are about to go out).

raw:
This  table  is used mainly for configuring exemptions from connection tracking in combination with the NOTRACK target.  It registers a the netfilter hooks with higher priority and  is  thus  called before ip_conntrack, or any other IP tables.  It provides the following built-in chains: PREROUTING (for packets arriving via any network interface) OUTPUT (for packets generated by local processes)


You can easily look at the contents of a table using the command "iptables -t xxx -L -n -v". For example:



root@hostname:~ # iptables -t filter -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
  643 53797 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
 162K   56M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
   82  5009 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           PKTTYPE = broadcast
   20   640 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           PKTTYPE = multicast
   10   520 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:8080
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:123
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:25
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:161
   13  1790 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:162
    0     0 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0           LOG flags 0 level 4 prefix `INPUT DROP: '
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0           LOG flags 0 level 4 prefix `FORWARD DROP: '
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 165K   56M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 LOG        all  --  *      *       0.0.0.0/0            0.0.0.0/0           LOG flags 0 level 4 prefix `OUTPUT DROP: '
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0


root@hostname:~ # iptables -t nat -L -v -n
Chain PREROUTING (policy ACCEPT 354 packets, 23490 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 17376 packets, 1361K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 17376 packets, 1361K bytes)
 pkts bytes target     prot opt in     out     source               destination



Tuesday, 14 August 2012

Displaying a SNMP MIB as Tree

The SNMP protocol works with numeric OIDs for example

.1.3.6.1.2.1.1.3.0

These numbers are like an IP address and point to a specific point in the SNMPdatabase so that you can retrieve the value stored there. Like an IP address, this sequence of numbers does not make any sense to a human. This is where a MIB file fits in: it allows you to convert the numeric string to a text representation.

snmptranslate is a tool included in net-snmp which allows you convert between numeric and text OIDs. In our example above this translates to system uptime.

#snmptranslate .1.3.6.1.2.1.1.3.0
DISMAN-EVENT-MIB::sysUpTimeInstance

or in reverse

#snmptranslate -On DISMAN-EVENT-MIB::sysUpTimeInstance
.1.3.6.1.2.1.1.3.0

where -On is output the OID numerically.

Most of the time however we are interested in taking a vendor provided MIB and working out what OIDs we can poll. For this example I'm using snmptranslate v5.7.1 installed on FreeBSD 9 and I will be interrogating the Opengear MIBs which are available on their FTP server.

Lets pick the OG-STATUS-MIB.mib. Download this MIB into a folder and then run snmptranslate as follows:

snmptranslate -m /home/myuser/opengear/OG-STATUS-MIB.mib -Tp
MIB search path: /home/myuser/.snmp/mibs:/usr/local/share/snmp/mibs
Cannot find module (OPENGEAR-SMI-MIB): At line 12 in /home/myuser/opengear/OG-STATUS-MIB.mib
Did not find 'opengear' in module #-1 (/home/myuser/opengear/OG-STATUS-MIB.mib)
Unlinked OID in OG-STATUS-MIB: ogStatus ::= { opengear 16 }
Undefined identifier: opengear near line 24 of /home/myuser/opengear/OG-STATUS-MIB.mib
Cannot adopt OID in OG-STATUS-MIB: ogStatusGroups ::= { ogStatusConformance 2 }
Cannot adopt OID in OG-STATUS-MIB: ogStatusCompliances ::= { ogStatusConformance 1 }
Cannot adopt OID in OG-STATUS-MIB: ogStatusCompliance ::= { ogStatusCompliances 1 }

Where -m is the path to the downloaded mib, -Tp prints in tree format. The error marked in red above shows that we are missing a module which is defined on line 12 of OG-STATUS-MIB.mib.

#cat OG-STATUS-MIB.mib |more
-- =====================================================================
-- == OG-STATUS-MIB :                                                 ==
-- == Opengear port signal notification Management Information Base   ==
-- ==                                                                 ==
-- == (c) Copyright 2010 Opengear Inc.                                ==
-- =====================================================================

OG-STATUS-MIB DEFINITIONS ::= BEGIN

IMPORTS
        opengear
                FROM OPENGEAR-SMI-MIB
        MODULE-IDENTITY, OBJECT-TYPE
                FROM SNMPv2-SMI
        Integer32
                FROM SNMPv2-SMI
        Counter64
                FROM SNMPv2-SMI
        MODULE-COMPLIANCE, OBJECT-GROUP
                FROM SNMPv2-CONF
        DisplayString
                FROM SNMPv2-TC;

If you look at line 12 (marked in blue above) it says you need the OPENGEAR-SMI-MIB which can be found on the opengear ftp site too: OG-SMI-MIB.mib. Download this file and then run snmptranslate again to view the tree. Note the order of the files as presented to the -m option is important otherwise snmptranslate does not work correctly.

#snmptranslate -m /home/myuser/opengear/OG-SMI-MIB.mib:/home/myuser/opengear/OG-STATUS-MIB.mib -Tp
+--iso(1)
   |
   +--org(3)
      |
      +--dod(6)
         |
         +--internet(1)
            |
            +--directory(1)
            |
            +--mgmt(2)
            |  |
            |  +--mib-2(1)
            |     |
            |     +--transmission(10)
            |
            +--experimental(3)
            |
            +--private(4)
            |  |
            |  +--enterprises(1)
            |     |
            |     +--opengear(25049)
            |        +--ogProducts(1)
            |        +--ogLegacyMgmt(2)
            |        +--ogExperimental(3)
            |        +--ogInternal(4)
            |        +--ogReserved1(5)
            |        +--ogReserved2(6)
            |        +--otherEnterprises(7)
            |        +--ogAgentCapability(8)
            |        +--ogConfig(9)
            |        +--ogMgmt(10)
            |        +--ogModules(11)
            |        +--ogStatus(16)
            |           |
            |           +--ogSerialPortStatusTable(1)
            |           |  |
            |           |  +--ogSerialPortStatusEntry(1)
            |           |     |  Index: ogSerialPortStatusIndex
            |           |     |
            |           |     +-- ---- Integer32 ogSerialPortStatusIndex(1)
            |           |     |        Range: 0..63
            |           |     +-- -R-- Integer32 ogSerialPortStatusPort(2)
            |           |     |        Range: 1..64
            |           |     +-- -R-- Counter64 ogSerialPortStatusRxBytes(3)
            |           |     +-- -R-- Counter64 ogSerialPortStatusTxBytes(4)
            |           |     +-- -R-- Integer32 ogSerialPortStatusSpeed(5)
            |           |     +-- -R-- Integer32 ogSerialPortStatusDCD(6)
            |           |     |        Values: off(0), on(1)
            |           |     +-- -R-- Integer32 ogSerialPortStatusDTR(7)
            |           |     |        Values: off(0), on(1)
            |           |     +-- -R-- Integer32 ogSerialPortStatusDSR(8)
            |           |     |        Values: off(0), on(1)
            |           |     +-- -R-- Integer32 ogSerialPortStatusCTS(9)
            |           |     |        Values: off(0), on(1)
            |           |     +-- -R-- Integer32 ogSerialPortStatusRTS(10)
            |           |              Values: off(0), on(1)
            |           |
            |           +--ogSerialPortActiveUsersTable(2)
            |           |  |
            |           |  +--ogSerialPortActiveUsersEntry(1)
            |           |     |  Index: ogSerialPortActiveUsersIndex
            |           |     |
            |           |     +-- ---- Integer32 ogSerialPortActiveUsersIndex(1)
            |           |     |        Range: 0..63
            |           |     +-- -R-- Integer32 ogSerialPortActiveUsersPort(2)
            |           |     |        Range: 1..64
            |           |     +-- -R-- String    ogSerialPortActiveUsersName(3)
            |           |              Textual Convention: DisplayString
            |           |              Size: 0..64
            |           |
            |           +--ogRpcStatusTable(3)
            |           |  |
            |           |  +--ogRpcStatusEntry(1)
            |           |     |  Index: ogRpcStatusIndex
            |           |     |
            |           |     +-- ---- Integer32 ogRpcStatusIndex(1)
            |           |     |        Range: 0..65535
            |           |     +-- -R-- String    ogRpcStatusName(2)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- Integer32 ogRpcStatusMaxTemp(3)
            |           |     +-- -R-- Integer32 ogRpcStatusAlertCount(4)
            |           |
            |           +--ogEmdStatusTable(4)
            |           |  |
            |           |  +--ogEmdStatusEntry(1)
            |           |     |  Index: ogEmdStatusIndex
            |           |     |
            |           |     +-- ---- Integer32 ogEmdStatusIndex(1)
            |           |     |        Range: 0..65535
            |           |     +-- -R-- String    ogEmdStatusName(2)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- Integer32 ogEmdStatusTemp(3)
            |           |     +-- -R-- Integer32 ogEmdStatusHumidity(4)
            |           |     +-- -R-- Integer32 ogEmdStatusAlertCount(5)
            |           |
            |           +--ogSignalAlertStatusTable(5)
            |           |  |
            |           |  +--ogSignalAlertStatusEntry(1)
            |           |     |  Index: ogSignalAlertStatusIndex
            |           |     |
            |           |     +-- ---- Integer32 ogSignalAlertStatusIndex(1)
            |           |     |        Range: 0..65535
            |           |     +-- -R-- Integer32 ogSignalAlertStatusPort(2)
            |           |     +-- -R-- String    ogSignalAlertStatusLabel(3)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- String    ogSignalAlertStatusSignalName(4)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- Integer32 ogSignalAlertStatusState(5)
            |           |              Values: off(0), on(1)
            |           |
            |           +--ogEnvAlertStatusTable(6)
            |           |  |
            |           |  +--ogEnvAlertStatusEntry(1)
            |           |     |  Index: ogEnvAlertStatusIndex
            |           |     |
            |           |     +-- ---- Integer32 ogEnvAlertStatusIndex(1)
            |           |     |        Range: 0..65535
            |           |     +-- -R-- String    ogEnvAlertStatusDevice(2)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- String    ogEnvAlertStatusSensor(3)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- Integer32 ogEnvAlertStatusOutlet(4)
            |           |     +-- -R-- Integer32 ogEnvAlertStatusValue(5)
            |           |     +-- -R-- Integer32 ogEnvAlertStatusOldValue(6)
            |           |     +-- -R-- Integer32 ogEnvAlertStatusStatus(7)
            |           |
            |           +--ogNutAlertStatusTable(7)
            |           |  |
            |           |  +--ogNutAlertStatusEntry(1)
            |           |     |  Index: ogNutAlertStatusIndex
            |           |     |
            |           |     +-- ---- Integer32 ogNutAlertStatusIndex(1)
            |           |     |        Range: 0..65535
            |           |     +-- -R-- Integer32 ogNutAlertStatusPort(2)
            |           |     +-- -R-- String    ogNutAlertStatusName(3)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- String    ogNutAlertStatusHost(4)
            |           |     |        Textual Convention: DisplayString
            |           |     |        Size: 0..255
            |           |     +-- -R-- String    ogNutAlertStatusStatus(5)
            |           |              Textual Convention: DisplayString
            |           |              Size: 0..255
            |           |
            |           +--ogStatusConformance(65535)
            |              |
            |              +--ogStatusCompliances(1)
            |              |  |
            |              |  +--ogStatusCompliance(1)
            |              |
            |              +--ogStatusGroups(2)
            |                 |
            |                 +--ogBasicStatusGroup(1)
            |                 +--ogBasicAlertStatusGroup(2)
            |
            +--security(5)
            |
            +--snmpV2(6)
               |
               +--snmpDomains(1)
               |
               +--snmpProxys(2)
               |
               +--snmpModules(3)

An alternative option is to view the OIDs and text strings as follows:

#snmptranslate -m /home/myuser/opengear/OG-SMI-MIB.mib:/home/myuser/opengear/OG-STATUS-MIB.mib -Tos
.1.3
.iso.org
.1.3.6
.iso.org.dod
.1.3.6.1
.iso.org.dod.internet
.1.3.6.1.1
.iso.org.dod.internet.directory
.1.3.6.1.2
.iso.org.dod.internet.mgmt
.1.3.6.1.2.1
.iso.org.dod.internet.mgmt.mib-2
.1.3.6.1.2.1.10
.iso.org.dod.internet.mgmt.mib-2.transmission
.1.3.6.1.3
.iso.org.dod.internet.experimental
.1.3.6.1.4
.iso.org.dod.internet.private
.1.3.6.1.4.1
.iso.org.dod.internet.private.enterprises
.1.3.6.1.4.1.25049
.iso.org.dod.internet.private.enterprises.opengear
.1.3.6.1.4.1.25049.1
.iso.org.dod.internet.private.enterprises.opengear.ogProducts
.1.3.6.1.4.1.25049.2
.iso.org.dod.internet.private.enterprises.opengear.ogLegacyMgmt
.1.3.6.1.4.1.25049.3
.iso.org.dod.internet.private.enterprises.opengear.ogExperimental
.1.3.6.1.4.1.25049.4
.iso.org.dod.internet.private.enterprises.opengear.ogInternal
.1.3.6.1.4.1.25049.5
.iso.org.dod.internet.private.enterprises.opengear.ogReserved1
.1.3.6.1.4.1.25049.6
.iso.org.dod.internet.private.enterprises.opengear.ogReserved2
.1.3.6.1.4.1.25049.7
.iso.org.dod.internet.private.enterprises.opengear.otherEnterprises
.1.3.6.1.4.1.25049.8
.iso.org.dod.internet.private.enterprises.opengear.ogAgentCapability
.1.3.6.1.4.1.25049.9
.iso.org.dod.internet.private.enterprises.opengear.ogConfig
.1.3.6.1.4.1.25049.10
.iso.org.dod.internet.private.enterprises.opengear.ogMgmt
.1.3.6.1.4.1.25049.11
.iso.org.dod.internet.private.enterprises.opengear.ogModules
.1.3.6.1.4.1.25049.16
.iso.org.dod.internet.private.enterprises.opengear.ogStatus
.1.3.6.1.4.1.25049.16.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable
.1.3.6.1.4.1.25049.16.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry
.1.3.6.1.4.1.25049.16.1.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusIndex
.1.3.6.1.4.1.25049.16.1.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusPort
.1.3.6.1.4.1.25049.16.1.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusRxBytes
.1.3.6.1.4.1.25049.16.1.1.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusTxBytes
.1.3.6.1.4.1.25049.16.1.1.5
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusSpeed
.1.3.6.1.4.1.25049.16.1.1.6
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusDCD
.1.3.6.1.4.1.25049.16.1.1.7
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusDTR
.1.3.6.1.4.1.25049.16.1.1.8
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusDSR
.1.3.6.1.4.1.25049.16.1.1.9
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusCTS
.1.3.6.1.4.1.25049.16.1.1.10
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortStatusTable.ogSerialPortStatusEntry.ogSerialPortStatusRTS
.1.3.6.1.4.1.25049.16.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortActiveUsersTable
.1.3.6.1.4.1.25049.16.2.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortActiveUsersTable.ogSerialPortActiveUsersEntry
.1.3.6.1.4.1.25049.16.2.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortActiveUsersTable.ogSerialPortActiveUsersEntry.ogSerialPortActiveUsersIndex
.1.3.6.1.4.1.25049.16.2.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortActiveUsersTable.ogSerialPortActiveUsersEntry.ogSerialPortActiveUsersPort
.1.3.6.1.4.1.25049.16.2.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSerialPortActiveUsersTable.ogSerialPortActiveUsersEntry.ogSerialPortActiveUsersName
.1.3.6.1.4.1.25049.16.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogRpcStatusTable
.1.3.6.1.4.1.25049.16.3.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogRpcStatusTable.ogRpcStatusEntry
.1.3.6.1.4.1.25049.16.3.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogRpcStatusTable.ogRpcStatusEntry.ogRpcStatusIndex
.1.3.6.1.4.1.25049.16.3.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogRpcStatusTable.ogRpcStatusEntry.ogRpcStatusName
.1.3.6.1.4.1.25049.16.3.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogRpcStatusTable.ogRpcStatusEntry.ogRpcStatusMaxTemp
.1.3.6.1.4.1.25049.16.3.1.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogRpcStatusTable.ogRpcStatusEntry.ogRpcStatusAlertCount
.1.3.6.1.4.1.25049.16.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable
.1.3.6.1.4.1.25049.16.4.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable.ogEmdStatusEntry
.1.3.6.1.4.1.25049.16.4.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable.ogEmdStatusEntry.ogEmdStatusIndex
.1.3.6.1.4.1.25049.16.4.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable.ogEmdStatusEntry.ogEmdStatusName
.1.3.6.1.4.1.25049.16.4.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable.ogEmdStatusEntry.ogEmdStatusTemp
.1.3.6.1.4.1.25049.16.4.1.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable.ogEmdStatusEntry.ogEmdStatusHumidity
.1.3.6.1.4.1.25049.16.4.1.5
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEmdStatusTable.ogEmdStatusEntry.ogEmdStatusAlertCount
.1.3.6.1.4.1.25049.16.5
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable
.1.3.6.1.4.1.25049.16.5.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable.ogSignalAlertStatusEntry
.1.3.6.1.4.1.25049.16.5.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable.ogSignalAlertStatusEntry.ogSignalAlertStatusIndex
.1.3.6.1.4.1.25049.16.5.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable.ogSignalAlertStatusEntry.ogSignalAlertStatusPort
.1.3.6.1.4.1.25049.16.5.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable.ogSignalAlertStatusEntry.ogSignalAlertStatusLabel
.1.3.6.1.4.1.25049.16.5.1.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable.ogSignalAlertStatusEntry.ogSignalAlertStatusSignalName
.1.3.6.1.4.1.25049.16.5.1.5
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogSignalAlertStatusTable.ogSignalAlertStatusEntry.ogSignalAlertStatusState
.1.3.6.1.4.1.25049.16.6
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable
.1.3.6.1.4.1.25049.16.6.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry
.1.3.6.1.4.1.25049.16.6.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusIndex
.1.3.6.1.4.1.25049.16.6.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusDevice
.1.3.6.1.4.1.25049.16.6.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusSensor
.1.3.6.1.4.1.25049.16.6.1.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusOutlet
.1.3.6.1.4.1.25049.16.6.1.5
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusValue
.1.3.6.1.4.1.25049.16.6.1.6
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusOldValue
.1.3.6.1.4.1.25049.16.6.1.7
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogEnvAlertStatusTable.ogEnvAlertStatusEntry.ogEnvAlertStatusStatus
.1.3.6.1.4.1.25049.16.7
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable
.1.3.6.1.4.1.25049.16.7.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable.ogNutAlertStatusEntry
.1.3.6.1.4.1.25049.16.7.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable.ogNutAlertStatusEntry.ogNutAlertStatusIndex
.1.3.6.1.4.1.25049.16.7.1.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable.ogNutAlertStatusEntry.ogNutAlertStatusPort
.1.3.6.1.4.1.25049.16.7.1.3
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable.ogNutAlertStatusEntry.ogNutAlertStatusName
.1.3.6.1.4.1.25049.16.7.1.4
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable.ogNutAlertStatusEntry.ogNutAlertStatusHost
.1.3.6.1.4.1.25049.16.7.1.5
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogNutAlertStatusTable.ogNutAlertStatusEntry.ogNutAlertStatusStatus
.1.3.6.1.4.1.25049.16.65535
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogStatusConformance
.1.3.6.1.4.1.25049.16.65535.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogStatusConformance.ogStatusCompliances
.1.3.6.1.4.1.25049.16.65535.1.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogStatusConformance.ogStatusCompliances.ogStatusCompliance
.1.3.6.1.4.1.25049.16.65535.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogStatusConformance.ogStatusGroups
.1.3.6.1.4.1.25049.16.65535.2.1
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogStatusConformance.ogStatusGroups.ogBasicStatusGroup
.1.3.6.1.4.1.25049.16.65535.2.2
.iso.org.dod.internet.private.enterprises.opengear.ogStatus.ogStatusConformance.ogStatusGroups.ogBasicAlertStatusGroup
.1.3.6.1.5
.iso.org.dod.internet.security
.1.3.6.1.6
.iso.org.dod.internet.snmpV2
.1.3.6.1.6.1
.iso.org.dod.internet.snmpV2.snmpDomains
.1.3.6.1.6.2
.iso.org.dod.internet.snmpV2.snmpProxys
.1.3.6.1.6.3
.iso.org.dod.internet.snmpV2.snmpModules
.0.0
.ccitt.zeroDotZero

Monday, 6 August 2012

Connecting to a MS-SQL Database using iODBC - Part 2

Part one of this blog showed how to configure the first two steps of the software chain below. This post continues with configuring the protocols used to connect to the SQL database.


SQL Express (2012) (The database server - Win7 Ultimate)
|
MS ODBC Server
|
MS SQL Connection protocols
|
Windows Firewall
|
Network + Firewalls (The network cloud)
|
FreeTDS (MS SQL Driver)
|
iODBC
|
Perl (The client machine - FreeBSD)



MS SQL Connection protocols

Click on Start -> Microsoft SQL Server 2012 ->Configuration tools -> SQL Server Configuration Manger. Expand the SQL Server Network Configuration and select Protocols for SQLEXPRESS (ie the name of the database you set when installing SQL Express 2012)



Double click on TCP/IP and ensure that Enabled is set to Yes in the Protocol tab.



In the IP Addresses Tab ensure that each IP section has TCP Dynamic ports set to nothing (ie not even zero) and TCP Port set to 1433.






Click ok to save this configuration.

Windows Firewall

 Click Start -> Control Panel -> Windows Firewall -> Advanced Settings (on the left hand side)





 Click Inbound Rules on the left and Add New Rule on the right. Select Port



Select TCP and set the Specific local ports to 1433.



Set the Action to "Allow the connection" and Apply the rule to Domain, Private and Public.
Give this rule a name like "SQL Port" and save it.


REBOOT the MS SQL machine for ALL the accumulated changes to take effect!


Network + Firewalls (The network cloud)

Talk to your network administrator and ask them to check if TCP 1433 is allowed from the client to the MQ SQL server. Check this using:

telnet 10.202.70.150 1433
Trying 10.202.70.150...
Connected to 10.202.70.150.
Escape character is '^]'.


If you get the Connected message then most likely everything is working fine.


 FreeTDS (MS SQL Driver)

Install FreeTDS

cd /usr/ports/databases/freetds
make install clean


pick iODBC and MSQL options when prompted.

Configure FreeTDS

cp /usr/local/etc/freetds.conf.dist /usr/local/etc/freetds.conf
vi /usr/local/etc/freetds.conf

Add a new database connection to the SQL Express Server by adding the following to freetds.conf

[MyServer]
host = 10.202.70.150
port = 1433
tds version = 8.0



Test the connection

Use the username and password created in post 1.

tsql -S MyServer -U username -P xxxxx
locale is "C"
locale charset is "US-ASCII"

! Pick the database
1> use "MyRealDatabase"
!execute
2> go
!What query to run
1> select * from ActivityLogs
!execute the query
2> go
guid  datetime  eventtype       eventid state   alarmguid       unitguid      

unitname        door    unitgroupguid   unitgroupname       userid userguid

username        workstation     modifieddate
C64F1202-473B-48D5-A404-00D9E4697349    Aug  2 2012 04:41PM     176     88    

512     00000000-0000-0000-0000-00000000000085B80E6C-A7B9-48D8-9156-09DCFDDE8F40

   Testrack Front  1       00








iODBC

Install perl ODBC

cd /usr/ports/databases/p5-DBD-ODBC
make install clean
*pick iODBC*


Add FreeTDS as a driver

cp /usr/local/etc/libiodbc/odbcinst.ini.sample /usr/local/etc/libiodbc/odbcinst.ini
vi /usr/local/etc/libiodbc/odbcinst.ini

add the following section of odbcinst.ini

[FreeTDS]
Description     = TDS driver (Sybase/MS SQL)
Driver          = /usr/local/lib/libtdsodbc.so

Perl (The client machine - FreeBSD)

Use the the following code to connect to the server

#! /usr/bin/perl

use strict;
use warnings;
use DBI;




my $user = 'Username';
my $pw = 'xxxx';

my $dbh = DBI->connect("dbi:ODBC:driver=FreeTDS;tds_version=8.0;Server=10.202.70.150;Port=1433", $user, $pw, {AutoCommit => 0, RaiseError => 1, PrintError => 0});

my $sql = "use \"MyRealServer\"";
print "$sql\n";
my $sth = $dbh->prepare($sql);
my $result = $sth->execute;

$sql = "select * from ActivityLogs";
print "$sql\n";
$sth = $dbh->prepare($sql);
$result = $sth->execute;

my @rows ;
while(@rows = $sth->fetchrow())
{
        print "$rows[0] rows returned by query\n";
}

$sth->finish;
$dbh->commit;
$dbh->disconnect;









There you have it. The longest part of this is getting the Microsoft GUI setup correctly and then patching the FreeTSD driver into iODBC.