<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
<channel>
    <title>freebsd.munk.me.uk - Shell</title>
    <link>http://freebsd.munk.me.uk/</link>
    <description>FreeBSD System Administration</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.5.2 - http://www.s9y.org/</generator>
    
    <image>
        <url>http://freebsd.munk.me.uk/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: freebsd.munk.me.uk - Shell - FreeBSD System Administration</title>
        <link>http://freebsd.munk.me.uk/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Command Line Web Browsing With WWW::Mechanize::Shell</title>
    <link>http://freebsd.munk.me.uk/archives/179-Command-Line-Web-Browsing-With-WWWMechanizeShell.html</link>
            <category>Perl</category>
            <category>Ports</category>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/179-Command-Line-Web-Browsing-With-WWWMechanizeShell.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=179</wfw:comment>

    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=179</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    &lt;span style=&quot;font-weight:bold&quot;&gt;Introduction&lt;/span&gt;&lt;br /&gt;
The perl module &lt;a href=&quot;http://search.cpan.org/~corion/WWW-Mechanize-Shell/&quot;  title=&quot;WWW::Mechanize::Shell On CPAN&quot;&gt;WWW::Mechanize::Shell&lt;/a&gt; is a brilliant tool for browsing websites at a very low level - think somewhere in between using telnet and using a command line based browser like lynx or links or w3m and you&#039;ll be close.  WWW::Mechanize::Shell is more than that though, it allows you to script a complete HTTP session so it can be replayed back at a later date without any interaction using WWW::Mechanize::Shell&#039;s parent perl module WWW::Mechanize - great for automatically submitting HTML forms/ POST data regularly via a cron job for example.&lt;br /&gt;
&lt;br /&gt;
In this article I&#039;ll be talking about installing WWW::Mechanize::Shell, look at a typical WWW::Mechanize::Shell browsing session and look at some examples of how I use WWW::Mechanize::Shell to make things easier.  Finally the article will end with a real world example - using mechshell to automate logging into FreshPorts and updating a watch list.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Installing WWW::Mechanize::Shell&lt;/span&gt;&lt;br /&gt;
As the name suggests, WWW::Mechanize::Shell is a perl module whose &#039;parent&#039; is the &lt;a href=&quot;http://search.cpan.org/~petdance/WWW-Mechanize/&quot;  title=&quot;WWW::Mechanize at CPAN&quot;&gt;WWW::Mechanize&lt;/a&gt; module written by Andy Lester (WWW::Mechanize::Shell itself is written by Max Maischein at time of original writing).  WWW::Mechanize does all the work in the background - WWW::Mechanize::Shell just makes it easy to interact in a HTTP session.  WWW::Mechanize::Shell and all it&#039;s dependencies can be installed from the ports tree:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;cd&amp;#160;/usr/ports/www/p5-WWW-Mechanize-Shell/&lt;br /&gt;
root@users&amp;#160;/home/munk/ports/www/p5-WWW-Mechanize-Shell#&amp;#160;make&amp;#160;install&lt;br /&gt;
root@users&amp;#160;/home/munk/ports/www/p5-WWW-Mechanize-Shell#&amp;#160;rehash&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Getting Started Using WWW::Mechanize::Shell&lt;/span&gt;&lt;br /&gt;
Once installed, start up the WWW::Mechanize::Shell using the following you can use the following commandline:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;perl&amp;#160;-MWWW&amp;#58;&amp;#58;Mechanize&amp;#58;&amp;#58;Shell&amp;#160;-eshell&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
To make things easier though I use a CSH shell alias which aliases &#039;mechshell&#039; to the command above:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;grep&amp;#160;mechshell&amp;#160;$cshrc&lt;br /&gt;
alias&amp;#160;mechshell&amp;#160;perl&amp;#160;-MWWW&amp;#58;&amp;#58;Mechanize&amp;#58;&amp;#58;Shell&amp;#160;-eshell&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Examples of WWW::Mechanize Usage&lt;/span&gt;&lt;br /&gt;
I usually use WWW::Mechanize when I want to manipulate data from websites that require a stateful HTTP session - ie a browsing session where there&#039;s more than one URL you have to visit to complete the &#039;session&#039;.  Usually these kind of stateful sessions involve logging into the website first, then browsing to another page to obtain the data and then I have the WWW::Mechanize perl script handle the data and return any results on the commandline.&lt;br /&gt;
&lt;br /&gt;
Some examples of scripts that I&#039;ve use WWW::Mechanize with:&lt;br /&gt;
&lt;br /&gt;
eclipse_flex_speed.pl&lt;br /&gt;
My ISP (Eclipse UK) used to allow you to &#039;flex&#039; your internet speed from 256k up to 2Mbps.  They ran an offer for a while where you could flex to the max for 3 months - unfortunately you could only flex for 12 hours at a time, which meant logging into the control panel every 12hrs, selecting the maximum speed and then submitting the form.  PITA basically.&lt;br /&gt;
&lt;br /&gt;
Instead I wrote eclipse_flex_speed.pl to automatically login to the Eclipse control panel, &#039;click&#039; the 2Mbps radio button and then submit the form so my speed got flexed automagically.  I then added the script as a cron job to autorun every 12hours, saving the haslle of doing it all manually!&lt;br /&gt;
&lt;br /&gt;
aod_get.pl&lt;br /&gt;
The BBC website allows you to listen to streams of all BBC radio broadcasts for up to a week after they&#039;ve been aired live.  The problem is that the web interface you listen to the stream on in your web browser only allows you to skip 5 or 15 minutes ahead in time and doesn&#039;t allow you to go to specific times in the stream.  To get around this you can obtain the URL of the real player stream and open it in a standalone real player - doing this you can go to any point in the stream easily.  Trouble is finding the URL of the stream isn&#039;t that easy and involves viewing the source HTML of the web UI and copy/pasting a partial URL.&lt;br /&gt;
&lt;br /&gt;
I started to write a WWW::Mechanize script to automate the &#039;screen scraping&#039; of all the available feeds from the BBC Audio On Demand site and listing them on one single HTML page linking the name of the feed to the real player feed URL.  As it goes though, someone else - Dave Cross - already had the same idea and wrote &lt;a href=&quot;http://blog.dave.org.uk/archives/000652.html&quot;&gt;a great script for scraping the BBC feeds&lt;/a&gt; automatically.  I now run this in a cronjob once a week.&lt;br /&gt;
&lt;br /&gt;
torrentflux_ctl.pl&lt;br /&gt;
This is a script for starting and stopping all torrents under the control of the torrentflux web based bittorrent client.  The script logs in as the torrent owner and then stops or starts all the torrents for that user - basically just does a GET of a URL that causes torrentflux to stop or start all torrents.  Crude but effective.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight:bold&quot;&gt;Real world example - Automating the update of FreshPorts watch list&lt;/span&gt;&lt;br /&gt;
Below is a real world example usage of WWW::Mechanize::Shell - automating the procedure of updating your watch list on &lt;a href=&quot;http://www.freshports.org/about.php&quot;  title=&quot;About FreshPorts&quot;&gt;Freshports.org&lt;/a&gt;.  I&#039;ve included comments as &#039;# this is a comment&#039; to help explain what each command is doing.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#&amp;#160;Start&amp;#160;up&amp;#160;mechshell&amp;#160;-&amp;#160;alias&amp;#160;for&amp;#160;&#039;perl&amp;#160;-MWWW&amp;#58;&amp;#58;Mechanize&amp;#58;&amp;#58;Shell&amp;#160;-eshell&#039;&amp;#58;&lt;br /&gt;
munk@users&amp;#160;/home/munk#&amp;#160;mechshell&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Request&amp;#160;the&amp;#160;URL&amp;#160;http&amp;#58;//www.freshports.org/login.php.&lt;br /&gt;
#&amp;#160;Note&amp;#160;the&amp;#160;HTTP&amp;#160;response&amp;#160;&#039;&amp;#40;200&amp;#41;&#039;&amp;#160;is&amp;#160;displayed&amp;#160;underneath&lt;br /&gt;
#&amp;#160;to&amp;#160;indicate&amp;#160;the&amp;#160;page&amp;#160;was&amp;#160;fetched&amp;#160;successfully&amp;#58;&lt;br /&gt;
&amp;#40;no&amp;#160;url&amp;#41;&amp;#62;get&amp;#160;http&amp;#58;//www.freshports.org/login.php&lt;br /&gt;
Retrieving&amp;#160;http&amp;#58;//www.freshports.org/login.php&amp;#40;200&amp;#41;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Use&amp;#160;the&amp;#160;mechshell&amp;#160;&#039;dump&#039;&amp;#160;command&amp;#160;to&amp;#160;dump&amp;#160;the&amp;#160;contents&lt;br /&gt;
#&amp;#160;of&amp;#160;all&amp;#160;forms&amp;#160;found&amp;#160;on&amp;#160;the&amp;#160;login&amp;#160;page&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/login.php&amp;#62;dump&lt;br /&gt;
POST&amp;#160;http&amp;#58;//www.freshports.org/login.php?origin=%2F&amp;#160;&amp;#91;l&amp;#93;&lt;br /&gt;
&amp;#160;&amp;#160;custom_settings=1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;hidden&amp;#160;readonly&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;LOGIN=1&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;hidden&amp;#160;readonly&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;UserID=&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;text&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;Password=&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;password&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;submit=Login&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;submit&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#60;NONAME&amp;#62;=&amp;#60;UNDEF&amp;#62;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;reset&amp;#41;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;There&#039;s&amp;#160;just&amp;#160;a&amp;#160;single&amp;#160;form&amp;#160;on&amp;#160;this&amp;#160;page&amp;#58;&lt;br /&gt;
#&amp;#160;-&amp;#160;the&amp;#160;form&#039;s&amp;#160;&#039;ACTION&#039;&amp;#160;is&amp;#160;set&amp;#160;to&amp;#160;submit&amp;#160;the&amp;#160;form&amp;#160;using&amp;#160;the&amp;#160;POST&amp;#160;method&lt;br /&gt;
#&amp;#160;&amp;#160;&amp;#160;to&amp;#160;the&amp;#160;url&amp;#160;http&amp;#58;//www.freshports.org/login.php?origin=%2F&lt;br /&gt;
#&amp;#160;The&amp;#160;form&amp;#160;contains&amp;#160;the&amp;#160;following&amp;#160;form&amp;#160;fields&amp;#58;&lt;br /&gt;
#&amp;#160;-&amp;#160;2&amp;#160;hidden&amp;#160;fields&lt;br /&gt;
#&amp;#160;-&amp;#160;1&amp;#160;text&amp;#160;field&amp;#160;called&amp;#160;&#039;UserID&#039;&lt;br /&gt;
#&amp;#160;-&amp;#160;1&amp;#160;password&amp;#160;field&amp;#160;called&amp;#160;&#039;Password&#039;&lt;br /&gt;
#&amp;#160;-&amp;#160;1&amp;#160;submit&amp;#160;field&amp;#160;called&amp;#160;&#039;Login&#039;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Fill&amp;#160;in&amp;#160;the&amp;#160;&#039;UserID&#039;&amp;#160;and&amp;#160;&#039;Password&#039;&amp;#160;fields&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/login.php&amp;#62;value&amp;#160;UserID&amp;#160;munk&lt;br /&gt;
http&amp;#58;//www.freshports.org/login.php&amp;#62;value&amp;#160;Password&amp;#160;xxxxxx&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;And&amp;#160;then&amp;#160;submit&amp;#160;the&amp;#160;form.&amp;#160;&amp;#160;Note&amp;#160;we&amp;#160;can&amp;#160;just&amp;#160;use&amp;#160;the&amp;#160;mechshell&amp;#160;&#039;submit&#039;&lt;br /&gt;
#&amp;#160;command&amp;#160;here&amp;#160;because&amp;#160;there&amp;#160;is&amp;#160;only&amp;#160;a&amp;#160;single&amp;#160;form&amp;#160;on&amp;#160;the&amp;#160;page.&amp;#160;&amp;#160;If&amp;#160;there&amp;#160;were&lt;br /&gt;
#&amp;#160;more&amp;#160;than&amp;#160;one&amp;#160;form&amp;#160;on&amp;#160;the&amp;#160;page&amp;#160;we&amp;#160;would&amp;#160;need&amp;#160;to&amp;#160;specify&amp;#160;which&amp;#160;button&amp;#160;exactly&amp;#160;to&lt;br /&gt;
#&amp;#160;click&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/login.php&amp;#62;submit&lt;br /&gt;
200&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Again&amp;#160;note&amp;#160;that&amp;#160;the&amp;#160;&#039;200&#039;&amp;#160;response&amp;#160;indicates&amp;#160;the&amp;#160;request&amp;#160;was&amp;#160;successful.&lt;br /&gt;
#&amp;#160;Also&amp;#160;note&amp;#160;that&amp;#160;the&amp;#160;next&amp;#160;mechshell&amp;#160;prompt&amp;#160;below&amp;#160;has&amp;#160;changed&amp;#160;from&lt;br /&gt;
#&amp;#160;&#039;http&amp;#58;//www.freshports.org/login.php&amp;#62;&#039;&amp;#160;to&amp;#160;just&amp;#160;&#039;http&amp;#58;//www.freshports.org/&#039;&amp;#160;-&lt;br /&gt;
#&amp;#160;this&amp;#160;indicates&amp;#160;that&amp;#160;the&amp;#160;login&amp;#160;script&amp;#160;has&amp;#160;probably&amp;#160;redirected&amp;#160;us&amp;#160;to&amp;#160;the&lt;br /&gt;
#&amp;#160;freshports&amp;#160;home&amp;#160;page.&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Now&amp;#160;we&amp;#160;take&amp;#160;a&amp;#160;look&amp;#160;to&amp;#160;check&amp;#160;that&amp;#160;the&amp;#160;login&amp;#160;succeeded&amp;#160;ok.&amp;#160;&amp;#160;To&amp;#160;do&amp;#160;this&amp;#160;we&amp;#160;use&lt;br /&gt;
#&amp;#160;the&amp;#160;mechshell&amp;#160;&#039;content&#039;&amp;#160;command&amp;#160;which&amp;#160;effectively&amp;#160;dumps&amp;#160;the&amp;#160;content&amp;#160;of&amp;#160;the&lt;br /&gt;
#&amp;#160;returned&amp;#160;page&amp;#160;back&amp;#160;at&amp;#160;us&amp;#160;in&amp;#160;a&amp;#160;pager.&lt;br /&gt;
#&amp;#160;What&amp;#160;we&#039;re&amp;#160;looking&amp;#160;for&amp;#160;is&amp;#160;the&amp;#160;text&amp;#160;&#039;Logged&amp;#160;in&amp;#160;as&amp;#160;munk&#039;&amp;#160;which&amp;#160;will&amp;#160;indicate&amp;#160;we&lt;br /&gt;
#&amp;#160;logged&amp;#160;in&amp;#160;ok&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/&amp;#62;content&lt;br /&gt;
&amp;#60;!DOCTYPE&amp;#160;HTML&amp;#160;PUBLIC&amp;#160;&quot;-//W3C//DTD&amp;#160;HTML&amp;#160;4.01&amp;#160;Transitional//EN&quot;&amp;#62;&lt;br /&gt;
&amp;#60;HTML&amp;#62;&lt;br /&gt;
&amp;#60;HEAD&amp;#62;&lt;br /&gt;
-snip-&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#60;td&amp;#160;NOWRAP&amp;#62;&amp;#60;FONT&amp;#160;SIZE=&quot;-1&quot;&amp;#62;Logged&amp;#160;in&amp;#160;as&amp;#160;munk&amp;#60;/FONT&amp;#62;&amp;#60;br&amp;#62;&amp;#60;FONT&amp;#160;SIZE=&quot;-1&quot;&amp;#62;&amp;#60;a&amp;#160;href=&quot;http://freebsd.munk.me.uk/customize.php?origin=%2F&quot;&amp;#160;title=&quot;Customize&amp;#160;your&amp;#160;settings&quot;&amp;#62;Customize&amp;#60;/a&lt;br /&gt;
&amp;#62;&amp;#60;/FONT&amp;#62;&amp;#60;br&amp;#62;&amp;#60;FONT&amp;#160;SIZE=&quot;-1&quot;&amp;#62;&amp;#60;a&amp;#160;href=&quot;http://freebsd.munk.me.uk/logout.php&quot;&amp;#160;title=&quot;Logout&amp;#160;of&amp;#160;the&amp;#160;website&quot;&amp;#62;Logout&amp;#60;/a&amp;#62;&amp;#60;/FONT&amp;#62;&amp;#60;br&amp;#62;&amp;#60;FONT&amp;#160;SIZE=&quot;-1&quot;&amp;#62;&amp;#60;a&amp;#160;href=&quot;/my-flagged-commits.php&quot;&amp;#160;title=&quot;Li&lt;br /&gt;
st&amp;#160;of&amp;#160;commits&amp;#160;you&amp;#160;have&amp;#160;flagged&quot;&amp;#62;My&amp;#160;Flagged&amp;#160;Commits&amp;#60;/a&amp;#62;&amp;#60;/FONT&amp;#62;&amp;#60;br&amp;#62;&lt;br /&gt;
-snip-&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Now&amp;#160;we&#039;re&amp;#160;logged&amp;#160;in&amp;#160;ok&amp;#160;we&amp;#160;can&amp;#160;continue&amp;#160;to&amp;#160;upload&amp;#160;the&amp;#160;mypkg_info.txt&amp;#160;file&amp;#160;we&lt;br /&gt;
#&amp;#160;created&amp;#160;earlier.&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;First&amp;#160;browse&amp;#160;to&amp;#160;the&amp;#160;pkg_upload.php&amp;#160;page&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/&amp;#62;get&amp;#160;http&amp;#58;//www.freshports.org/pkg_upload.php&lt;br /&gt;
Retrieving&amp;#160;http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#40;200&amp;#41;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Now&amp;#160;use&amp;#160;&#039;dump&#039;&amp;#160;to&amp;#160;see&amp;#160;a&amp;#160;list&amp;#160;of&amp;#160;form&amp;#160;fields&amp;#160;on&amp;#160;this&amp;#160;page.&lt;br /&gt;
#&amp;#160;Note&amp;#160;that&amp;#160;there&amp;#160;are&amp;#160;2&amp;#160;submit&amp;#160;buttons&amp;#160;on&amp;#160;this&amp;#160;page&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#62;dump&lt;br /&gt;
POST&amp;#160;http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#160;&amp;#40;multipart/form-data&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;pkg_info=&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;file&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;staging=Staging&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;submit&amp;#41;&lt;br /&gt;
&amp;#160;&amp;#160;wlid=5393&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;option&amp;#41;&amp;#160;&amp;#160;&amp;#160;&amp;#91;&amp;#42;5393/main&amp;#42;&amp;#93;&lt;br /&gt;
&amp;#160;&amp;#160;replaceappend=replace&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;radio&amp;#41;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#91;&amp;#42;replace/Replace&amp;#160;list&amp;#160;contents|append/Append&amp;#160;to&amp;#160;list&amp;#160;&amp;#40;duplicates&amp;#160;will&amp;#160;be&amp;#160;removed&amp;#41;&amp;#93;&lt;br /&gt;
&amp;#160;&amp;#160;upload=Upload&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#40;submit&amp;#41;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;We&amp;#160;need&amp;#160;to&amp;#160;fill&amp;#160;out&amp;#160;the&amp;#160;form&amp;#160;here.&amp;#160;&amp;#160;Uploading&amp;#160;files&amp;#160;with&amp;#160;mechshell&amp;#160;is&amp;#160;as&lt;br /&gt;
#&amp;#160;simple&amp;#160;as&amp;#160;completing&amp;#160;the&amp;#160;correct&amp;#160;file&amp;#160;type&amp;#160;field&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#62;value&amp;#160;pkg_info&amp;#160;/tmp/mypkg_info.txt&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Ok,&amp;#160;now&amp;#160;we&#039;re&amp;#160;ready&amp;#160;to&amp;#160;submit&amp;#160;the&amp;#160;form.&lt;br /&gt;
#&amp;#160;Note&amp;#160;that&amp;#160;because&amp;#160;there&amp;#160;are&amp;#160;2&amp;#160;submit&amp;#160;buttons&amp;#160;on&amp;#160;this&amp;#160;form,&amp;#160;we&amp;#160;must&amp;#160;explicitly&lt;br /&gt;
#&amp;#160;tell&amp;#160;mechshell&amp;#160;which&amp;#160;button&amp;#160;it&amp;#160;is&amp;#160;that&amp;#160;we&amp;#160;want&amp;#160;to&amp;#160;click&amp;#160;on&amp;#160;-&amp;#160;to&amp;#160;do&amp;#160;that&amp;#160;we&amp;#160;use&lt;br /&gt;
#&amp;#160;the&amp;#160;&#039;click&#039;&amp;#160;command.&amp;#160;&amp;#160;Just&amp;#160;using&amp;#160;&#039;submit&#039;&amp;#160;here&amp;#160;would&amp;#160;possibly&amp;#160;click&amp;#160;on&amp;#160;the&lt;br /&gt;
#&amp;#160;&#039;staging&#039;&amp;#160;button&amp;#160;which&amp;#160;is&amp;#160;not&amp;#160;what&amp;#160;we&amp;#160;want&amp;#160;-&amp;#160;instead&amp;#160;we&amp;#160;use&amp;#160;the&amp;#160;command&lt;br /&gt;
#&amp;#160;&#039;click&amp;#160;upload&#039;&amp;#160;to&amp;#160;indicate&amp;#160;we&amp;#160;want&amp;#160;to&amp;#160;click&amp;#160;on&amp;#160;the&amp;#160;&#039;upload&#039;&amp;#160;button&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#62;click&amp;#160;upload&lt;br /&gt;
&amp;#40;200&amp;#41;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Success!&amp;#160;&amp;#160;It&#039;s&amp;#160;a&amp;#160;good&amp;#160;idea&amp;#160;now&amp;#160;to&amp;#160;just&amp;#160;check&amp;#160;that&amp;#160;this&amp;#160;worked&amp;#160;by&amp;#160;browsing&amp;#160;in&lt;br /&gt;
#&amp;#160;a&amp;#160;web&amp;#160;browser&amp;#160;to&amp;#160;your&amp;#160;watch&amp;#160;list&amp;#160;and&amp;#160;checking&amp;#160;the&amp;#160;new&amp;#160;items&amp;#160;were&amp;#160;updated&amp;#160;ok&amp;#160;&amp;#40;of&lt;br /&gt;
#&amp;#160;course&amp;#160;you&amp;#160;can&amp;#160;do&amp;#160;this&amp;#160;in&amp;#160;mechshell&amp;#160;if&amp;#160;you&amp;#160;want&amp;#160;but&amp;#160;I&#039;ll&amp;#160;leave&amp;#160;that&amp;#160;out&amp;#160;here!&amp;#41;.&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Finally,&amp;#160;the&amp;#160;really&amp;#160;cool&amp;#160;bit.&amp;#160;&amp;#160;The&amp;#160;mechshell&amp;#160;&#039;script&#039;&amp;#160;command&amp;#160;will&amp;#160;dump&amp;#160;out&lt;br /&gt;
#&amp;#160;the&amp;#160;perl&amp;#160;code&amp;#160;required&amp;#160;to&amp;#160;perform&amp;#160;all&amp;#160;of&amp;#160;the&amp;#160;above&amp;#160;actions&amp;#160;again&amp;#160;if&amp;#160;you&amp;#160;copy&lt;br /&gt;
#&amp;#160;them&amp;#160;into&amp;#160;a&amp;#160;perl&amp;#160;script&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#62;script&lt;br /&gt;
#!perl&amp;#160;-w&lt;br /&gt;
use&amp;#160;strict;&lt;br /&gt;
use&amp;#160;WWW&amp;#58;&amp;#58;Mechanize;&lt;br /&gt;
use&amp;#160;WWW&amp;#58;&amp;#58;Mechanize&amp;#58;&amp;#58;FormFiller;&lt;br /&gt;
use&amp;#160;URI&amp;#58;&amp;#58;URL;&lt;br /&gt;
-snip-&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Also,&amp;#160;if&amp;#160;you&amp;#160;provide&amp;#160;a&amp;#160;filename&amp;#160;as&amp;#160;an&amp;#160;argument&amp;#160;to&amp;#160;the&amp;#160;&#039;script&#039;&amp;#160;command,&lt;br /&gt;
#&amp;#160;mechshell&amp;#160;will&amp;#160;dump&amp;#160;all&amp;#160;the&amp;#160;script&amp;#160;commands&amp;#160;to&amp;#160;that&amp;#160;filename&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#62;script&amp;#160;/tmp/freshports_update.pl&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Finally,&amp;#160;use&amp;#160;&#039;quit&#039;&amp;#160;to&amp;#160;exit&amp;#160;the&amp;#160;mechshell&amp;#58;&lt;br /&gt;
http&amp;#58;//www.freshports.org/pkg_upload.php&amp;#62;quit&lt;br /&gt;
munk@users&amp;#160;/home/munk#&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Now all that remains is to open up /tmp/freshports_update.pl and tidy the script up so that it&#039;s more suitable for automated use via cron.  For example, any &#039;dump&#039; and &#039;content&#039; commands can be taken out - these would only cause problems anyway if run from a non-interactive shell as used by cron.&lt;br /&gt;
&lt;br /&gt;
We also need to add some code to have the script dump the contents of &#039;pkg_info -qoa&#039; to a temporary file prior to uploading.&lt;br /&gt;
&lt;br /&gt;
The completed &#039;quick and dirty&#039; hack looks like this then:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#!/usr/bin/perl&amp;#160;-w&lt;br /&gt;
use&amp;#160;strict;&lt;br /&gt;
use&amp;#160;WWW&amp;#58;&amp;#58;Mechanize;&lt;br /&gt;
use&amp;#160;WWW&amp;#58;&amp;#58;Mechanize&amp;#58;&amp;#58;FormFiller;&lt;br /&gt;
use&amp;#160;URI&amp;#58;&amp;#58;URL;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;FreshPorts&amp;#160;username/pass&amp;#58;&lt;br /&gt;
my&amp;#160;$user=&quot;munk&quot;;&lt;br /&gt;
my&amp;#160;$pass=&quot;xxxxx&quot;;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Temp&amp;#160;location&amp;#160;to&amp;#160;store&amp;#160;output&amp;#160;from&amp;#160;&#039;pkg_info&amp;#160;-qoa&#039;&amp;#58;&lt;br /&gt;
my&amp;#160;$mypkg_info=&quot;/tmp/freshports/mypkg_info.txt&quot;;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;prepare&amp;#160;file&amp;#160;containing&amp;#160;output&amp;#160;from&amp;#58;&amp;#160;pkg_info&amp;#160;-qoa&lt;br /&gt;
`pkg_info&amp;#160;-qoa&amp;#160;&amp;#62;&amp;#160;$mypkg_info`;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Prepare&amp;#160;WWW&amp;#58;&amp;#58;Mechanize&amp;#58;&lt;br /&gt;
my&amp;#160;$agent&amp;#160;=&amp;#160;WWW&amp;#58;&amp;#58;Mechanize-&amp;#62;new&amp;#40;&amp;#160;autocheck&amp;#160;=&amp;#62;&amp;#160;1&amp;#160;&amp;#41;;&lt;br /&gt;
my&amp;#160;$formfiller&amp;#160;=&amp;#160;WWW&amp;#58;&amp;#58;Mechanize&amp;#58;&amp;#58;FormFiller-&amp;#62;new&amp;#40;&amp;#41;;&lt;br /&gt;
$agent-&amp;#62;env_proxy&amp;#40;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Login&amp;#160;to&amp;#160;FreshPorts&amp;#58;&lt;br /&gt;
$agent-&amp;#62;get&amp;#40;&#039;http&amp;#58;//www.freshports.org/login.php&#039;&amp;#41;;&lt;br /&gt;
$agent-&amp;#62;form_number&amp;#40;1&amp;#41;&amp;#160;if&amp;#160;$agent-&amp;#62;forms&amp;#160;and&amp;#160;scalar&amp;#160;@{$agent-&amp;#62;forms};&lt;br /&gt;
{&amp;#160;local&amp;#160;$^W;&amp;#160;$agent-&amp;#62;current_form-&amp;#62;value&amp;#40;&#039;UserID&#039;,&amp;#160;$user&amp;#41;;&amp;#160;};&lt;br /&gt;
{&amp;#160;local&amp;#160;$^W;&amp;#160;$agent-&amp;#62;current_form-&amp;#62;value&amp;#40;&#039;Password&#039;,&amp;#160;$pass&amp;#41;;&amp;#160;};&lt;br /&gt;
$agent-&amp;#62;submit&amp;#40;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Submit&amp;#160;pkg_info&amp;#160;details&amp;#160;to&amp;#160;FreshPorts&amp;#160;pkg_upload&amp;#160;page&amp;#58;&lt;br /&gt;
$agent-&amp;#62;get&amp;#40;&#039;http&amp;#58;//www.freshports.org/pkg_upload.php&#039;&amp;#41;;&lt;br /&gt;
$agent-&amp;#62;form_number&amp;#40;1&amp;#41;&amp;#160;if&amp;#160;$agent-&amp;#62;forms&amp;#160;and&amp;#160;scalar&amp;#160;@{$agent-&amp;#62;forms};&lt;br /&gt;
{&amp;#160;local&amp;#160;$^W;&amp;#160;$agent-&amp;#62;current_form-&amp;#62;value&amp;#40;&#039;pkg_info&#039;,&amp;#160;$mypkg_info&amp;#41;;&amp;#160;};&lt;br /&gt;
$agent-&amp;#62;click&amp;#40;&#039;upload&#039;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;Remove&amp;#160;temporary&amp;#160;file&amp;#58;&lt;br /&gt;
`rm&amp;#160;$mypkg_info`;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
After saving the script and making the file executable, an entry can then be added to cron to have the script auto update the list of ports at freshports once a week - or however often you require it to be updated, once a week is more than enough for me.  Sorted! :) 
    </content:encoded>

    <pubDate>Wed, 15 Aug 2007 12:46:00 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/179-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Let root see all files with locate</title>
    <link>http://freebsd.munk.me.uk/archives/207-Let-root-see-all-files-with-locate.html</link>
            <category>FreeBSD</category>
            <category>Shell</category>
            <category>SSH</category>
    
    <comments>http://freebsd.munk.me.uk/archives/207-Let-root-see-all-files-with-locate.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=207</wfw:comment>

    <slash:comments>6</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=207</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    The &lt;a href=&quot;http://nixdoc.net/man-pages/FreeBSD/locate.1.html&quot;  title=&quot;locate manpage&quot;&gt;locate&lt;/a&gt; utility on linux was one of the first tools I hit when I made the move to FreeBSD a few years back - knowing where files are is half the battle when you&#039;re trying to configure things and find documentation on how to do it. The trouble with locate though as &lt;a href=&quot;http://freebsd.amazingdev.com/blog/archives/000805.html&quot;  title=&quot;Locate This!&quot;&gt;jdarnold mentions in his article &#039;Locate This!&#039;&lt;/a&gt; is that if you build the locate database as &#039;root&#039;, you end up exposing everything to any user that runs the locate command.  The other problem he mentions is the locate db is only updated weekly on FreeBSD by default via the &lt;a href=&quot;http://www.freebsd.org/cgi/man.cgi?query=periodic&amp;apropos=0&amp;sektion=0&amp;manpath=FreeBSD+6.1-RELEASE&amp;format=html&quot;  title=&quot;periodic manpage&quot;&gt;periodic&lt;/a&gt; system which isn&#039;t really enough if you use your system regularly.&lt;br /&gt;
&lt;br /&gt;
I remember thinking along the same lines a while back and after reading through the man pages the solution I found was to create two separate databases - one for root and one for regular users.  The &#039;regular&#039; db is updated on a weekly basis as per the default on FreeBSD via periodic, whereas the other &#039;root&#039; locate db is built daily in a crontab so I can get the latest up to date details on which files are where.&lt;br /&gt;
&lt;br /&gt;
To get the root db built first you need to create a crontab entry - i put this in /etc/crontab:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;39&amp;#160;&amp;#160;2&amp;#160;&amp;#160;&amp;#160;&amp;#42;&amp;#160;&amp;#160;&amp;#160;&amp;#42;&amp;#160;&amp;#160;&amp;#160;&amp;#42;&amp;#160;&amp;#160;&amp;#160;root&amp;#160;&amp;#160;&amp;#160;&amp;#160;env&amp;#160;-i&amp;#160;LOCATE_CONFIG=/root/locate/conf/locate.rc&amp;#160;/usr/libexec/locate.updatedb&amp;#160;&amp;#62;&amp;#160;/dev/null&amp;#160;2&amp;#62;&amp;amp;1&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This tells the locate.updatedb script to use a separate configuration file - /root/locate/conf/locate.rc - for building root&#039;s locate db.  The content of /root/locate/conf/locate.rc look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;FCODES=&quot;/root/locate/db/locate.database.root&quot;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
which indicates that this db should be built in /root/locate/db/locate.database.root instead of the default locate in /var/db/locate.database.  You can safely run the command as root on the commandline to initialize your new db:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;env&amp;#160;-i&amp;#160;LOCATE_CONFIG=/root/locate/conf/locate.rc&amp;#160;/usr/libexec/locate.updatedb&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Once the database is built you can move on to test the new db works ok:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;locate&amp;#160;-d&amp;#160;/root/locate/db/locate.database.root&amp;#160;.cshrc.root&lt;br /&gt;
/root/.cshrc.root&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This file is only readable by root, so it seems to work ok.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To make things easier, add a shell alias in root&#039;s .cshrc file aliasing &#039;locate&#039; to the command &#039;locate -d /root/locate/db/locate.database.root&#039;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;grep&amp;#160;locate&amp;#160;$cshrc&lt;br /&gt;
alias&amp;#160;locate&amp;#160;locate&amp;#160;-d&amp;#160;/root/locate/db/locate.database.root&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
With the &quot;-d /root/locate/db/locate.database.root&quot; switch, locate will use the db at /root/locate/db/locate.database.root instead of the default /var/db/locate.database and root will be able to use locate to find any files in the filesystem, not just those that are world readable.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Finally, one way to update the regular locate db as root but without making it list every world readable file is to perform the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#!/bin/sh&lt;br /&gt;
#&amp;#160;make&amp;#160;sure&amp;#160;db&amp;#160;file&amp;#160;exists&amp;#58;&lt;br /&gt;
touch&amp;#160;/var/db/locate.database&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;then&amp;#160;change&amp;#160;ownership&amp;#160;to&amp;#160;the&amp;#160;nobody&amp;#160;user&amp;#58;&lt;br /&gt;
chown&amp;#160;nobody&amp;#160;/var/db/locate.database&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;make&amp;#160;it&amp;#160;writeable&amp;#160;by&amp;#160;nobody&amp;#160;and&amp;#160;readable&amp;#160;by&amp;#160;everyone&amp;#160;else&amp;#58;&lt;br /&gt;
chmod&amp;#160;644&amp;#160;/var/db/locate.database&amp;#160;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;then&amp;#160;move&amp;#160;on&amp;#160;to&amp;#160;update&amp;#160;the&amp;#160;db...&lt;br /&gt;
#&amp;#160;first&amp;#160;make&amp;#160;sure&amp;#160;we&#039;re&amp;#160;in&amp;#160;the&amp;#160;/&amp;#160;folder&amp;#160;where&amp;#160;the&amp;#160;db&amp;#160;update&amp;#160;starts&amp;#58;&lt;br /&gt;
cd&amp;#160;/&amp;#160;&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;then&amp;#160;finally&amp;#160;run&amp;#160;the&amp;#160;updatedb&amp;#160;command&amp;#160;as&amp;#160;the&amp;#160;&#039;nobody&#039;&amp;#160;user&amp;#58;&lt;br /&gt;
echo&amp;#160;&quot;/usr/libexec/locate.updatedb&quot;&amp;#160;|&amp;#160;su&amp;#160;-&amp;#160;-fm&amp;#160;nobody&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This is basically what the 310.locate periodic script does and results in a locate db that contains only files that are readable by the &#039;nobody&#039; user - essentially all &#039;world readable&#039; files.&lt;br /&gt;
&lt;br /&gt;
Comparing the sizes of the root db against the nobody db:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/#&amp;#160;ls&amp;#160;-al&amp;#160;/var/db/locate.database&amp;#160;/root/locate/db/locate.database.root&lt;br /&gt;
-rw-r--r--&amp;#160;&amp;#160;1&amp;#160;root&amp;#160;&amp;#160;&amp;#160;&amp;#160;wheel&amp;#160;&amp;#160;4070484&amp;#160;Nov&amp;#160;18&amp;#160;02&amp;#58;45&amp;#160;/root/locate/db/locate.database.root&lt;br /&gt;
-rw-r--r--&amp;#160;&amp;#160;1&amp;#160;nobody&amp;#160;&amp;#160;wheel&amp;#160;&amp;#160;3280409&amp;#160;Nov&amp;#160;18&amp;#160;11&amp;#58;41&amp;#160;/var/db/locate.database&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
You can see the size difference there, not as many entries in nobody&#039;s db as root&#039;s.  Just to double check:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;root@users&amp;#160;/root#&amp;#160;locate&amp;#160;.cshrc.root&lt;br /&gt;
/root/bin/ktrace.out&lt;br /&gt;
/root/ktrace.out&lt;br /&gt;
/usr/local/etc/snort/ktrace.out&lt;br /&gt;
root@users&amp;#160;/root#&amp;#160;echo&amp;#160;&quot;locate&amp;#160;ktrace.out&quot;&amp;#160;|&amp;#160;su&amp;#160;-&amp;#160;-fm&amp;#160;nobody&lt;br /&gt;
/usr/local/etc/snort/ktrace.out&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
So from that you can see that &#039;nobody&#039; can see the ktrace.out files located in /root - apart from root of course :)  Sorted. 
    </content:encoded>

    <pubDate>Sat, 18 Nov 2006 10:38:40 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/207-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Expand shell globs using 'ctrl-z'</title>
    <link>http://freebsd.munk.me.uk/archives/205-Expand-shell-globs-using-ctrl-z.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/205-Expand-shell-globs-using-ctrl-z.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=205</wfw:comment>

    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=205</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Just noticed a semi useful feature of the CSH shell (shells in general? not tested it) whilst running &#039;rm -rf *&#039; in a directory.  Got a bit paranoid I was doing something silly (running the command as root), so hit &#039;ctrl-z&#039; to put the process into the background and the &#039;*&#039; part was expanded in the job control list:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;11&amp;#58;21&amp;#58;24&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/torrentflux.munk.me.uk#&amp;#160;rm&amp;#160;-rf&amp;#160;&amp;#42;&lt;br /&gt;
^Z&lt;br /&gt;
&amp;#91;1&amp;#93;&amp;#160;&amp;#160;+&amp;#160;Suspended&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;rm&amp;#160;-rf&amp;#160;TF_BitTornado&amp;#160;adodb&amp;#160;downloads&amp;#160;images&amp;#160;language&amp;#160;mods&amp;#160;searchEngines&amp;#160;themes&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Yay.  Useful tip #341 you&#039;ll probably never use but at some point in the future think mmm... now where did I read about that thing about this thing... 
    </content:encoded>

    <pubDate>Sun, 05 Nov 2006 11:22:58 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/205-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>CSH Tips: Auto 'whereis' on the tcsh command line</title>
    <link>http://freebsd.munk.me.uk/archives/202-CSH-Tips-Auto-whereis-on-the-tcsh-command-line.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/202-CSH-Tips-Auto-whereis-on-the-tcsh-command-line.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=202</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=202</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Just read &lt;a href=&quot;http://freebsd.amazingdev.com/blog/archives/000776.html&quot;  title=&quot;whereis &quot;&gt;this entry&lt;/a&gt; about the &#039;whereis&#039; command on Unix and it reminded me of another great shell tip for tcsh users (csh/tcsh on FreeBSD since they&#039;re the same thing!) - the shell can &#039;normalize&#039; any command on the command line if you bind the normalize function to a keystroke - this allows you to easily see how a command would expand after it&#039;s executed.  Somewhat esoteric without an example, but I use it so much I thought I&#039;d post about it.&lt;br /&gt;
&lt;br /&gt;
First off set a key binding for the normalize command - in the shell type in:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;bindkey&amp;#160;&quot;^W&quot;&amp;#160;normalize-command&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
or add it to ~/.cshrc to make it permanent.  Obviously you can set it to whatever keybinding you want, I use ctrl-w.&lt;br /&gt;
&lt;br /&gt;
Now type in any command that you&#039;d use on the commandline and then whilst the cursor is at the end of the command, hit the keystroke you entered for the normalize command - in our case above, ctrl-w.&lt;br /&gt;
&lt;br /&gt;
The command you entered should automatically get expanded to the absolute path of the command.  For example if I type in:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;ls&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
and then hit &#039;ctrl-w&#039; whilst the cursor is just after the &#039;s&#039;, the result will look like:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;/bin/ls&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Magic!  &lt;br /&gt;
&lt;br /&gt;
Like I say I use this function quite a lot on the commandline, particularly when I want to see how a command I enter will get expanded - the normalize function works on aliases as well as just plain commands, so it&#039;s quicker to type &#039;portupgrade^w&#039; than to type in &#039;alias portupgrade&#039; to see how I&#039;ve got my portupgrade alias set up.   It&#039;s also great for quickly editing system executables from the commandline without having to remember where the file/script is or cut/paste the results from &#039;whereis&#039; or &#039;locate&#039; etc.&lt;br /&gt;
 
    </content:encoded>

    <pubDate>Wed, 18 Oct 2006 00:10:26 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/202-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Shell Tip: Review your most commonly used command lines</title>
    <link>http://freebsd.munk.me.uk/archives/201-Shell-Tip-Review-your-most-commonly-used-command-lines.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/201-Shell-Tip-Review-your-most-commonly-used-command-lines.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=201</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=201</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Just got through reading an interesting article on &lt;a href=&quot;http://www.lifehacker.com/software/unix/review-your-most-oftused-unix-commands-202712.php&quot;  title=&quot;review commonly used unix commandlines&quot;&gt;how to review your most commonly used Unix commands&lt;/a&gt;.  The idea is to sort the most commonly used commands numerically with a view to maybe shortening the most command ones using aliases, similar in a way to the time saving article mentioned &lt;a href=&quot;http://freebsd.munk.me.uk/archives/177-CSH-Tips-Create-Variables-Everywhere.html&quot;  title=&quot;Shell Shortcuts&quot;&gt;here&lt;/a&gt; a while ago.&lt;br /&gt;
&lt;br /&gt;
(The lifehacker article is actually just picking up on the original article on IBM&#039;s site entitled &lt;a href=&quot;http://www-128.ibm.com/developerworks/aix/library/au-productivitytips.html?ca=dgr-lnxw07UNIX-Office-Tips&quot;  title=&quot;Unix productivity tips&quot;&gt;Unix productivity tips&lt;/a&gt; and is a good read for anyone wanting to improve their efficiency on the shell command line.)&lt;br /&gt;
&lt;br /&gt;
Note for tcsh on FreeBSD, the command you probably want to use is this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;history&amp;#160;|&amp;#160;tail&amp;#160;-1000&amp;#160;|&amp;#160;awk&amp;#160;&#039;{print&amp;#160;$3}&#039;&amp;#160;|&amp;#160;sort&amp;#160;|&amp;#160;uniq&amp;#160;-c&amp;#160;|&amp;#160;sort&amp;#160;-r&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
which outputs this kind of list for my shell history (listing top 10 used commands using &#039;| head -10&#039;):&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;;&amp;#160;history&amp;#160;|&amp;#160;tail&amp;#160;-1000&amp;#160;|&amp;#160;awk&amp;#160;&#039;{print&amp;#160;$3}&#039;&amp;#160;|&amp;#160;sort&amp;#160;|&amp;#160;uniq&amp;#160;-c&amp;#160;|&amp;#160;sort&amp;#160;-r&amp;#160;|&amp;#160;head&amp;#160;-10&lt;br /&gt;
&amp;#160;212&amp;#160;sc&lt;br /&gt;
&amp;#160;158&amp;#160;m&lt;br /&gt;
&amp;#160;&amp;#160;96&amp;#160;fg&lt;br /&gt;
&amp;#160;&amp;#160;75&amp;#160;s&lt;br /&gt;
&amp;#160;&amp;#160;68&amp;#160;cd&lt;br /&gt;
&amp;#160;&amp;#160;56&amp;#160;vi&lt;br /&gt;
&amp;#160;&amp;#160;37&amp;#160;ls&lt;br /&gt;
&amp;#160;&amp;#160;26&amp;#160;grep&lt;br /&gt;
&amp;#160;&amp;#160;19&amp;#160;man&lt;br /&gt;
&amp;#160;&amp;#160;18&amp;#160;alias&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
I&#039;m pretty happy with that, most of the commands are either 1 or 2 character aliases at least :) 
    </content:encoded>

    <pubDate>Tue, 10 Oct 2006 00:22:47 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/201-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Executing webserver commands using su on FreeBSD</title>
    <link>http://freebsd.munk.me.uk/archives/200-Executing-webserver-commands-using-su-on-FreeBSD.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/200-Executing-webserver-commands-using-su-on-FreeBSD.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=200</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=200</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    On FreeBSD the webserver user - www - doesn&#039;t have a shell enabled, so testing any PHP/CGI commands that are executed using a &#039;system&#039; call can&#039;t easily be tested on the commandline.&lt;br /&gt;
&lt;br /&gt;
For example say you wanted to test the result of this PHP call in a shell:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;exec&amp;#40;&#039;echo&amp;#160;&quot;hello&amp;#160;world&quot;&#039;&amp;#41;;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
If the web user www had a shell - like it does on many linux distros (a bad thing (tm), compromise is easier if you can spawn a shell under the user the webserver runs as) - you could just do:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;su&amp;#160;www&lt;br /&gt;
echo&amp;#160;&quot;hello&amp;#160;world&quot;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
and see the results fine.  Since www has no shell though on FreeBSD, you can&#039;t just &#039;su&#039; to www.&lt;br /&gt;
&lt;br /&gt;
Instead you can do the following:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;echo&amp;#160;&#039;echo&amp;#160;&quot;hello&amp;#160;world&quot;&#039;&amp;#160;|&amp;#160;su&amp;#160;-&amp;#160;-f&amp;#160;-m&amp;#160;www&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
This runs the command as the &#039;www&#039; user, but doesn&#039;t try to invoke the &#039;www&#039; user&#039;s .cshrc file or modify the environment (which is something to be aware of since it&#039;s your shell environment you&#039;re using, probably root, make sure there&#039;s nothing there that could affect the outcome of the result).&lt;br /&gt;
&lt;br /&gt;
This entry is a memo to myself really, I always forget the &#039;su - -fm user&#039; thing!  Maybe there&#039;s a reason not to use this as well, feel free to comment. 
    </content:encoded>

    <pubDate>Sat, 07 Oct 2006 20:04:03 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/200-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Shell Tips: Generate random numbers on the shell commandline</title>
    <link>http://freebsd.munk.me.uk/archives/198-Shell-Tips-Generate-random-numbers-on-the-shell-commandline.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/198-Shell-Tips-Generate-random-numbers-on-the-shell-commandline.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=198</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=198</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Whilst reading through &lt;a href=&quot;http://www.bsdstats.org/downloads/300.statistics&quot;  title=&quot;bsdstats utility&quot;&gt;the shell script for the bsdstats utility&lt;/a&gt; I noticed a nice way of generating a random number on the command line:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;jot&amp;#160;-r&amp;#160;1&amp;#160;0&amp;#160;900&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Which generates a random integer between  0 and 900.  &lt;br /&gt;
&lt;br /&gt;
Useful to remember, have resorted to some wierd and wonderful ways of generating randomness before now (usually involving consumption of alcohol:o). 
    </content:encoded>

    <pubDate>Sat, 07 Oct 2006 19:15:00 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/198-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Improve Your Shell Life With Screen</title>
    <link>http://freebsd.munk.me.uk/archives/185-Improve-Your-Shell-Life-With-Screen.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/185-Improve-Your-Shell-Life-With-Screen.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=185</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=185</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Screen is one of those utilities you use for a short while and then find you can&#039;t live without it.  Imagine having a single Putty session running on your client machine, but having the same functionality of hitting &amp;lt;ALT&amp;gt;-F1, &amp;lt;ALT&amp;gt;-F2, &amp;lt;ALT&amp;gt;-F3 to switch easily between 3 different virtual tty&#039;s on the live server.  Screen allows you to login to a server once and then open as many extra shells as you want all within the same screen session and switch easily between them with simple keystrokes.&lt;br /&gt;
&lt;br /&gt;
A sample session goes something like follows (try this yourself if you&#039;re new to screen, remember to install it first!):&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li/&gt;Login to the server.&lt;br /&gt;
&lt;br /&gt;
&lt;li/&gt;Fire up screen for the first time:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;22&amp;#58;43&amp;#58;29&amp;#93;&amp;#160;munk@users&amp;#160;/home/munk#&amp;#160;screen&lt;br /&gt;
&amp;#60;screen&amp;#160;starts&amp;#160;up&amp;#62;&lt;br /&gt;
Screen&amp;#160;version&amp;#160;4.00.02&amp;#160;&amp;#40;FAU&amp;#41;&amp;#160;5-Dec-03&lt;br /&gt;
&lt;br /&gt;
Copyright&amp;#160;&amp;#40;c&amp;#41;&amp;#160;1993-2002&amp;#160;Juergen&amp;#160;Weigert,&amp;#160;Michael&amp;#160;Schroeder&lt;br /&gt;
Copyright&amp;#160;&amp;#40;c&amp;#41;&amp;#160;1987&amp;#160;Oliver&amp;#160;Laumann&lt;br /&gt;
...&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#91;Press&amp;#160;Space&amp;#160;or&amp;#160;Return&amp;#160;to&amp;#160;end.&amp;#93;&lt;/div&gt;&lt;br /&gt;
&lt;li/&gt;Hit space to continue, now you&#039;re inside screen.  Try running some command there like &#039;ls -al&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;li/&gt;Now start another shell/window in screen by hitting the keystroke &#039;ctrl-a c&#039; (c for create).  Another screen shell should open up.  Try running something in there like &#039;echo &quot;hello world&#039;.&lt;br /&gt;
&lt;br /&gt;
&lt;li/&gt;Now go back to the original screen by hitting the keystroke &#039;ctrl-a ctrl-a&#039; - you should see the output from the &#039;ls -al&#039; command still there.  To move back to the second screen shell, hit the keystroke again &#039;ctrl-a ctrl-a&#039; - you should see the output from the &#039;echo &quot;hello world&quot;&#039; command.&lt;br /&gt;
&lt;br /&gt;
&lt;li/&gt;Now try detaching from the screen session.  This is the feature that makes screen rock - you can open lots of shells under screen and then when you want to go and do something else, instead of closing each and every shell down, you just &#039;detach&#039; from screen.  &lt;br /&gt;
&lt;br /&gt;
This effectively leaves the shells running in the background - or whatever it is you&#039;ve got running in the screen shell - and when you want to come back later you can type a simple command and &#039;attach&#039; back to the screen session with all the shells/processes etc still running right where you left off.  Very cool for staying logged into IRC servers whilst you&#039;re away from the machine - even get logged into two different servers in two different shells running in screen.&lt;br /&gt;
&lt;br /&gt;
To detach from a screen session, just type in &#039;ctrl-a ctrl-d&#039; - you should see the text &#039;[detached]&#039;. You can close down the shell you&#039;re left in and go off and do something else now.  In fact you don&#039;t even need to detach from screen when you want to leave a screen session - say you want to shut your workstation down quickly and you have putty running an instance of screen.  Just kill the putty process and job done (I actually just hit the &#039;power&#039; button on the PC and let windows kill everything for me:)).  The screen session will still be ok in the background and when you come back later you can just resume the session fine.&lt;br /&gt;
&lt;br /&gt;
&lt;li/&gt;Ok now try and reattach to the screen process we detached from earlier.  To do this, login to the server as the user you started screen as and type in &#039;screen -d -r&#039; - I have this aliased to &#039;sc&#039; because I use it so much. After entering that command, screen should reattach itself to the session you left off above, with the two screen shells/windows running (try &#039;ctrl-a ctrl-a&#039; again).  Magic :)&lt;br /&gt;
&lt;br /&gt;
&lt;li/&gt;To quit one of the shells from inside screen, just issue the command you&#039;d usually use to exit the shell - usually &#039;exit&#039; or &#039;logout&#039; or the &#039;ctrl-d&#039; keystroke depending on what shell you&#039;re using.  When you exit out of the last shell running under screen, screen itself will exit.&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can find all the keystrokes for getting things done in screen are listed in the man page &#039;man screen&#039;.&lt;br /&gt;
&lt;br /&gt;
The list of most common keystrokes in screen to help you get around though are:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;ctrl-a c       - create a new shell / window&lt;br /&gt;
ctrl-a ctrl-a - switch to the shell/window previously displayed&lt;br /&gt;
ctrl-a n       - switch to the next shell/window (useful if you have more than 2 windows/shells running)&lt;br /&gt;
ctrl-a p       - switch to the previous shell/window (useful if you have more than 2 windows/shells running)&lt;br /&gt;
ctrl-a d       - exit out of the screen session&lt;/blockquote&gt;&lt;br /&gt;
Note also that normal shell keystrokes that start with &#039;ctrl-a&#039; - these have to be &#039;escaped&#039; before you execute them.  The main culprit is the &#039;ctrl-a&#039; keystroke to get to the start of a line in the shell - to do this from within screen you have to type in &#039;ctrl-a a&#039; - that&#039;s &#039;hold down ctrl and hit a at the same time, let go, then hit a again(!).  Slightly confusing at first but after a short while it becomes second nature.&lt;br /&gt;
&lt;br /&gt;
There are a load of other cool things you can do in screen, like naming windows/screens, splitting windows so you can have 2 shells on the same display (one shell at the top half of the display, one at the bottom), switch to a screen quickly using &#039;ctrl-a 0...9&#039; for screen number 0 ... 9 etc - the commands above though are the ones I use the most and you can survive easily with just those - let me know if I left anything out :) 
    </content:encoded>

    <pubDate>Mon, 18 Sep 2006 22:20:00 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/185-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Shell Tips: Improved Shell Usage Using Temp Files</title>
    <link>http://freebsd.munk.me.uk/archives/181-Shell-Tips-Improved-Shell-Usage-Using-Temp-Files.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/181-Shell-Tips-Improved-Shell-Usage-Using-Temp-Files.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=181</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=181</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Most of the time when working in a shell, it&#039;s the output from commands that you&#039;re interested in.  Using this tip you can make it easier to capture the output from a shell command to a temp file and not worry about cleaning up after you.&lt;br /&gt;
&lt;br /&gt;
The gist of the tip is very simple - create a temp directory inside your home directory - ie /home/munk/tmp - then setup a shell environment variable called $tmp which points to /home/munk/tmp/tmp.$$ - where $$ represents the process id of the current login shell.  Finally add a line to your ~/.logout file that deletes anything within /home/munk/tmp.  With this in place you can then easily redirect output from commands straight to that $tmp file, page through it as you want and then not worry about having to remove it manually at the end of the login session.&lt;br /&gt;
&lt;br /&gt;
This may sound convoluted but is something I would find hard working without now.  It takes a little while to get used to remembering to use &#039;&gt;$tmp&#039; at the end of commands to capture the output, but once you get used to it it&#039;s like second nature and you know you have the output around to go back to during the login session.&lt;br /&gt;
&lt;br /&gt;
This tip can be applied to any shell, but I personally use the CSH Shell so the code snippets below will focus on the syntax used in the CSH rc file, ~/.cshrc and ~/.logout.  The same principle applies to other shells though and shouldn&#039;t be too hard to set up.&lt;br /&gt;
&lt;br /&gt;
Here are the steps for setting the system up:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li/&gt;Create a temp directory inside your home directory and make sure only your user has access to it:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;mkdir&amp;#160;~/tmp&lt;br /&gt;
chmod&amp;#160;700&amp;#160;~/tmp&lt;/div&gt;&lt;br /&gt;
To make the system more portable you can also add the following to ~/.cshrc:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;&amp;#160;!-d&amp;#160;~/tmp&amp;#160;&amp;#93;&amp;#160;&amp;amp;&amp;amp;&amp;#160;mkdir&amp;#160;~/tmp&amp;#160;&amp;amp;&amp;amp;&amp;#160;chmod&amp;#160;700&amp;#160;~/tmp&lt;/div&gt;&lt;br /&gt;
&lt;li/&gt;Edit your ~/.cshrc file and add the following environment variable, $tmp:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;setenv&amp;#160;tmp&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;~/tmp/tmp.$$&lt;/div&gt;&lt;br /&gt;
The $tmp filename variable will be unique to this login session, $$ is the special shell variable which contains the process ID of the current process - in this case the PID will be that of the current shell login session.  You can see the value of the $tmp variable by using &#039;echo&#039;:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;echo&amp;#160;$tmp&lt;br /&gt;
/home/munk/tmp/tmp.26119&lt;/div&gt;&lt;br /&gt;
Also you can verify the current shell has that PID:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;ps&amp;#160;-auxww&amp;#160;26119&lt;br /&gt;
munk&amp;#160;26119&amp;#160;&amp;#160;0.0&amp;#160;&amp;#160;0.2&amp;#160;&amp;#160;1544&amp;#160;1068&amp;#160;&amp;#160;p2&amp;#160;&amp;#160;Ds&amp;#160;&amp;#160;&amp;#160;&amp;#160;5&amp;#58;39PM&amp;#160;&amp;#160;&amp;#160;0&amp;#58;00.13&amp;#160;/bin/tcsh&lt;/div&gt;&lt;br /&gt;
&lt;li/&gt;Edit or create ~/.logout to include the following:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;;&amp;#160;cat&amp;#160;~/.logout&lt;br /&gt;
rm&amp;#160;-rf&amp;#160;~/tmp/&amp;#160;&amp;amp;&amp;amp;&amp;#160;mkdir&amp;#160;~/tmp&amp;#160;&amp;amp;&amp;amp;&amp;#160;chown&amp;#160;munk&amp;#58;munk&amp;#160;~/tmp&lt;/div&gt;&lt;br /&gt;
You might want something in there to change perms on the directory to 700 as well if you want to protect anything dumped into $tmp.&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
Now you&#039;re ready to use the system.  Logout and back in again or issue &#039;rehash&#039; in CSH to setup the $tmp variable.&lt;br /&gt;
&lt;br /&gt;
Use the $tmp variable anywhere where you have a lot of output scrolling along that you want to be able to page through easily any time during the shell session:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#62;&amp;#160;grep&amp;#160;mess&amp;#160;$messages&amp;#62;$tmp&lt;br /&gt;
&amp;#62;&amp;#160;head&amp;#160;$tmp&lt;br /&gt;
Sep&amp;#160;&amp;#160;1&amp;#160;00&amp;#58;00&amp;#58;45&amp;#160;users&amp;#160;last&amp;#160;message&amp;#160;repeated&amp;#160;4&amp;#160;times&lt;br /&gt;
Sep&amp;#160;&amp;#160;3&amp;#160;00&amp;#58;54&amp;#58;04&amp;#160;users&amp;#160;last&amp;#160;message&amp;#160;repeated&amp;#160;188&amp;#160;times&lt;br /&gt;
Sep&amp;#160;&amp;#160;3&amp;#160;14&amp;#58;53&amp;#58;54&amp;#160;users&amp;#160;last&amp;#160;message&amp;#160;repeated&amp;#160;119&amp;#160;times&lt;br /&gt;
...&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
If you want to use a different &#039;temp&#039; file during the session, just add a number onto the end of $tmp - ie $tmp.1 etc:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;;&amp;#160;tail&amp;#160;$messages&amp;#160;&amp;#62;&amp;#160;$tmp.2&lt;br /&gt;
;&amp;#160;cat&amp;#160;$tmp.2&lt;br /&gt;
Sep&amp;#160;11&amp;#160;09&amp;#58;53&amp;#58;30&amp;#160;users&amp;#160;sshd&amp;#91;12583&amp;#93;&amp;#58;&amp;#160;login_getclass&amp;#58;&amp;#160;unknown&amp;#160;class&amp;#160;&#039;root&#039;&lt;br /&gt;
Sep&amp;#160;11&amp;#160;09&amp;#58;53&amp;#58;30&amp;#160;users&amp;#160;sshd&amp;#91;12584&amp;#93;&amp;#58;&amp;#160;warning&amp;#58;&amp;#160;/etc/hosts.allow,&amp;#160;line&amp;#160;29&lt;br /&gt;
Sep&amp;#160;11&amp;#160;09&amp;#58;53&amp;#58;31&amp;#160;users&amp;#160;sshd&amp;#91;12585&amp;#93;&amp;#58;&amp;#160;login_getclass&amp;#58;&amp;#160;unknown&amp;#160;class&amp;#160;&#039;root&#039;&lt;br /&gt;
...&lt;/div&gt;&lt;br /&gt;
This can be useful if you need to keep the contents of the first $tmp file and work with the content of another command easily.&lt;br /&gt;
&lt;br /&gt;
Another extra tip to use with this one is to create a shell alias called &#039;myread&#039;.  What the myread alias does is have any output from a command sent both to the current tty console as regular output but at the same time have it also piped to the $tmp file and then finally open the $tmp file in an editor of your choice.  Here&#039;s the &#039;myread&#039; CSH alias - syntax for other shells will differ slightly:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;alias&amp;#160;myread&amp;#160;&#039;tee&amp;#160;$tmp&amp;#160;&amp;amp;&amp;amp;&amp;#160;vim&amp;#160;$tmp&#039;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
That way instead of doing this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#62;&amp;#160;grep&amp;#160;something&amp;#160;somefile&amp;#160;&amp;#62;&amp;#160;$tmp&lt;br /&gt;
&amp;#62;&amp;#160;vim&amp;#160;$tmp&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
you can just do this instead:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#62;&amp;#160;grep&amp;#160;something&amp;#160;somefile&amp;#160;|&amp;#160;myread&lt;/div&gt; 
    </content:encoded>

    <pubDate>Mon, 11 Sep 2006 16:29:51 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/181-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>CSH Tips: Create Variables Everywhere</title>
    <link>http://freebsd.munk.me.uk/archives/177-CSH-Tips-Create-Variables-Everywhere.html</link>
            <category>Shell</category>
    
    <comments>http://freebsd.munk.me.uk/archives/177-CSH-Tips-Create-Variables-Everywhere.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=177</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=177</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    Shell environment variables allow you to easily access commonly used filenames/strings without the hassle of typing them out in longhand.&lt;br /&gt;
&lt;br /&gt;
For example instead of typing out:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;cd&amp;#160;/usr/local/etc/rc.d&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
it&#039;s a lot quicker just typing:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;cd&amp;#160;$rc_dir&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Even better, command line completion in CSH allows you to use &lt;TAB&gt; autocomplete on env variables, so you only need to type the first few chars of the variable, hit &lt;TAB&gt; and have a list of possible completions given:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;1&amp;#58;36&amp;#58;07&amp;#93;&amp;#160;munk@users&amp;#160;/home/munk#&amp;#160;cd&amp;#160;$r&amp;#60;TAB&amp;#62;&lt;br /&gt;
rc_dir&amp;#160;&amp;#160;&amp;#160;&amp;#160;rcconf&amp;#160;&amp;#160;&amp;#160;&amp;#160;rejectlog&amp;#160;rubybase&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
giving all the env variables that start with &#039;r&#039;.&lt;br /&gt;
&lt;br /&gt;
Adding env variables can be done on the commandline using:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;setenv&amp;#160;var_name&amp;#160;string&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
where &#039;var_name&#039; is the name of the variable you want to create and &#039;string&#039; is the string you want to assign to the vbl.&lt;br /&gt;
&lt;br /&gt;
However the best way is to add &#039;setenv&#039; lines to your ~/.cshrc file so the variables are ready to use when you login:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;;&amp;#160;grep&amp;#160;setenv&amp;#160;$cshrc&amp;#160;|&amp;#160;head&lt;br /&gt;
setenv&amp;#160;munk_dir&amp;#160;&quot;/home/munk/&quot;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;setenv&amp;#160;PAGER&amp;#160;&quot;less&amp;#160;-i&quot;&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;setenv&amp;#160;EDITOR&amp;#160;vim&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;setenv&amp;#160;PAGER&amp;#160;&quot;less&amp;#160;-i&quot;&lt;br /&gt;
setenv&amp;#160;&amp;#160;BLOCKSIZE&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;K&lt;br /&gt;
setenv&amp;#160;&amp;#160;MAIL&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;/var/mail/$USER&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;setenv&amp;#160;TERM&amp;#160;xterm-color&lt;br /&gt;
#&amp;#160;Common&amp;#160;setenvs&amp;#58;&lt;br /&gt;
setenv&amp;#160;tmp&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;/home/munk/tmp/tmp.$$&lt;br /&gt;
setenv&amp;#160;MONTH&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;`date&amp;#160;&quot;+%m&quot;`&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Some of my fav env vbls:&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#&amp;#160;useful&amp;#160;for&amp;#160;redirecting&amp;#160;output&amp;#160;to&amp;#160;a&amp;#160;temp&amp;#160;file&amp;#160;unique&amp;#160;to&amp;#160;this&amp;#160;login&amp;#160;session&amp;#160;-&amp;#160;&#039;grep&amp;#160;something&amp;#160;somedir&amp;#160;-ri&amp;#160;&amp;#62;&amp;#160;$tmp&#039;&amp;#58;&lt;br /&gt;
setenv&amp;#160;tmp&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;/home/munk/tmp/tmp.$$&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;useful&amp;#160;for&amp;#160;creating&amp;#160;timestamps&amp;#160;in&amp;#160;filenames&amp;#160;-&amp;#160;tar&amp;#160;zcvf&amp;#160;sometar.$DATE.tar.gz&amp;#160;somedir&amp;#58;&lt;br /&gt;
setenv&amp;#160;DATE&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;`date&amp;#160;&quot;+%Ymd&quot;`&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;easy&amp;#160;access&amp;#160;to&amp;#160;/var/log/messages&amp;#160;-&amp;#160;&#039;tail&amp;#160;-f&amp;#160;$messages&#039;&amp;#160;etc&amp;#58;&lt;br /&gt;
setenv&amp;#160;messages&amp;#160;/var/log/messages&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;start/stop/restart&amp;#160;system&amp;#160;daemons&amp;#160;quicker&amp;#160;with&amp;#160;&#039;$rc_dir/daemon.sh&amp;#160;restart&#039;&amp;#58;&lt;br /&gt;
setenv&amp;#160;rc_dir&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;/usr/local/etc/rc.d&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;access&amp;#160;the&amp;#160;perl&amp;#160;base&amp;#160;directory&amp;#160;quicker&amp;#58;&lt;br /&gt;
setenv&amp;#160;perlbase&amp;#160;/usr/local/lib/perl5/site_perl/5.8.8&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;crontab&amp;#58;&lt;br /&gt;
setenv&amp;#160;crontab&amp;#160;/etc/crontab&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;quickly&amp;#160;edit&amp;#160;~/.cshrc&amp;#160;-&amp;#160;vi&amp;#160;$cshrc&amp;#58;&lt;br /&gt;
setenv&amp;#160;cshrc&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;/home/munk/.cshrc&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;change&amp;#160;the&amp;#160;order&amp;#160;of&amp;#160;sourceforge&amp;#160;websites&amp;#160;that&amp;#160;portupgrade&amp;#160;fetches&amp;#160;from&amp;#160;by&amp;#160;default&amp;#58;&lt;br /&gt;
setenv&amp;#160;MASTER_SITE_SOURCEFORGE&amp;#160;\&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&quot;http&amp;#58;//eu.dl.sourceforge.net/%SUBDIR%/&amp;#160;\&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;http&amp;#58;//us.dl.sourceforge.net/%SUBDIR%/&amp;#160;\&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;ftp&amp;#58;//us.dl.sourceforge.net/pub/sourceforge/%SUBDIR%/&amp;#160;\&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;ftp&amp;#58;//ftp.kddlabs.co.jp/sourceforge/%SUBDIR%/&amp;#160;\&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;ftp&amp;#58;//ftp.chg.ru/pub/sourceforge/%SUBDIR%/&quot;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Any time I find myself using a string on the commandline more than half a dozen times in a session I usually add a variable in $cshrc for it - actually using a shell alias &#039;cshrc_edit&#039;, but that&#039;s another top tip and needs an article all to itself :) 
    </content:encoded>

    <pubDate>Thu, 07 Sep 2006 00:15:34 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/177-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>
<item>
    <title>Solving permission problems with parsepath.pl</title>
    <link>http://freebsd.munk.me.uk/archives/175-Solving-permission-problems-with-parsepath.pl.html</link>
            <category>Apache</category>
            <category>FreeBSD</category>
            <category>General</category>
            <category>Perl</category>
            <category>PHP</category>
            <category>Security</category>
            <category>Shell</category>
            <category>SSH</category>
    
    <comments>http://freebsd.munk.me.uk/archives/175-Solving-permission-problems-with-parsepath.pl.html#comments</comments>
    <wfw:comment>http://freebsd.munk.me.uk/wfwcomment.php?cid=175</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://freebsd.munk.me.uk/rss.php?version=2.0&amp;type=comments&amp;cid=175</wfw:commentRss>
    

    <author>nospam@example.com (munk)</author>
    <content:encoded>
    &lt;a href=&quot;http://sial.org/howto/debug/unix/parsepath&quot;  title=&quot;parsepath.pl - a script for solving file permission problems&quot;&gt;parsepath.pl&lt;/a&gt; is a brilliant perl script for fixing permissions problems on Unix based platforms by &lt;a href=&quot;http://sial.org/&quot;  title=&quot;Jeremy Mates&quot;&gt;Jeremy Mates&lt;/a&gt;.   Probably the most common type of permission problem from a sysadmin/webmaster&#039;s viewpoint is uploading a file to a directory in a website&#039;s document root folder and then trying to access the file or script in a web browser only to get the dreaded 403 error message:&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Forbidden&lt;br /&gt;
You don&#039;t have permission to access /foo/bar/test.php on this server.&lt;br /&gt;
&lt;/blockquote&gt;&lt;br /&gt;
&lt;br /&gt;
Most time the solution is very simple, just change the permissions on &#039;test.php&#039; to make sure the user the webserver runs as can read the file correctly - the simplest and most common method being to change the mode of the file to &#039;755&#039;:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;chmod&amp;#160;755&amp;#160;test.php&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Unfortunately sometimes it&#039;s not that easy and many times you see users asking &#039;I&#039;m getting &#039;access denied&#039; errors even though I&#039;ve changed the perms to 755&#039;.  The problem is that one of the subdirectories that the &#039;test.php&#039; file lives in has permissions set so that the webserver can&#039;t read the file properly.  Now that&#039;s where the headache comes in :)&lt;br /&gt;
&lt;br /&gt;
However, &lt;a href=&quot;http://sial.org/howto/debug/unix/parsepath&quot;  title=&quot;parsepath.pl - a script for solving file permission problems&quot;&gt;parsepath.pl&lt;/a&gt; can take the headache out of fixing permissions problems.&lt;br /&gt;
&lt;br /&gt;
Say you have a website document root directory tree /usr/local/www/web/www.munk.me.uk/foo/bar and you upload a web script &#039;test.php&#039; into that directory.  You try and access the file in a webbrowser but get the 403 permission denied error above.  First off you check the permissions on the file itself:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;23&amp;#58;58&amp;#58;17&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar#&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;;&amp;#160;ls&amp;#160;-l&lt;br /&gt;
total&amp;#160;0&lt;br /&gt;
-rwxr-xr-x&amp;#160;&amp;#160;1&amp;#160;www&amp;#160;www&amp;#160;&amp;#160;0&amp;#160;Sep&amp;#160;&amp;#160;4&amp;#160;23&amp;#58;39&amp;#160;test.php&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
That looks ok, with permissions 755 and the owner/group set to &#039;www&#039; the webserver user &#039;www&#039; should be able to read the file ok.  So in this case the problem must be with the permissions on one of the parent subdirectories.  The old method of working out the perms would be either to trawl one by one through each directory checking the perms on each subdirectory or to change the permissions recursively on the document root folder so all subfolders have the read bit set for the webserver user/group.&lt;br /&gt;
&lt;br /&gt;
With parsepath.pl things are a lot simpler though - just run the following command:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;0&amp;#58;03&amp;#58;21&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar#&amp;#160;parsepath.pl&amp;#160;user=www&amp;#160;+r&amp;#160;test.php&lt;br /&gt;
!&amp;#160;group=www&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0700&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo&lt;br /&gt;
!&amp;#160;unix-other&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0750&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
With this command parsepath.pl  recurses through each subdirectory below the file/path you feed it on the commandline and tells you the permissions problems - if any - for the user &#039;www&#039; (the user=www argument) to read (the +r argument) the file &#039;test.php&#039;.&lt;br /&gt;
&lt;br /&gt;
In the output, we&#039;re told that permissions to read the test.php by the user www fails on two counts:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;#&amp;#160;the&amp;#160;group&amp;#160;bit&amp;#160;on&amp;#160;the&amp;#160;folder&amp;#160;&#039;foo&#039;&amp;#160;doesn&#039;t&amp;#160;have&amp;#160;the&amp;#160;+rx&amp;#160;flag&amp;#160;set&amp;#58;&lt;br /&gt;
!&amp;#160;group=www&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0700&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo&lt;br /&gt;
&lt;br /&gt;
#&amp;#160;the&amp;#160;other&amp;#160;bit&amp;#160;on&amp;#160;the&amp;#160;folder&amp;#160;&#039;bar&#039;&amp;#160;doesn&#039;t&amp;#160;have&amp;#160;the&amp;#160;+rx&amp;#160;flag&amp;#160;set&amp;#58;&lt;br /&gt;
!&amp;#160;unix-other&amp;#160;+rx&amp;#160;fails&amp;#58;&amp;#160;d&amp;#160;0750&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
With this information it&#039;s easy enough to go in and make the changes necessary to fix the problem using &#039;chmod g+rx foo foo/bar&#039;.&lt;br /&gt;
&lt;br /&gt;
There are other ways of invoking parsepath.pl though.  Running it just with a file/path as an argument it&#039;ll tell you the permissions on each subdirectory under it:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;bb-code-title&quot;&gt;CODE:&lt;/div&gt;&lt;div class=&quot;bb-code&quot;&gt;&amp;#91;0&amp;#58;10&amp;#58;33&amp;#93;&amp;#160;root@users&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar#&amp;#160;&lt;br /&gt;
&amp;#62;&amp;#160;parsepath.pl&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar/test.php&lt;br /&gt;
%&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar/test.php&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/usr&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local&lt;br /&gt;
d&amp;#160;0755&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www&lt;br /&gt;
d&amp;#160;0770&amp;#160;www&amp;#58;wheel&amp;#160;/usr/local/www/web&lt;br /&gt;
d&amp;#160;0750&amp;#160;www&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk&lt;br /&gt;
d&amp;#160;0700&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo&lt;br /&gt;
d&amp;#160;0750&amp;#160;root&amp;#58;wheel&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar&lt;br /&gt;
f&amp;#160;0755&amp;#160;root&amp;#58;www&amp;#160;/usr/local/www/web/www.munk.me.uk/foo/bar/test.php&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
which can is better to see a whole tree in one go.  &lt;br /&gt;
&lt;br /&gt;
No permissions were harmed in the making of this article!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I&#039;ll include the parsepath.pl script in the extended article just in case the original ever gets lost - big credit of course goes to the author of the script, &lt;a href=&quot;http://sial.org/&quot;  title=&quot;Jeremy Mates&quot;&gt;Jeremy Mates&lt;/a&gt;.  His site is actually very interesting from a sysadmin&#039;s point of view containing lots of interesting admin scripts and thoughts on system administration in general - spent quite a while grazing through his stuff there - cheers Jeremy.&lt;br /&gt;
 &lt;br /&gt;&lt;a href=&quot;http://freebsd.munk.me.uk/archives/175-Solving-permission-problems-with-parsepath.pl.html#extended&quot;&gt;Continue reading &quot;Solving permission problems with parsepath.pl&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Mon, 04 Sep 2006 22:41:00 +0000</pubDate>
    <guid isPermaLink="false">http://freebsd.munk.me.uk/archives/175-guid.html</guid>
    <creativeCommons:license>http://creativecommons.org/licenses/by/2.5/</creativeCommons:license>
</item>

</channel>
</rss>