<?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>dopefish.de &#187; bash</title>
	<atom:link href="http://www.dopefish.de/archives/tag/bash/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dopefish.de</link>
	<description>Headquaters of the evil genius</description>
	<lastBuildDate>Mon, 23 Jan 2012 13:34:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How to build an efficient GeoIP SQL table</title>
		<link>http://www.dopefish.de/archives/1037</link>
		<comments>http://www.dopefish.de/archives/1037#comments</comments>
		<pubDate>Mon, 22 Aug 2011 19:17:48 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[GeoIP]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=1037</guid>
		<description><![CDATA[This here is a very handy little script I threw together to generate a geoip.sql table for quickly determining which country a IP is from. I already hear you saying &#8220;Just convert the IP to an INT and use BETWEEN, how hard can it be&#8221;. And you are right, that works. And it may even [...]]]></description>
			<content:encoded><![CDATA[<p>This here is a very handy little script I threw together to generate a geoip.sql table for quickly determining which country a IP is from. I already hear you saying &#8220;Just convert the IP to an INT and use BETWEEN, how hard can it be&#8221;. And you are right, that works. And it may even be your easiest solution, but it just isn&#8217;t fast. And if you are planning on hammering the table with thousands of queries you are going to end up looking for something fast.</p>
<p>A while back I found a very interesting posting at www.<a href="http://jcole.us/blog/archives/2007/11/24/on-efficiently-geo-referencing-ips-with-maxmind-geoip-and-mysql-gis/" target="_blank">jcole.us</a>&nbsp;that described how to use <a href="http://dev.mysql.com/doc/refman/5.0/en/creating-spatial-indexes.html" target="_blank">Spacial Indexes</a>&nbsp;together with <a href="http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html" target="_blank">MySQL&#8217;s GIS</a> to speed up the queries. The posting has been online for a while and both it and the replies are worth reading.</p>
<p>All I did was make a small bash script to download the current &#8220;lite&#8221; version of GeoIP CSV file from maxmind.com, use the information from the posting to throw/transform it into a local database table and dump out a .sql file that can be easily imported into any other database. The script isn&#8217;t failproof though, it expects your user to be able to use mysql and have permission to create databases/tables and &#8220;load data local infile&#8221;.</p>
<p><strong><a href="http://dopefish.de/bash/generate_geoip_sql.sh" target="_blank">generate_geoip_sql.sh</a></strong></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#          FILE: generate_geoip_sql.sh</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#         USAGE: ./generate_geoip_sql.sh</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#   DESCRIPTION: Generates a optimized sql dump with geoipdata from maxmind.com</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#         USAGE: import sql into a database</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#  REQUIREMENTS: mysql database rights to create databases and LOAD DATA LOCAL INFILE</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#       EXAMPLE:</span>
<span style="color: #666666; font-style: italic;">#                 SELECT country_code,country_name</span>
<span style="color: #666666; font-style: italic;">#                 FROM   geo_ip</span>
<span style="color: #666666; font-style: italic;">#                 WHERE  MBRCONTAINS(ip_poly, POINTFROMWKB(POINT(INET_ATON('1.2.3.4'), 0)))</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#                 SELECT country_code,country_name</span>
<span style="color: #666666; font-style: italic;">#                 FROM   geo_ip</span>
<span style="color: #666666; font-style: italic;">#                 WHERE  INET_ATON('1.2.3.4') </span>
<span style="color: #666666; font-style: italic;">#                 BETWEEN ip_from AND ip_to;</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#        AUTHOR:  Ryan Schulze (rs), ryan@dopefish.de</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> nounset                              <span style="color: #666666; font-style: italic;"># Treat unset variables as an error</span>
&nbsp;
<span style="color: #007800;">CSVFile</span>=<span style="color: #ff0000;">&quot;GeoIPCountryWhois.csv&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #800000;">${CSVFile}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-en</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #007800;">${CSVFile}</span> already exists, download a newer version? [Y/n]: &quot;</span>
  <span style="color: #c20cb9; font-weight: bold;">read</span> answer
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(echo $answer|tr &quot;N&quot; &quot;n&quot;)</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;n&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
  <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #800000;">${CSVFile}</span>
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #800000;">${CSVFile}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">tmpfile</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">mktemp</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
  <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> <span style="color: #800000;">${tmpfile}</span> http:<span style="color: #000000; font-weight: bold;">//</span>geolite.maxmind.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>geoip<span style="color: #000000; font-weight: bold;">/</span>database<span style="color: #000000; font-weight: bold;">/</span>GeoIPCountryCSV.zip
  <span style="color: #c20cb9; font-weight: bold;">unzip</span> <span style="color: #800000;">${tmpfile}</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #800000;">${tmpfile}</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>generating geoip.sql&quot;</span>
&nbsp;
mysql <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;create database if not exists temp&quot;</span>
mysql <span style="color: #660033;">-D</span> temp <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;drop table if exists geo_ip&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'
CREATE TABLE geo_ip
(
  id           INT UNSIGNED  NOT NULL auto_increment,
  ip_poly      POLYGON       NOT NULL,
  ip_from      INT UNSIGNED  NOT NULL,
  ip_to        INT UNSIGNED  NOT NULL,
  country_code CHAR(2)       NOT NULL,
  country_name CHAR(50)      NOT NULL,
  PRIMARY KEY (id),
  SPATIAL INDEX (ip_poly)
);
LOAD DATA LOCAL INFILE &quot;GeoIPCountryWhois.csv&quot;
INTO TABLE geo_ip
FIELDS
  TERMINATED BY &quot;,&quot;
  ENCLOSED BY &quot;\&quot;&quot;
LINES
  TERMINATED BY &quot;\n&quot;
(
  @ip_from_string, @ip_to_string,
  @ip_from, @ip_to,
  @country_code, @country_string
)
SET
  id      := NULL,
  ip_from := @ip_from,
  ip_to   := @ip_to,
  ip_poly := GEOMFROMWKB(POLYGON(LINESTRING(
    /* clockwise, 4 points and back to 0 */
    POINT(@ip_from, -1), /* 0, top left */
    POINT(@ip_to,   -1), /* 1, top right */
    POINT(@ip_to,    1), /* 2, bottom right */
    POINT(@ip_from,  1), /* 3, bottom left */
    POINT(@ip_from, -1)  /* 0, back to start */
  ))),
  country_code := @country_code,
  country_name := @country_string
;
'</span> <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-D</span> temp
&nbsp;
mysqldump <span style="color: #660033;">--opt</span> temp geo_ip <span style="color: #000000; font-weight: bold;">&gt;</span> geoip.sql
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>done generating geoip.sql&quot;</span></pre></td></tr></table></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/1037/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to install ruby 1.9.2 on Ubuntu 10.04</title>
		<link>http://www.dopefish.de/archives/1018</link>
		<comments>http://www.dopefish.de/archives/1018#comments</comments>
		<pubDate>Thu, 07 Jul 2011 18:49:52 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[LTS]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Ubuuntu]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=1018</guid>
		<description><![CDATA[The current LTS version of Ubuntu is 10.04 and the most current version of ruby it ships with is 1.9.1. Unfurtunately 1.9.1 wasn&#8217;t that great of a release and anyone using the 1.9 branch really should use the stable 1.9.2.
After doing a bit of researching I found some information on how the best approach to [...]]]></description>
			<content:encoded><![CDATA[<p>The current <a title="Ubuntu LTS" href="https://wiki.ubuntu.com/LTS" target="_blank">LTS</a> version of Ubuntu is 10.04 and the most current version of ruby it ships with is 1.9.1. Unfurtunately 1.9.1 wasn&#8217;t that great of a release and anyone using the 1.9 branch really should use the stable 1.9.2.</p>
<p>After doing a bit of researching I found some information on how the best approach to get ruby installed is. Downloading the source, compiling it and registering the installed version with the package manager.</p>
<p>The following little bash script takes care of installing ruby 1.9.2 on a ubuntu or debian based system (or any other version if you change the $Version variable in the script). The script just consolidates information found online and wraps it up into a nice bashscript</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">#          FILE:  install_ruby_1.9.sh</span>
<span style="color: #666666; font-style: italic;"># </span>
<span style="color: #666666; font-style: italic;">#         USAGE:  ./install_ruby_1.9.sh </span>
<span style="color: #666666; font-style: italic;"># </span>
<span style="color: #666666; font-style: italic;">#        AUTHOR: Ryan Schulze (rs), ryan@dopefish.de</span>
<span style="color: #666666; font-style: italic;">#       CREATED: 07/07/2011 11:59:37 AM CDT</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
&nbsp;
<span style="color: #007800;">Version</span>=<span style="color: #ff0000;">&quot;1.9.2-p180&quot;</span>
<span style="color: #007800;">GZFile</span>=<span style="color: #ff0000;">&quot;ruby-<span style="color: #007800;">${Version}</span>.tar.gz&quot;</span>
<span style="color: #007800;">Download</span>=<span style="color: #ff0000;">&quot;http://ftp.ruby-lang.org/pub/ruby/1.9/<span style="color: #007800;">${GZFile}</span>&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(id -u)</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You need root permission to execute this script&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #660033;">-q</span> update
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #660033;">-qy</span> upgrade
<span style="color: #c20cb9; font-weight: bold;">apt-get install</span> <span style="color: #660033;">-qy</span> build-essential <span style="color: #c20cb9; font-weight: bold;">wget</span> zlib1g-dev libssl-dev libffi-dev <span style="color: #c20cb9; font-weight: bold;">autoconf</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-e</span> <span style="color: #800000;">${GZFile}</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #800000;">${Download}</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xzf</span> <span style="color: #800000;">${GZFile}</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ruby-<span style="color: #800000;">${Version}</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">autoconf</span>
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--with-ruby-version</span>=<span style="color: #800000;">${Version}</span> <span style="color: #660033;">--prefix</span>=<span style="color: #000000; font-weight: bold;">/</span>usr <span style="color: #660033;">--program-suffix</span>=<span style="color: #800000;">${Version}</span> 
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${Version}</span><span style="color: #000000; font-weight: bold;">/</span>bin
&nbsp;
update-alternatives \
	<span style="color: #660033;">--install</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ruby ruby <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #800000;">${Version}</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #800000;">${Version//./}</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">cut</span> <span style="color: #660033;">-d-</span> -f1<span style="color: #7a0874; font-weight: bold;">&#41;</span> \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>man<span style="color: #000000; font-weight: bold;">/</span>man1<span style="color: #000000; font-weight: bold;">/</span>ruby.1.gz ruby.1.gz <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>man<span style="color: #000000; font-weight: bold;">/</span>man1<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #800000;">${Version}</span>.1 \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span>bin        gem-bin   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>ruby<span style="color: #000000; font-weight: bold;">/</span>gems<span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${Version}</span><span style="color: #000000; font-weight: bold;">/</span>bin \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>erb  erb  <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>erb<span style="color: #800000;">${Version}</span> \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>irb  irb  <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>irb<span style="color: #800000;">${Version}</span> \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rdoc rdoc <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rdoc<span style="color: #800000;">${Version}</span> \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ri   ri   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ri<span style="color: #800000;">${Version}</span> \
	<span style="color: #660033;">--slave</span>   <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>gem  gem  <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>gem<span style="color: #800000;">${Version}</span> \
&nbsp;
update-alternatives <span style="color: #660033;">--config</span> ruby
update-alternatives <span style="color: #660033;">--display</span> gem <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> update-alternatives <span style="color: #660033;">--remove-all</span> gem
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[+] Done installing&quot;</span>
ruby <span style="color: #660033;">-v</span></pre></div></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/1018/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to easily add colored text output in bash scripts</title>
		<link>http://www.dopefish.de/archives/969</link>
		<comments>http://www.dopefish.de/archives/969#comments</comments>
		<pubDate>Tue, 31 May 2011 22:29:36 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Me]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=969</guid>
		<description><![CDATA[Here is small snippet that can give your shell scripts some nice output: colortext.sh As with the debug.sh script, just download it to the same directory as your own script and add it with

Select All Code:. colortext.sh

It contains one simple function called text with the syntax text &#60;color&#62; &#8220;text to be output&#8221;. Color can be red, [...]]]></description>
			<content:encoded><![CDATA[<p>Here is small snippet that can give your shell scripts some nice output: <a href="http://www.dopefish.de/files/colortext.sh" target="_blank">colortext.sh</a> As with the <a href="http://www.dopefish.de/archives/873" target="_blank">debug.sh</a> script, just download it to the same directory as your own script and add it with</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. colortext.sh</pre></div></div></div>

<p>It contains one simple function called <span style="color: #0000ff;">text</span> with the syntax <span style="color: #0000ff;">text &lt;color&gt; &#8220;text to be output&#8221;</span>. Color can be red, green, yellow, blue or grey. The function does not automatically add a linebreak to the putput, so pop a <span style="color: #0000ff;">\n</span> in there if you need it. I prefer using it together with printf for clean and easy color output.</p>
<p>Here are some examples of how the function can be used, and below the corresponding output:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. colortext.sh
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;normal text&quot;</span>
text blue <span style="color: #ff0000;">&quot;blue text, &quot;</span>
text yellow <span style="color: #ff0000;">&quot;yellow text<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Using it together with echo</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Status of script: [&quot;</span>;text <span style="color: #c20cb9; font-weight: bold;">red</span> <span style="color: #ff0000;">&quot;ERROR&quot;</span>;<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;]&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># more elegant usage with printf</span>
<span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;Status of script: [%s]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span>text green <span style="color: #ff0000;">&quot;OK&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div></div>

<p>Output:</p>
<blockquote><p>normal text<br />
<span style="color: #0000ff;">blue text</span>, <span style="color: #808000;">yellow text<br />
</span> Status of script: [<span style="color: #ff0000;">ERROR</span>]<br />
Status of script: [<span style="color: #339966;">OK</span>]</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/969/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to add debugging to shellscripts</title>
		<link>http://www.dopefish.de/archives/873</link>
		<comments>http://www.dopefish.de/archives/873#comments</comments>
		<pubDate>Wed, 04 May 2011 21:44:13 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[debug]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=873</guid>
		<description><![CDATA[Debugging bash scripts is pretty straightforward, throwing around a couple echo and set -x quickly gives you what you need. But what if you want to add a nice breakpoint,  debugging to lots of paces in the code or turn all debugging on or off at once? Then this little script I wrote is the [...]]]></description>
			<content:encoded><![CDATA[<p>Debugging bash scripts is pretty straightforward, throwing around a couple <span style="color: #3366ff;">echo</span> and <span style="color: #3366ff;">set -x</span> quickly gives you what you need. But what if you want to add a nice breakpoint,  debugging to lots of paces in the code or turn all debugging on or off at once? Then this little script I wrote is the right thing for you: <a href="http://www.dopefish.de/files/debug.sh" target="_blank">debug.sh</a> just download it to the same directory as your script and include it with the following line:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">. debug.sh</pre></div></div></div>

<p>It contains 4 simple functions that will make your bash coding easier.<br />
<span style="color: #3366ff;">debug</span> and <span style="color: #3366ff;">breakpoint</span> both print the argument with a timestamp to STDERR<br />
You can turn off all the functions by adding a <span style="color: #3366ff;">DEBUG=false</span> into your code</p>
<p>Example:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
. debug.sh
&nbsp;
debug_on
<span style="color: #7a0874; font-weight: bold;">echo</span> foo
debug_off
&nbsp;
debug <span style="color: #ff0000;">&quot;I am a test&quot;</span>
&nbsp;
breakpoint <span style="color: #ff0000;">&quot;wait for a bit&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">done</span></pre></div></div></div>

<p>Output:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">+ <span style="color: #7a0874; font-weight: bold;">echo</span> foo
foo
+ debug_off
+ <span style="color: #c20cb9; font-weight: bold;">true</span>
+ <span style="color: #000000; font-weight: bold;">set</span> +x
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">16</span>:<span style="color: #000000;">37</span>:<span style="color: #000000;">47</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> I am a <span style="color: #7a0874; font-weight: bold;">test</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">16</span>:<span style="color: #000000;">37</span>:<span style="color: #000000;">47</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-breakpoint-</span> <span style="color: #7a0874; font-weight: bold;">wait</span> <span style="color: #000000; font-weight: bold;">for</span> a bit
press any key to <span style="color: #7a0874; font-weight: bold;">continue</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/873/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wireshark remote capturing</title>
		<link>http://www.dopefish.de/archives/749</link>
		<comments>http://www.dopefish.de/archives/749#comments</comments>
		<pubDate>Tue, 08 Mar 2011 19:27:50 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[tcpdump]]></category>
		<category><![CDATA[wireshark]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=749</guid>
		<description><![CDATA[yeah, this is real simple stuff, not really worth writing a script for it. but on the other hand it saves me from remembering how to do it every time I need it (which isn&#8217;t often). So here is a little script to setup remote capturing with wireshark.
All it basically does is ssh to the [...]]]></description>
			<content:encoded><![CDATA[<p>yeah, this is real simple stuff, not really worth writing a script for it. but on the other hand it saves me from remembering how to do it every time I need it (which isn&#8217;t often). So here is a little script to setup remote capturing with wireshark.<br />
All it basically does is ssh to the remote host and tcpdump sucking the output via stdout through the ssh connection to a local pipe, that is then used by wireshark to display the stream. Because of this you may want to make sure you aren&#8217;t capturing your own ssh data when doing this <img src='http://www.dopefish.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">destination</span>=<span style="color: #ff0000;">&quot;1.2.3.4&quot;</span> <span style="color: #666666; font-style: italic;"># server to run the remote capture on</span>
<span style="color: #007800;">filter</span>=<span style="color: #ff0000;">&quot;port 80&quot;</span>      <span style="color: #666666; font-style: italic;"># tcpdump filter options</span>
<span style="color: #007800;">interface</span>=<span style="color: #ff0000;">&quot;eth0&quot;</span>      <span style="color: #666666; font-style: italic;"># interface to listen on on remote server</span>
<span style="color: #007800;">opts</span>=<span style="color: #ff0000;">&quot;-n -p -s 0&quot;</span>     <span style="color: #666666; font-style: italic;"># other tcmpdump options</span>
&nbsp;
<span style="color: #666666; font-style: italic;">##### let the fun begin #####</span>
&nbsp;
<span style="color: #007800;">mypipe</span>=<span style="color: #ff0000;">&quot;/tmp/remotecap.$$.cap&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">mkfifo</span> <span style="color: #800000;">${mypipe}</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">ssh</span> root<span style="color: #000000; font-weight: bold;">@</span><span style="color: #800000;">${destination}</span> <span style="color: #ff0000;">&quot;tcpdump <span style="color: #007800;">${opts}</span> -i <span style="color: #007800;">${interface}</span> -w - <span style="color: #007800;">${filter}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #800000;">${mypipe}</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;
<span style="color: #007800;">pipepid</span>=<span style="color: #007800;">$!</span>
&nbsp;
wireshark <span style="color: #660033;">-k</span> <span style="color: #660033;">-N</span> ntC <span style="color: #660033;">-t</span> a <span style="color: #660033;">-i</span> <span style="color: #800000;">${mypipe}</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #800000;">${pipepid}</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #800000;">${mypipe}</span></pre></div></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/749/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XEN 3.4 with ipv6 routing</title>
		<link>http://www.dopefish.de/archives/704</link>
		<comments>http://www.dopefish.de/archives/704#comments</comments>
		<pubDate>Wed, 01 Sep 2010 20:41:54 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[ipv6]]></category>
		<category><![CDATA[patch]]></category>
		<category><![CDATA[routing]]></category>
		<category><![CDATA[xen]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=704</guid>
		<description><![CDATA[Yes, there are a few postings out there about getting ipv6 routing running with XEN. But I&#8217;ll throw this online anyway since there are a few changes I had to make for it to work on my server. This text is intended for people who know their way around Linux and XEN so it will [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, there are a few postings out there about getting ipv6 routing running with XEN. But I&#8217;ll throw this online anyway since there are a few changes I had to make for it to work on my server. This text is intended for people who know their way around Linux and XEN so it will be a bit technical and won&#8217;t spell out every single step you have to make. </p>
<p>Most of the changes are based off scripts and information from <a href="http://notes.benv.junerules.com/all/software/xen-and-routed-ipv6/">BenV</a> and <a href="http://wnagele.com/2010/07/13/ipv6-and-xen-on-a-hetzner-hosted-system/">wnagele</a> (latter is interesting for me since I am also running XEN on a hetzner server). Have a look at the two links if anything is unclear. Now let&#8217;s start the fun <img src='http://www.dopefish.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>First of all we need IPv6 up and running on the host (dom0). Add the IP and gateway to your /etc/network/interfaces<br />
This is what mine looks like:<br />
<code lang="text">iface eth0 inet6 static<br />
  address     2a01:4f8:100:1123::2<br />
  netmask     64<br />
  gateway     2a01:4f8:100:1120::1<br />
  pre-up ip -6 route add 2a01:4f8:100:1120::1 dev eth0</code><br />
Check if the IP address is responding to the outside world (e.g. with <a href="http://www.wiberg.nu/iptools.php">wiberg.nu/iptools.php</a>), if everything looks ok, proceed &#8230;<br />
Now we need to enable a few things to get routing and neighbor discovery running on the host (dom0). Edit your /etc/sysctl.conf and add/change these 2 entries (and while you are at it, set them with &#8220;sysctl -w&#8221; too):<br />
<code lang="text">net.ipv6.conf.all.forwarding=1<br />
net.ipv6.conf.all.proxy_ndp=1</code></p>
<p>So, your host should by now be online with ipv6 and soon be able to route packets to it&#8217;s guests. By default XEN will only take care of IPv4 when a guest is created, so here is a small patchfile that adds support for IPv6: <a href="http://www.dopefish.de/files/xen-ipv6-vif-route.patch">xen-ipv6-vif-route.patch</a>. The patch changes vif-route and vif-common.sh, while these files may be in different places depending on your distribution, /etc/xen/scripts/ is where they can commonly be found. Download the patch to the directory with the scripts to be changed and execute a &#8220;patch -p0 < xen-ipv6-vif-route.patch" (vif-common.sh gets a few new IPv6 functions, and iptables now won't try to change stuff for IPv6 IPs. vif-route changes are: ndp is enabled for the vif device and the route/neighbor IPv6 settings are set)</p>
<p>So, now that the scripts know how to setup all our IPv6 needs, we need to add the IPv6 IP to our guest settings (.cfg file typically found in /etc/xen/). What we want to change is the "vif" setting. Add the IPv6 IP of the guest to the IPv4 IP (just the IP without the trailing /network, space separated form the IPv4 IP):<br />
<code lang="text">vif = [ 'mac=B1:A3:3F:25:11:B8, ip=2a01:4f8:100:1123::5 188.40.34.101' ]</code></p>
<p>Now you can create the guest(domU) and add the IPv6 IP to the /etc/network/interfaces of the guest if you haven't so already (it uses the host (dom0) as the gateway).<br />
<code lang="text"><br />
  iface eth0 inet6 static<br />
  address     2a01:4f8:100:1123::5<br />
  netmask     64<br />
  gateway     2a01:4f8:100:1123::2<br />
</code><br />
Restart the networking on the guest (or reboot it) and you should now be able to ping the guest from the internet. See, easy wasn't it <img src='http://www.dopefish.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/704/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script of the day &#8211; clean up stale .ssh/known_hosts</title>
		<link>http://www.dopefish.de/archives/702</link>
		<comments>http://www.dopefish.de/archives/702#comments</comments>
		<pubDate>Thu, 19 Aug 2010 10:24:12 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Internet Stuff]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=702</guid>
		<description><![CDATA[This little script takes an IP or hostname as a parameter, and if there is an offending key in the .ssh/known_hosts it removes it and replaces it with the current valid one useful if you are moving/reinstalling a large amount of servers &#8230;

Select All Code:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
#===============================================================================
# FILE: ssh-cleankey.sh
# USAGE: ./ssh-cleankey.sh 
#
# DESCRIPTION: deletes stale ssh known_hosts [...]]]></description>
			<content:encoded><![CDATA[<p>This little script takes an IP or hostname as a parameter, and if there is an offending key in the .ssh/known_hosts it removes it and replaces it with the current valid one useful if you are moving/reinstalling a large amount of servers &#8230;</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
<span style="color: #666666; font-style: italic;"># FILE: ssh-cleankey.sh</span>
<span style="color: #666666; font-style: italic;"># USAGE: ./ssh-cleankey.sh </span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># DESCRIPTION: deletes stale ssh known_hosts entries</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># true or fasle</span>
<span style="color: #007800;">VERBOSE</span>=<span style="color: #c20cb9; font-weight: bold;">false</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#=== Exit codes ================================================================</span>
<span style="color: #666666; font-style: italic;"># 1 - Not a valid IP or not reachable</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#=== FUNCTION ================================================================</span>
<span style="color: #666666; font-style: italic;"># NAME: print_help</span>
<span style="color: #666666; font-style: italic;"># DESCRIPTION: Prints help and exits</span>
<span style="color: #666666; font-style: italic;">#===============================================================================</span>
print_help<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #666666; font-style: italic;">#{{{</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: <span style="color: #780078;">`basename $0`</span> &quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;e.g. ./<span style="color: #780078;">`basename $0`</span> 1.2.3.4&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #666666; font-style: italic;">#}}}</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #007800;">HOST</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${1}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span>
print_help
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">ping</span> <span style="color: #660033;">-w1</span> <span style="color: #660033;">-c1</span> <span style="color: #007800;">$HOST</span> <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #007800;">$VERBOSE</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ERROR: <span style="color: #007800;">$HOST</span> is either not a valid IP/hostname, or is not reachable via ping&quot;</span>
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">Check</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">connecttimeout</span>=<span style="color: #000000;">10</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">stricthostkeychecking</span>=no <span style="color: #007800;">$HOST</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;Offending key&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$Check</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #007800;">$VERBOSE</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOST</span> is stale, updating known_hosts&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-R</span> <span style="color: #007800;">$HOST</span> <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
<span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">connecttimeout</span>=<span style="color: #000000;">10</span> <span style="color: #660033;">-o</span> <span style="color: #007800;">stricthostkeychecking</span>=no <span style="color: #007800;">$HOST</span> <span style="color: #c20cb9; font-weight: bold;">true</span> <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #007800;">$VERBOSE</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOST</span> is OK&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></td></tr></table></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/702/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking a list of IPs against RBL</title>
		<link>http://www.dopefish.de/archives/697</link>
		<comments>http://www.dopefish.de/archives/697#comments</comments>
		<pubDate>Tue, 10 Aug 2010 09:52:07 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=697</guid>
		<description><![CDATA[This is more a reminder to myself than anything else &#8230; this is small snippet that takes a list of IPs and does a whois on all that aren&#8217;t in a RBL
Lets say we have al list of IPs in a file &#8220;iplist.txt&#8221;:
12.172.121.171
24.149.208.68
38.105.100.9
58.185.207.86
61.201.51.66
64.78.164.169
Snippet that checks the IPs (can of course be easily changed to check [...]]]></description>
			<content:encoded><![CDATA[<p>This is more a reminder to myself than anything else &#8230; this is small snippet that takes a list of IPs and does a whois on all that aren&#8217;t in a RBL</p>
<p>Lets say we have al list of IPs in a file &#8220;iplist.txt&#8221;:<br />
<code lang="text">12.172.121.171<br />
24.149.208.68<br />
38.105.100.9<br />
58.185.207.86<br />
61.201.51.66<br />
64.78.164.169</code></p>
<p>Snippet that checks the IPs (can of course be easily changed to check IPs that are IN a RBL)<br />
<span style="font-family: monospace;"></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">ip</span> <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">cat</span> iplist.txt<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #7a0874; font-weight: bold;">test</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rblcheck <span style="color: #660033;">-qm</span> <span style="color: #800000;">${ip}</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">continue</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #c20cb9; font-weight: bold;">clear</span>
<span style="color: #c20cb9; font-weight: bold;">whois</span> <span style="color: #007800;">$ip</span>
<span style="color: #7a0874; font-weight: bold;">printf</span> <span style="color: #ff0000;">&quot;###########################<span style="color: #000099; font-weight: bold;">\n</span>##### %15s #####<span style="color: #000099; font-weight: bold;">\n</span>###########################<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${ip}</span>&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">read</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div></div>

<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/697/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>bash: using the content of a variable as variable name</title>
		<link>http://www.dopefish.de/archives/685</link>
		<comments>http://www.dopefish.de/archives/685#comments</comments>
		<pubDate>Thu, 05 Aug 2010 11:14:21 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=685</guid>
		<description><![CDATA[Since the implementation of Arrays in Bash is somewhat lacking compared to higher level programming languages (only one-dimensional), and hash lists require a bit of work to set up, you may run into a situation where you have a small list of key/value pairs that are both variable and you need to store.
There are various [...]]]></description>
			<content:encoded><![CDATA[<p>Since the implementation of <a href="http://tldp.org/LDP/abs/html/arrays.html#ARRAYREF">Arrays</a> in Bash is somewhat lacking compared to higher level programming languages (only one-dimensional), and hash lists require <a href="http://tldp.org/LDP/abs/html/contributed-scripts.html#HASHEX2">a bit of work</a> to set up, you may run into a situation where you have a small list of key/value pairs that are both variable and you need to store.<br />
There are various solutions for the problem, e.g. creating two arrays (one for the keys, one for the values, and combining them by using the same index values for the entries), or using the functions from the link above to build a hash list. For me the easiest way to solve the problem, if I only have a few variables and don&#8217;t want to bloat the code, is to (mis)use <strong><a href="http://tldp.org/LDP/abs/html/declareref.html">declare</a></strong>. declare is intended for setting the type of a variable (constant, array, integer,&#8230;), but has the nice side affect that you can use variables in the key name, and you can set the value of the variable.</p>
<p>usage:<br />
<code lang="bash">declare ${Key}=${Value}</code></p>
<p>Example:<br />
$File_Config is variable holding the name of a configfile, the content of the file could look like this:<br />
<code lang="text">foo:one<br />
bar:three<br />
foobar:bignumber</code></p>
<p>Snippet:<br />
<span style="font-family: monospace;"></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${File_Config}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">read</span> line
<span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #007800;">Key</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${line%%:*}</span>&quot;</span> <span style="color: #666666; font-style: italic;"># chop off everything after the first :</span>
<span style="color: #007800;">Value</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${line#*:}</span>&quot;</span> <span style="color: #666666; font-style: italic;"># chop off everything before the first :</span>
<span style="color: #007800;">Value</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$(echo ${Value}|sed 's#^ +##g;s# +$##g')</span>&quot;</span> <span style="color: #666666; font-style: italic;"># chop off any excess whitespace</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${Key}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${Value}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;"># only continue if we have something to do</span>
<span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">declare</span> Configuration_<span style="color: #800000;">${Key}</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${Value}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${File_Config}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></td></tr></table></div></div>

<p></span></p>
<p>after the snippet has read the configfile, you can use <em>$Configuration_foo</em>, <em>$Configuration_bar</em> and <em>$Configuration_foobar</em> in your script. The keynames could also have came from a mysql query, array, command line args, &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/685/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mythtv and IR Blaster</title>
		<link>http://www.dopefish.de/archives/591</link>
		<comments>http://www.dopefish.de/archives/591#comments</comments>
		<pubDate>Sun, 24 Jan 2010 15:19:45 +0000</pubDate>
		<dc:creator>dopefish</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[ir blaster]]></category>
		<category><![CDATA[MythTV]]></category>

		<guid isPermaLink="false">http://www.dopefish.de/?p=591</guid>
		<description><![CDATA[We use a set-top-box as the video source for mythtv. And while this works perfectly fine, we have to manually make sure the right channel is set for what mythtv wants to record. This can turn out to being a pain at time, especially when multiple things are planned to be recorded on different channels. [...]]]></description>
			<content:encoded><![CDATA[<p>We use a set-top-box as the video source for <a href="http://www.mythtv.org/">mythtv</a>. And while this works perfectly fine, we have to manually make sure the right channel is set for what mythtv wants to record. This can turn out to being a pain at time, especially when multiple things are planned to be recorded on different channels. So I went and ordered a IR transmitter from <a href="http://www.irblaster.info/">irblaster.info</a> to give mythtv the possibility to change channels itself.</p>
<p>Since a bunch of the infos I found online were a bit out dated, here are a few tips for anyone using current versions of the software (I&#8217;ve got mythtv-backend 0.22 and lirc 0.8.4a running on a mythbuntu based system when I wrote this)</p>
<p>Hardware is easy to set up, just plug it into a free serial port.<br />
1st we will head over to <a href="http://lirc.sourceforge.net/remotes/">http://lirc.sourceforge.net/remotes/</a> and grab the file for our reciever set-top-box if we don&#8217;t already have them.<br />
Then go edit /etc/lirc/hardware.conf and add the transmitter settings, these were mine:<br />
<code lang="bash">TRANSMITTER="Skymaster_XL10"<br />
TRANSMITTER_MODULES="lirc_dev lirc_serial"<br />
TRANSMITTER_DRIVER=""<br />
TRANSMITTER_DEVICE="/dev/lirc1"<br />
TRANSMITTER_LIRCD_CONF="skymaster/XL10"<br />
TRANSMITTER_LIRCD_ARGS=""</code><br />
now include the reciever settings to /etc/lirc/lircd.conf<br />
I edited my /etc/init.d/lirc to execute the following line before starting the daemon (was needed to ensure the lirc_serial module can be loaded):<br />
<code lang="bash">/bin/setserial /dev/ttyS0 uart none</code><br />
Restart lircd and you can then start testing if transmitting codes work with irsend works. here is a little wrapper script I wrote around irsend for mythtv to use. You may have to twiddle around with the sleeps and change the $Remote to your reciever. If the sleeps are too long (-&gt; the script takes to long to finish switching channels), then mythtv will timeout and not display live tv.</p>
<p><span style="font-family: monospace;"></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Select All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">Remote</span>=<span style="color: #ff0000;">&quot;Skymaster_XL10&quot;</span>
&nbsp;
<span style="color: #007800;">send</span>=<span style="color: #ff0000;">&quot;irsend -d /dev/lircd1 SEND_ONCE&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(echo $1|grep -c &quot;^[0-9]&quot;)</span>&quot;</span> = <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;"># non-numeric command</span>
<span style="color: #007800;">$send</span> <span style="color: #007800;">$Remote</span> <span style="color: #007800;">$1</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #666666; font-style: italic;"># numeric</span>
<span style="color: #000000; font-weight: bold;">for</span> digit <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/./&amp; /g'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #007800;">$send</span> <span style="color: #007800;">$Remote</span> <span style="color: #007800;">$digit</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1.5</span>
<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span></pre></td></tr></table></div></div>

<p></span></p>
<p>If everything is working fine, then go ahead and tell mythtv to use the script to change channels. This is done in the tuner card setup of mythtv-backend (mythtv-setup).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dopefish.de/archives/591/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Served from: www.dopefish.de @ 2012-02-08 06:32:58 by W3 Total Cache -->
