<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indented! &#187; SysLog</title>
	<atom:link href="http://www.indented.co.uk/index.php/tag/syslog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indented.co.uk</link>
	<description></description>
	<lastBuildDate>Mon, 17 Oct 2011 19:03:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>NetShell</title>
		<link>http://www.indented.co.uk/index.php/2010/11/25/netshell/</link>
		<comments>http://www.indented.co.uk/index.php/2010/11/25/netshell/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 19:56:50 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[DHCP]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[smtp]]></category>
		<category><![CDATA[subnet]]></category>
		<category><![CDATA[Subnet Math]]></category>
		<category><![CDATA[SysLog]]></category>

		<guid isPermaLink="false">http://www.indented.co.uk/?p=1561</guid>
		<description><![CDATA[It&#8217;s been a while since I&#8217;ve posted, and since it&#8217;s almost Christmas I thought I&#8217;d better get on with it. Without further ado I want to post NetShell (I&#8217;m not very good at coming up with imaginative names). NetShell is a collection of 17 functions and a few supporting functions in a script module. Download [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I&#8217;ve posted, and since it&#8217;s almost Christmas I thought I&#8217;d better get on with it.</p>
<p>Without further ado I want to post NetShell (I&#8217;m not very good at coming up with imaginative names). NetShell is a collection of 17 functions and a few supporting functions in a script module. </p>
<p>Download <a href='http://www.indented.co.uk/wp-content/uploads/2011/05/NetShell.zip'>NetShell</a><br />
<span id="more-1561"></span><br />
Installation is a manual process, but not too hard. Open up Documents\WindowsPowerShell\Modules, extract the ZIP file. Make sure it includes the NetShell folder or it won&#8217;t work. The module is not currently signed, something else on the to-do list. Once it&#8217;s there, Import-Module NetShell and off you go.</p>
<p>It includes the following:</p>
<table>
<tr>
<td><b>ConvertTo-BinaryIP</b></td>
<td>Converts an IP address into a binary string</td>
</tr>
<tr>
<td><b>ConvertTo-Byte</b></td>
<td>A supporting function, a simple conversion of a string to a byte array (ASCII encoding)</td>
</tr>
<tr>
<td><b>ConvertTo-DecimalIP</b></td>
<td>Converts an IP address to 32-bit decimal number</td>
</tr>
<tr>
<td><b>ConvertTo-DottedDecimalIP</b></td>
<td>Converts a binary or 32-bit decimal back to an IP</td>
</tr>
<tr>
<td><b>ConvertTo-Mask</b></td>
<td>Converts from a mask length. For example, gets you from 22 to 255.255.252.0</td>
</tr>
<tr>
<td><b>ConvertTo-MaskLength</b></td>
<td>Converts from the IP form of a mask to the length</td>
</tr>
<tr>
<td><b>ConvertTo-String</b></td>
<td>Another supporting function, converts a byte array to a string (ASCII encoding)</td>
</tr>
<tr>
<td><b>Get-BroadcastAddress</b></td>
<td>Returns the broadcast address for the specified IP address and subnet mask</td>
</tr>
<tr>
<td><b>Get-NetworkAddress</b></td>
<td>Returns the network address for the specified IP address and subnet mask</td>
</tr>
<tr>
<td><b>Get-NetworkRange</b></td>
<td>Returns every IP within the specified range</td>
</tr>
<tr>
<td><b>Get-NetworkSummary</b></td>
<td>Everything about an IP address and mask I considered useful</td>
</tr>
<tr>
<td><b>New-DhcpDiscoverPacket</b></td>
<td>A supporting function for Send-DhcpDiscover. Creates the packet to send (a large byte array)</td>
</tr>
<tr>
<td><b>New-Socket</b></td>
<td>Creates an instance of System.Net.Sockets.Socket, an arbitrary network socket to do with as you please.</td>
</tr>
<tr>
<td><b>New-SysLogDateTime</b></td>
<td>A supporting function to create a DateTime string in the format SysLog likes.</td>
</tr>
<tr>
<td><b>Read-DhcpOption</b></td>
<td>A supporting function to read off an Option from a DHCP packet. Needs to be fed the Extended.BinaryReader class at the top of the module.</td>
</tr>
<tr>
<td><b>Read-DhcpPacket</b></td>
<td>Creates and uses an instance of Extended.BinaryReader to process a DHCP packet and translate the fields.</td>
</tr>
<tr>
<td><b>Receive-Bytes</b></td>
<td>Receives a stream of bytes from the network using a socket</td>
</tr>
<tr>
<td><b>Remove-Socket</b></td>
<td>Cleans up after New-Socket</td>
</tr>
<tr>
<td><b>Send-Bytes</b></td>
<td>Sends an arbitrary byte array over the network using a socket</td>
</tr>
<tr>
<td><b>Send-DhcpDiscover</b></td>
<td>Creates and sends a DHCPDISCOVER packet, then processes and returns the response</td>
</tr>
<tr>
<td><b>Start-Syslog</b></td>
<td>Starts a SysLog server. No termination for this one at the moment. Needs a bit more work.</td>
</tr>
<tr>
<td><b>Test-Smtp</b></td>
<td>Does the SMTP test you normally wind up doing with telnet, returning all the results along with the SMTP banner.</td>
</tr>
<tr>
<td><b>Test-SysLogDateTime</b></td>
<td>A supporting function to check the format of a DateTime that may or may not be present in a SysLog message.</td>
</tr>
<tr>
<td><b>Test-SysLogPRI</b></td>
<td>A supporting function to test of the PRI value in a SysLog message.</td>
</tr>
<tr>
<td><b>Test-TcpPort</b></td>
<td>Returns a boolean indicating whether or not the port connection succeeded.</td>
</tr>
</table>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2010/11/25/netshell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SysLog in PowerShell</title>
		<link>http://www.indented.co.uk/index.php/2009/12/01/syslog-in-powershell/</link>
		<comments>http://www.indented.co.uk/index.php/2009/12/01/syslog-in-powershell/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 16:01:07 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SysLog]]></category>

		<guid isPermaLink="false">http://www.highorbit.co.uk/?p=1215</guid>
		<description><![CDATA[I bumped into a requirement to run a SysLog relay on one of my Windows 2008 R2 systems. After poking around on Google, and after getting a bit bored with the third-party offerings, I threw together a simple server of my own. There is plenty of room for improvement here, but it works (for me [...]
No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I bumped into a requirement to run a SysLog relay on one of my Windows 2008 R2 systems. After poking around on Google, and after getting a bit bored with the third-party offerings, I threw together a simple server of my own.<br />
<span id="more-1215"></span><br />
There is plenty of room for improvement here, but it works (for me at least) as it stands.</p>
<p>Written for and tested under PowerShell 2.0. The script could probably use some error handling.</p>
<pre class="brush: powershell; title: ; notranslate">
# SysLog.ps1
#
# A basic SysLog server. Behaviour should be fairly consistent with
# RFC 3164 (http://www.ietf.org/rfc/rfc3164.txt).

# Network Configuration

$SysLogPort = 514                  # Default SysLog Port
$Buffer = New-Object Byte[] 1024   # Maximum SysLog message size

# Server Configuration

$EnableMessageValidation = $True   # Enable check of the PRI and Header
$EnableRelay = $True               # Enable relay to $RelayTargetIP
$EnableLocalLogging = $True        # Enable local logging of received messages
$EnableConsoleLogging = $False     # Enable logging to the console
$EnableHostNameLookup = $True      # Lookup hostname for connecting IP
$EnableHostNamesOnly = $True       # Uses Host Name only instead of FQDNs

$RelayTargetIP = &quot;10.0.0.1&quot;        # Must be an IP Address
$LogFolder = &quot;C:\SysLog\LogFiles&quot;  # Path must exist

# Global variables used to store day and date-stamp for log roll-over

$Day = (Get-Date).Day
$DateStamp = (Get-Date).ToString(&quot;yyyy.MM.dd&quot;)

# Relay Initialisation

If ($EnableRelay)
{
  $RelayTarget = [Net.IPAddress]::Parse($RelayTargetIP)
  $RelayTargetEndPoint = New-Object Net.IPEndPoint($RelayTarget, $SysLogPort)
}

# A launcher for the process
#
# Caller: Manual / Script

Function Start-SysLog
{
  $Socket = CreateSocket
  StartReceive $Socket
}

# Create and bind to the socket
#
# Caller: Start-SysLog

Function CreateSocket
{
  $Socket = New-Object Net.Sockets.Socket(
    [Net.Sockets.AddressFamily]::Internetwork,
    [Net.Sockets.SocketType]::Dgram,
    [Net.Sockets.ProtocolType]::Udp)

  $ServerIPEndPoint = New-Object Net.IPEndPoint(
    [Net.IPAddress]::Any,
    $SysLogPort)

  $Socket.Bind($ServerIPEndPoint)

  Return $Socket
}

# Recieve a single message
#
# Caller: Start-SysLog

Function StartReceive([Net.Sockets.Socket]$Socket)
{
  # Placeholder to store source of incoming packet
  $SenderIPEndPoint = New-Object Net.IPEndPoint([Net.IPAddress]::Any, 0)
  $SenderEndPoint = [Net.EndPoint]$SenderIPEndPoint

  $ServerRunning = $True
  While ($ServerRunning -eq $True)
  {
    $BytesReceived = $Socket.ReceiveFrom($Buffer, [Ref]$SenderEndPoint)
    $Message = $Buffer[0..$($BytesReceived - 1)]

    $Message = ValidateMessage $Message $SenderEndPoint.Address.IPAddressToString

    If ($EnableRelay)
    {
      RelayMessage $Socket $Message
    }
  }
}

# Relay the message to an upstream SysLog server. Either basic forwarding,
# or full validation.
#
# Caller: StartReceive

Function RelayMessage([Net.Sockets.Socket]$Socket, [Byte[]]$Message)
{
  [Void]$Socket.SendTo($Message, $RelayTargetEndPoint)
}

# Check the validity of the message (if option is enabled). Adjust message
# according to recommendations in RFC 3164.
#
# Caller: StartReceive

Function ValidateMessage([Byte[]]$Message, [String]$HostName)
{
  If ($EnableMessageValidation)
  {
    $MessageString = [Text.Encoding]::ASCII.GetString($Message)

    If (IsValidPRI($MessageString))
    {
      If (!(IsValidDateTime($MessageString)))
      {
        $PRI = [Int]($MessageString -Replace &quot;&lt;|&gt;.*&quot;)
        $HostName = GetHostName $HostName
        $MessageString = &quot;&lt;$PRI&gt;$(NewDateTimeString) $HostName $MessageString&quot;
        $Message = EncodeMessage $MessageString
      }
    }
    Else
    {
      $HostName = GetHostName $HostName
      $MessageString = &quot;&lt;13&gt;$(NewDateTimeString) $HostName $MessageString&quot;
      $Message = EncodeMessage $MessageString
    }
  }
  If ($EnableLocalLogging -Or $EnableConsoleLogging)
  {
    If ($MessageString -eq $Null)
    {
      $MessageString = [Text.Encoding]::ASCII.GetString($Message)
    }
    If ($EnableLocalLogging) { WriteToLog $MessageString $HostName }
    If ($EnableConsoleLogging) { Write-Host $MessageString }
  }
  Return $Message
}

# Validate the PRI (Priority Field - Facility and Severity)
# No parsing is performed. No network prioritisation is implemented
#
# Caller: ValidateMessage

Function IsValidPRI([String]$MessageString)
{
  If ($MessageString.SubString(0, 1) -ne &quot;&lt;&quot;)
  {
    Return $False
  }
  If (!$MessageString.SubString(2, 3).Contains(&quot;&gt;&quot;))
  {
    Return $False
  }

  $PRI = [Int]($MessageString -Replace &quot;&lt;|&gt;.*&quot;)
  # PRI = (Facility * 8) + Severity. Maximum and minimum values from RFC 3164
  If ($PRI -lt 1 -Or $PRI -gt 191)
  {
    Return $False
  }
  Return $True
}

# Validate the TimeStamp formatting
#
# Caller: ValidateMessage

Function IsValidDateTime([String]$MessageString)
{
  $IsValid = $False
  If ($MessageString -Match &quot;(?&lt;=\&gt;)\w{3}\s\s?\d{1,2}\s(\d\d:){2}\d\d(?=\s)&quot;)
  {
    $Date = New-Object DateTime
    ForEach ($Format in @(&quot;MMM  d hh:mm:ss&quot;, &quot;MMM dd hh:mm:ss&quot;))
    {
      $Date = New-Object DateTime
      $IsValid = [DateTime]::TryParseExact(
        $Matches[0],
        $Format,
        [Globalization.CultureInfo]::InvariantCulture,
        [Globalization.DateTimeStyles]::AssumeUniversal,
        [Ref]$Date)
      If ($IsValid) { Return $True }
    }
  }
  Return $False
}

# Create a new DateTime String
#
# Caller: ValidateMessage

Function NewDateTimeString
{
  $Date = (Get-Date).ToUniversalTime()
  If ($Date.Day -lt 10)
  {
    Return $Date.ToString(&quot;MMM  d HH:mm:ss&quot;)
  }
  Return $Date.ToString(&quot;MMM dd HH:mm:ss&quot;)
}

# Attempt to lookup the HostName if an IP value was passed.
# [Net.Dns]::GetHostEntry fails to return if a Forward Lookup record
# does not exist. NsLookup as a simple alternative.
#
# Caller: ValidateMessage

Function GetHostName([String]$HostName)
{
  If (!$EnableHostNameLookup) { Return $HostName }
  If ([Net.IPAddress]::TryParse($HostName, [Ref]$Null))
  {
    $Temp = (nslookup -q=ptr $HostName | ?{ $_ -Like &quot;*name = *&quot; })
    $Temp = $Temp -Replace &quot;.*name = &quot;
    If ($Temp -ne [String]::Empty) { $HostName = $Temp }
  }
  If ($EnableHostNamesOnly)
  {
    Return $HostName.Split(&quot;.&quot;)[0]
  }
  Return $HostName
}

# Returns a Byte Array representation of the original message.
# If the length is greater than 1024 Bytes the array is truncated
# as stipulated under RFC 3164.
#
# Caller: ValidateMessage

Function EncodeMessage([String]$MessageString)
{
  $Message = [Text.Encoding]::ASCII.GetBytes($MessageString)
  If ($Message.Length -gt 1024)
  {
    Return $Message[0..1023]
  }
  Return $Message
}

# Maintain a per-host log file in the $LogFolder
# Script does not clean up old log files
#
# Caller: ValidateMessage

Function WriteToLog([String]$MessageString, [String]$HostName)
{
  # Simple time based roll-over check
  If ((Get-Date).Day -ne $Day)
  {
    $Day = (Get-Date).Day
    $DateStamp = (Get-Date).ToString(&quot;yyyy.MM.dd&quot;)
  }

  $LogFile = &quot;$LogFolder\$HostName-$DateStamp.log&quot;
  $MessageString &gt;&gt; $LogFile
}

# Start the server

Start-SysLog
</pre>
<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.indented.co.uk/index.php/2009/12/01/syslog-in-powershell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

