<?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>Marcus Nyberg &#187; bash</title>
	<atom:link href="http://www.marcusnyberg.com/category/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marcusnyberg.com</link>
	<description>Digital and dangerous</description>
	<lastBuildDate>Mon, 26 Jul 2010 11:00:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Do something every X iteration in a bash loop</title>
		<link>http://www.marcusnyberg.com/2010/07/23/do-something-every-x-iteration-in-a-bash-loop/</link>
		<comments>http://www.marcusnyberg.com/2010/07/23/do-something-every-x-iteration-in-a-bash-loop/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 08:30:42 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://www.marcusnyberg.com/?p=663</guid>
		<description><![CDATA[If you have a bash loop and want to &#8221;do something&#8221; at every X iteration you are in trouble because bash isn&#8217;t that good with numbers. Anyway, today I created this small example that you can use. In my case I needed to read a pretty large txt-file and create a simple html output table [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a bash loop and want to &#8221;do something&#8221; at every X iteration you are in trouble because bash isn&#8217;t that good with numbers. Anyway, today I created this small example that you can use. In my case I needed to read a pretty large txt-file and create a simple html output table of the rows. The problem was to insert TR-tags between the TD-tags like below:</p>
<p>td row 1<br />
td row 2<br />
td row 3<br />
tr<br />
td row 4<br />
td row 5<br />
td row 6<br />
tr</p>
<p>My solution solved the problem!  It is pretty helpful that we have the modulo (%) operator in bash.</p>
<pre name="code" class="xml">
#!/bin/bash

# Change this to how many rows row you want to skip before doing something
doevery=3

for (( c=1; c<=100; c++ ))
do
	remainder=`expr $c % $doevery`
	echo "Remainder: $remainder"

	if [ $remainder = "0" ];	then
		echo "Every $doevery iteration"
		# Do something
	fi

done
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.marcusnyberg.com/2010/07/23/do-something-every-x-iteration-in-a-bash-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nagios check_nt over ssh-tunnel</title>
		<link>http://www.marcusnyberg.com/2010/01/04/nagios-check_nt-over-ssh-tunnel/</link>
		<comments>http://www.marcusnyberg.com/2010/01/04/nagios-check_nt-over-ssh-tunnel/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:21:02 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[nagios]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.marcusnyberg.com/?p=374</guid>
		<description><![CDATA[I am a big fan of Nagios for host and service monitoring and this is my first post on the subject. My task was to check services on a Windows Server with Nagios and NSClient++. But the firewall only allowed me to use ssh and thats why I could not connect to port 12489 (that [...]]]></description>
			<content:encoded><![CDATA[<p>I am a big fan of <a href="http://www.nagios.org/">Nagios</a> for host and service monitoring and this is my first post on the subject. My task was to check services on a Windows Server with Nagios and <a href="http://nsclient.org/nscp/">NSClient++</a>. But the firewall only allowed me to use ssh and thats why I could not connect to port 12489 (that NSClient listens to) from my Nagios server. The only way to solve the problem was to use a <a href="http://www.marcusnyberg.com/2010/01/04/bash-script-that-open-and-close-an-ssh-tunnel-automagically/">SSH-tunnel that I can open and close</a> whenever I needed to.</p>
<p><strong>Workflow for my solution</strong><br />
1. Nagios initiates that a service should be checked.<br />
2. Nagios executes the check_nt_by_ssh_tunnel check-command with additional parameters<br />
3. The script creates a ssh-tunnel<br />
4. The script checks the Windows server with the Nagios builtin command check_nt<br />
5. The script returns the check_nt output<br />
6. The script closes the ssh-tunnel<br />
7. Nagios does its processing.</p>
<p><strong>Prerequisites</strong><br />
* A fully working Nagios installation<br />
* NSClient installed and working on the Windows Server that should be monitored<br />
* <a href="http://bloggerdigest.blogspot.com/2006/11/ssh-auto-login-or-passwordless-login.html">SSH autologin</a> is configured between the two machines<br />
* Read <a href="http://www.marcusnyberg.com/2010/01/04/bash-script-that-open-and-close-an-ssh-tunnel-automagically/">my other article</a> for details about the SSH-tunnel used in this solution.</p>
<p>First you have to create the check-script on the Nagios server (in the Nagios libexec-folder). See below:</p>
<p><strong>check_nt_by_ssh_tunnel.sh</strong></p>
<pre name="code" class="html">
#!/bin/bash

# $1 = HOSTNAME, 83.121.233.2
# $2 = LOCAL PORT, 14880
# $3 = USERNAME, ex: someusername
# $4 = CHECK PARAMETERS, ex: -w 80 -c 90 -v MEMUSE

if [ -z "$1" ]
then
        echo "Missing HOSTNAME"
        exit
fi

if [ -z "$2" ]
then
        echo "Missing LOCAL PORT"
        exit
fi

if [ -z "$3" ]
then
        echo "Missing USERNAME"
        exit
fi

# Open ssh-tunnel and wait for it to open
ssh -f -N -L $2:localhost:12489 $3@$1 &#038;
sleep 30

# Run check_nt command
CHECK="/usr/local/nagios/libexec/check_nt -H localhost -p $2 $4"
#echo $CHECK
eval $CHECK

# Close ssh-tunnel
sleep 5
CMD="ps -eo pid,args | grep 'ssh -f -N -L $2:localhost' | grep -v  'grep' | cut -c1-6"
#echo $CMD
PID=`eval $CMD`
#echo $PID
kill -9 $PID
</pre>
<p>Make it executable:<br />
chmod 755 check_nt_by_ssh_tunnel.sh</p>
<p>Then verify that i works by running the command:<br />
./check_nt_by_ssh_tunnel.sh 83.121.233.2 14880 someusername &#8221;-w 80 -c 90 -v MEMUSE&#8221;</p>
<p>You should then either see a valid Nagios plugin-output or get an error-message. Make sure that is works! You may have to modify the script depending on your *nix OS.</p>
<p>Next modify Nagios checkcommands.cfg or similiar and add the following. This makes the command available for use in Nagios.</p>
<pre name="code" class="html">
# USAGE: check_nt_by_ssh_tunnel!11101!username!"-l 5,80,90 -v CPULOAD"
define command{
	command_name		check_nt_by_ssh_tunnel
	command_line		PATH_TO_COMMAND/check_nt_by_ssh_tunnel.sh $HOSTADDRESS$ $ARG1$ $ARG2$ $ARG3$
}
</pre>
<p>Next add a service definition that uses the check-command.</p>
<pre name="code" class="html">
define service{
	use				       generic-service
        host_name                        MY_HOSTNAME
        service_description             Memory usage
        check_command                 check_nt_by_ssh_tunnel!11100!username!"-w 80 -c 90 -v MEMUSE"
}
</pre>
<p>The last step you need to do is to restart Nagios and check if it works.</p>
<p>* I recommend you to change the portnumber for each service-definition to avoid collisions. Make them unique to the service-check.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marcusnyberg.com/2010/01/04/nagios-check_nt-over-ssh-tunnel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bash script that open and close an ssh-tunnel automagically</title>
		<link>http://www.marcusnyberg.com/2010/01/04/bash-script-that-open-and-close-an-ssh-tunnel-automagically/</link>
		<comments>http://www.marcusnyberg.com/2010/01/04/bash-script-that-open-and-close-an-ssh-tunnel-automagically/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 13:46:57 +0000</pubDate>
		<dc:creator>marcus</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.marcusnyberg.com/?p=361</guid>
		<description><![CDATA[My problem was that I needed to connect to a port on a server that only was accessible by ssh. This connection should be made in a bash-script that starts and ends within a limited timeframe. That&#8217;s why I needed to solve the problem with a SSH-tunnel. George Notaras has made an article about auto-closing [...]]]></description>
			<content:encoded><![CDATA[<p>My problem was that I needed to connect to a port on a server that only was accessible by ssh. This connection should be made in a bash-script that starts and ends within a limited timeframe. That&#8217;s why I needed to solve the problem with a SSH-tunnel. George Notaras has made an article about <a href="http://www.g-loaded.eu/2006/11/24/auto-closing-ssh-tunnels/">auto-closing ssh tunnels</a> that describes the problem and a solution that should work for most people. However, I did not need a auto-closing ssh tunnel because I want to close the tunnel whenever I want to. </p>
<p>My solution is a little bit more brute because it kills the tunnel. But hey, it works! Check out the simple solution below.</p>
<p><strong>Prerequisites</strong><br />
For this to work you need to have <a href="http://bloggerdigest.blogspot.com/2006/11/ssh-auto-login-or-passwordless-login.html">ssh auto login</a> between the two servers.</p>
<p>I have used this solution successfully with the Nagios monitoring system. Read <a href="http://www.marcusnyberg.com/2010/01/04/nagios-check_nt-over-ssh-tunnel/">Nagios check_nt over ssh-tunnel</a>.</p>
<pre name="code" class="html">
#!/bin/bash

# Open tunnel and wait for it to open
# 12489 = remote portnumber
# 3456  = local portnumber
# increase sleep-time if slow to connect
ssh -f -N -L 3456:localhost:12489 username@somehost &#038;
sleep 30

# Run some command that uses the opened tunnel
# telnet localhost 3456

# Close the tunnel by killing it
sleep 5
CMD="ps -eo pid,args | grep 'ssh -f -N -L 3456:localhost' | grep -v  'grep' | cut -c1-6"
#echo $CMD
PID=`eval $CMD`
#echo $PID
kill -9 $PID
</pre>
<p>You may have to change the parameters to the ps-command for this to work on your nix-machine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.marcusnyberg.com/2010/01/04/bash-script-that-open-and-close-an-ssh-tunnel-automagically/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
