<?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>Historias de un Geek &#187; python</title>
	<atom:link href="http://albertein.com.mx/archives/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://albertein.com.mx</link>
	<description>AlbertEin - Desvarios de una mente perturbada</description>
	<lastBuildDate>Wed, 30 Nov 2011 16:12:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Mi primera vez (python)</title>
		<link>http://albertein.com.mx/archives/211</link>
		<comments>http://albertein.com.mx/archives/211#comments</comments>
		<pubDate>Sun, 01 Feb 2009 02:36:28 +0000</pubDate>
		<dc:creator>AlbertEin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[primera vez]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://albertein.gamersla.net/?p=211</guid>
		<description><![CDATA[Hace tiempo que habia visto a python, me llamo la atención por sus características poco usuales, como que los espacios tenga un valor semántico a diferencia de uno puramente visual, donde la identación en el código crea los bloques de codigos que en otros lenguajes se crean con &#8220;begin y end&#8221;, &#8220;{ y }&#8221;, etc. [...]]]></description>
			<content:encoded><![CDATA[<p>Hace tiempo que habia visto a python, me llamo la atención por sus características poco usuales, como que los espacios tenga un valor semántico a diferencia de uno puramente visual, donde la identación en el código crea los bloques de codigos que en otros lenguajes se crean con &#8220;begin y end&#8221;, &#8220;{ y }&#8221;, etc.</p>
<p>Desgraciadamente no habia tenido oportunidad de usarlo para un requerimiento real de mi trabajo, pero eso cambio el día de hoy.</p>
<p><strong>Hoy fue mi primera vez.</strong></p>
<p>El siguiente script fue mi primero con python, toma dos parámetros de la linea de comando, el primero es un archivo con una lista de nombres de usuarios, como el que se obtiene con el siguiente comando:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> : <span style="color: #ff0000;">'{print $1}'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">passwd</span> <span style="color: #000000; font-weight: bold;">&gt;</span> userDatabase.txt</pre></td></tr></table></div>

<p>El segundo parámetro es un archivo que contiene una lista de usuario{Tabulador}password como por ejemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">panchito	pancho123
albertein	paparazi
dariamox	password</pre></td></tr></table></div>

<p>Lo que hace es imprimir el usuario y contraseña (separado por &#8220;:&#8221;) del segundo archivo cuando el usuario se encuentre listado en el primer archivo. Un formato idóneo para usarlo junto con chpasswd (utilería que cambia las contraseñas del sistema en modo batch).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">python passwordFormatter.py systemUsers passwordDatabase <span style="color: #000000; font-weight: bold;">|</span> chpasswd</pre></td></tr></table></div>

<p>El dichoso script, es este:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #008000;">file</span> = <span style="color: #008000;">open</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
userList = <span style="color: #008000;">file</span>.<span style="color: black;">read</span> <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">file</span>.<span style="color: black;">close</span> <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #008000;">file</span> = <span style="color: #008000;">open</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">file</span>:
       userData = line.<span style="color: black;">split</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span><span style="color: black;">&#41;</span>
       username = userData <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
       password = userData <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
       <span style="color: #ff7700;font-weight:bold;">if</span> username <span style="color: #ff7700;font-weight:bold;">in</span> userList:
               <span style="color: #ff7700;font-weight:bold;">print</span> username + <span style="color: #483d8b;">':'</span> + password,
&nbsp;
<span style="color: #008000;">file</span>.<span style="color: black;">close</span> <span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://albertein.com.mx/archives/211/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

