<?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>blog.sudosu.net &#187; Shell Scripting</title>
	<atom:link href="http://blog.sudosu.net/category/computers/linux/shell-scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sudosu.net</link>
	<description>Got root?</description>
	<lastBuildDate>Mon, 21 Dec 2009 18:27:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Seamless SSH</title>
		<link>http://blog.sudosu.net/2008/seamless-ssh/</link>
		<comments>http://blog.sudosu.net/2008/seamless-ssh/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 18:59:10 +0000</pubDate>
		<dc:creator>schof</dc:creator>
				<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://blog.sudosu.net/?p=269</guid>
		<description><![CDATA[I&#8217;m transitioning my daily work desktop from OS X Leopard to Kubuntu Hardy. (I&#8217;ll be writing more about that in the future.) My job is split between managing people and doing development and system administration for a bunch of Ubuntu boxes, so running the same platform that I&#8217;m administering makes a lot of sense. I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m transitioning my daily work desktop from OS X Leopard to Kubuntu Hardy. (I&#8217;ll be writing more about that in the future.) My job is split between managing people and doing development and system administration for a bunch of Ubuntu boxes, so running the same platform that I&#8217;m administering makes a lot of sense. I DO miss some of the fit-and-finish of OS X, though, and I haven&#8217;t completely transitioned over to Linux for everything.</p>
<p>Ssh-agent is a great program that lets you add the password to your SSH private key to memory, and then you don&#8217;t need to type in the ssh key passphrase every time. The basic usage is that you start BASH as a child of ssh-agent, and then use a program called ssh-add to prompt you for the password and store it in memory.</p>
<p>On OS X, there&#8217;s a GREAT program called SSHKeychain that handles this, storing the password in your OS X keychain, so it&#8217;s really seemless.</p>
<p>On Linux, you need to type in &#8220;ssh-add&#8221; manually every time you want to store the key, and after that your SSH sessions will be seamless.</p>
<p>However, I&#8217;m always forgetting to do that, and thus getting prompted for the password. Too many seams. I added the following code snippet to the end of my .bashrc file, and thus, every time I open a bash shell, it checks whether ssh-agent has any keys in memory. If it does, the shell starts as normal. If ssh-agent doesn&#8217;t have any keys in memory, it prompts you for the password. Simple, and as seamless as I can make it.</p>
<blockquote><p>## Add key to ssh-add if it has not been added.</p>
<p>ssh-add -l &amp;&gt; /dev/null<br />
SSHADDRESULT=$?<br />
if [ "$SSHADDRESULT" -ne "0" ]; then<br />
ssh-add<br />
fi</p></blockquote>
<p><strong>UPDATE 2008-07-02</strong>: Here&#8217;s a much more succinct way of writing that:</p>
<blockquote><p>ssh-add -l &amp;&gt;/dev/null || ssh-add</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.sudosu.net/2008/seamless-ssh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Correction to Ubuntu Geek&#8217;s &#8220;How To Check Your External IP Address From The Command Line&#8221;</title>
		<link>http://blog.sudosu.net/2007/correction-to-ubuntu-geeks-how-to-check-your-external-ip-address-from-the-command-line/</link>
		<comments>http://blog.sudosu.net/2007/correction-to-ubuntu-geeks-how-to-check-your-external-ip-address-from-the-command-line/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 02:20:26 +0000</pubDate>
		<dc:creator>schof</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Shell Scripting]]></category>

		<guid isPermaLink="false">http://blog.sudosu.net/2007/correction-to-ubuntu-geeks-how-to-check-your-external-ip-address-from-the-command-line/</guid>
		<description><![CDATA[I follow the Ubuntu Geek blog, and have found some very useful tips there. However, there&#8217;s a problem with their latest tip, &#8220;Howto Check you (sic) external IP Address from the command line.&#8221;
Some background: There&#8217;s a very useful website located at whatismyip.com, which reports the IP you used to connect to the site. If you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>I follow the <a href="http://www.ubuntugeek.com/" target="_blank">Ubuntu Geek</a> blog, and have found some very useful tips there. However, there&#8217;s a problem with their latest tip, &#8220;<a href="http://www.ubuntugeek.com/howto-check-you-external-ip-address-from-the-command-line.html" target="_blank">Howto Check you (sic) external IP Address from the command line</a>.&#8221;</p>
<p>Some background: There&#8217;s a very useful website located at <a href="http://whatismyip.com" target="_blank">whatismyip.com</a>, which reports the IP you used to connect to the site. If you&#8217;re on a computer behind a router which does NAT (<a href="http://en.wikipedia.org/wiki/Network_address_translation" target="_blank">Network Address Translation</a>), you can&#8217;t find out what your external IP is by issuing commands on the computer. Your computer&#8217;s address is (for instance) 192.168.1.5, but when traffic reaches the router, the router translates (with NAT) your address to an external IP address, such as 34.23.64.9 (an IP address I just made up).</p>
<p>WhatIsMyIP.com (and other sites like it) sprang up to  fill that void, and give you a simple way of finding out what external IP address you&#8217;re using.</p>
<p>Ubuntu Geek&#8217;s script fetches the whatismyip.com page with wget, parses it to find the IP address, and prints the IP address.</p>
<p>I tried it on my OS X box, and it didn&#8217;t work, because wget isn&#8217;t installed on OS X &#8212; but curl, a similar tool, is.</p>
<p>So I started modifying the script to use curl &#8212; and discovered an interesting comment in the source code to whatismyip.com&#8217;s page:</p>
<blockquote><p> &lt;!&#8211;Please set your code to scrape your IP from www.whatismyip.com/automation/n09230945.asp Please set your code to hit this page at a REASONABLE pace.  For more info, please see our &#8220;What&#8217;s New&#8221; page.&#8211;&gt;</p></blockquote>
<p>Hitting that link gets you just your IP address, which has two benefits over Ubuntu Geek&#8217;s implementation &#8212; it means you have no parsing to do, and it gives a SIGNIFICANTLY lower load to WhatIsMyIP&#8217;s servers. (It&#8217;s rude to slam someone else&#8217;s servers with an automated script &#8212; even though a script that simply fetches a web page once doesn&#8217;t slam a server, 10,000 people running that script would.)</p>
<p>Here&#8217;s my take on Ubuntu Geek&#8217;s BASH script:</p>
<blockquote><p>#!/bin/bash</p>
<p>echo -n &#8220;Your external IP Address is: &#8221;<br />
curl http://www.whatismyip.com/automation/n09230945.asp<br />
echo &#8220;.&#8221;</p></blockquote>
<p>For comparison&#8217;s here&#8217;s Ubuntu Geek&#8217;s original script:</p>
<blockquote><p>#!/bin/bash</p>
<p>echo Your external IP Address is:<br />
wget http://Www.whatismyip.com -O &#8211; -o /dev/null | \<br />
grep &#8216;&lt;TITLE&gt;&#8217; | sed -r &#8217;s/&lt;TITLE&gt;WhatIsMyIP\.com \- //g&#8217; | \<br />
sed -r &#8217;s/&lt;\/TITLE&gt;//g&#8217;<br />
exit 0</p></blockquote>
<p>Moral of the story? Always look for a simpler way of doing things.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sudosu.net/2007/correction-to-ubuntu-geeks-how-to-check-your-external-ip-address-from-the-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setuid &amp; Chown interaction that almost put me in the fetal position, whimpering</title>
		<link>http://blog.sudosu.net/2007/setuid-chown-interaction-that-almost-put-me-in-the-fetal-position-whimpering/</link>
		<comments>http://blog.sudosu.net/2007/setuid-chown-interaction-that-almost-put-me-in-the-fetal-position-whimpering/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 05:43:55 +0000</pubDate>
		<dc:creator>schof</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Shell Scripting]]></category>

		<guid isPermaLink="false">http://blog.sudosu.net/2007/setuid-chown-interaction-that-almost-put-me-in-the-fetal-position-whimpering/</guid>
		<description><![CDATA[I&#8217;m putting together a postinst (post installation) script for a Debian package I created for work. And there&#8217;s a little C program in there that needs to run as root, but be called by a regular user.
Well, there&#8217;s a standard way of doing that in Linux/Unix, called setuid. You set the &#8220;setuid bit&#8221; to on, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m putting together a postinst (post installation) script for a Debian package I created for work. And there&#8217;s a little C program in there that needs to run as root, but be called by a regular user.</p>
<p>Well, there&#8217;s a standard way of doing that in Linux/Unix, called setuid. You set the &#8220;setuid bit&#8221; to on, and the program will run as the owner of the program. So, since it&#8217;s owned by root, a regular user can run a particular program, and have that program run with the permissions that root has. Very handy.</p>
<p>But I couldn&#8217;t figure out why the program was not being installed setuid root. I could see in the postinst script that the command was valid. I could cut-and-paste the chmod line (that set the setuid bit) from the script to the command-line, run it, and it worked perfectly. And there weren&#8217;t any other commands in the postinst script that affected permissions (the setuid bit is a permission bit) for that file.</p>
<p>However, there WAS a chown command later in the script. (It started in a parent directory and recursed into the directory with the C program I was dealing with.) Eventually, I narrowed it down to that chown line, and once I saw chown was the cause, I guess it sort of made sense.  I was able to reproduce the problem by chowning a file after setting the setuid bit.</p>
<p>I guess from a security standpoint, you could do a lot of stupid things by setting the setuid bit (making a program operate as the owner of the program) and then changing the owner. So, to prevent you from shooting yourself in the foot, changing ownership of a file unsets the setuid bit.</p>
<p>Still, this seems somewhat counter to the philosophy of Unix &#8212; first, to not do unexpected things, and second, to give users approximately an order of magnitude more rope than they&#8217;d need to hang themselves. I can&#8217;t think of many other commands that silently prevent you from doing something that MAY be stupid. Unix usually assumes that you know what you&#8217;re doing &#8212; even to the point that you can enter a command to delete every file on your hard drive, and Unix will happily delete all your files without asking for confirmation. The fact that chown has this behavior was NOT obvious to me.</p>
<p>(This is a highly technical rant, and I&#8217;m writing it late at night after coding for a while &#8212; if it doesn&#8217;t make any sense, it&#8217;s probably me, not you.)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sudosu.net/2007/setuid-chown-interaction-that-almost-put-me-in-the-fetal-position-whimpering/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Remove All Subversion Folders From A Directory Tree</title>
		<link>http://blog.sudosu.net/2007/how-to-remove-all-subversion-folders-from-a-directory-tree/</link>
		<comments>http://blog.sudosu.net/2007/how-to-remove-all-subversion-folders-from-a-directory-tree/#comments</comments>
		<pubDate>Sat, 23 Jun 2007 22:20:10 +0000</pubDate>
		<dc:creator>schof</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://blog.sudosu.net/2007/how-to-remove-all-subversion-folders-from-a-directory-tree/</guid>
		<description><![CDATA[Say you&#8217;ve got a Subversion source code tree checked out, and for whatever reason you want to remove all Subversion directories inside that tree. (The thing that makes a Subversion tree a Subversion tree is the presence of a &#8220;.svn&#8221; folder in every folder of the tree. If you had a complicated source tree with [...]]]></description>
			<content:encoded><![CDATA[<p>Say you&#8217;ve got a <a href="http://subversion.tigris.org/" title="Subversion Version Control System" target="_blank">Subversion</a> source code tree checked out, and for whatever reason you want to remove all Subversion directories inside that tree. (The thing that makes a Subversion tree a Subversion tree is the presence of a &#8220;.svn&#8221; folder in every folder of the tree. If you had a complicated source tree with lots of subdirectories, it would take you forever to remove each one.)</p>
<p>You can remove all &#8220;.svn&#8221; directories starting below &#8220;~/svn/exampleproject&#8221; &#8212; change this to suit your system &#8212; with the following command:</p>
<blockquote><p>find ~/svn/exampleproject -name &#8220;\.svn&#8221; -exec rm -rf  {} \;</p></blockquote>
<p>To make sure that the above command is going to do what  you want it to do, you may want to first generate a list of what it will delete (I highly recommend it).</p>
<blockquote><p>find ~/svn/exampleproject -name &#8220;\.svn&#8221; -exec echo &#8220;rm -rf  {}&#8221;  \;</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.sudosu.net/2007/how-to-remove-all-subversion-folders-from-a-directory-tree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

