Wednesday, February 29, 2012

netdiscover

Not saying anything smart in this post, just placing here as note to self about the name of this tool and basic commands to get it started if I'm on a new machine that needs it.



sudo apt-get install netdiscover
sudo netdiscover
sudo netdiscover -r 192.168.1.0/24
sudo netdiscover -i wlan0 -r 192.168.1.0/24
sudo netdiscover -h
Netdiscover 0.3-beta7 [Active/passive arp reconnaissance tool]
Written by: Jaime Penalba 

Usage: netdiscover [-i device] [-r range | -l file | -p] [-s time] [-n node] [-c count] [-f] [-d] [-S] [-P] [-C]
  -i device: your network device
  -r range: scan a given range instead of auto scan. 192.168.6.0/24,/16,/8
  -l file: scan the list of ranges contained into the given file
  -p passive mode: do not send anything, only sniff
  -F filter: Customize pcap filter expression (default: "arp")
  -s time: time to sleep between each arp request (miliseconds)
  -n node: last ip octet used for scanning (from 2 to 253)
  -c count: number of times to send each arp reques (for nets with packet loss)
  -f enable fastmode scan, saves a lot of time, recommended for auto
  -d ignore home config files for autoscan and fast mode
  -S enable sleep time supression betwen each request (hardcore mode)
  -P print results in a format suitable for parsing by another program
  -L in parsable output mode (-P), continue listening after the active scan is completed

If -r, -l or -p are not enabled, netdiscover will scan for common lan addresses.

Thursday, January 19, 2012

Print just the name of the running Python program

#!/usr/bin/env python
import sys
import os
for i in sys.argv:
        print i
print sys.argv[0]
print os.path.basename(sys.argv[0].split(".")[0])
#>bin/test.py 56 555
bin/test.py
56
555
bin/test.py
test

Sunday, January 1, 2012

MySQL Connect in Python

#!/usr/bin/env python
import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='cyrushellborg666', db='brewing')
cur = conn.cursor()
cur.execute("SHOW TABLES")
# print cur.description
# r = cur.fetchall()
# print r
# ...or...
for r in cur:
   print r
cur.close()
conn.close()

Friday, December 30, 2011

Basic timer in Python

#!/usr/bin/env python


import time
import threading

class Timer(threading.Thread):
  def __init__(self, seconds):
   self.runTime = seconds
   threading.Thread.__init__(self)
  def run(self):
   time.sleep(self.runTime)
   print "Buzzzz!! Time's up!"

class CountDownTimer(Timer):
  def run(self):
   counter = self.runTime
   for sec in range(self.runTime):
    print counter
    time.sleep(1.0)
    counter -= 1
   print "Done."


class CountDownExec(CountDownTimer):   
  def __init__(self, seconds, action):  
   self.action = action    
   CountDownTimer.__init__(self, seconds) 
  def run(self):      
   CountDownTimer.run(self)   
   self.action()     
   
def myAction():      
  print "Performing my action..."   



c = CountDownExec(10, myAction)
c.start()
#>./timer.py 
10
9
8
7
6
5
4
3
2
1
Done.
Performing my action...

1wire generalized PHP script

<?php
$ow
="/mnt/1wire";
print 
"<table border=\"1\" padding=\"4\" >\n";
print 
"<tr><th>Full Path</th><th>Device</th><th>File</th><th>File Contents</th></tr>\n";
if (
$owhdl = opendir($ow)) {
    while (
false !== ($a = readdir($owhdl))) {
        
$aArray = explode('.',$a);      
        if ( (
count($aArray) == 2) && (strlen($aArray[0]) == 2 )) {
             if (
$owahdl = opendir($ow."/".$a)) {
                   while (
false !== ($b = readdir($owahdl))) {
                           
$c=$ow."/".$a."/".$b;  
                               if (
file_exists($c)) {
                        
$contents = fopen($c, "r");
                                
$parameter=array_pop(explode("/",$c));
                    
$fc=fgets($contents); 
                            print 
"\n<tr><td>".$ow."/</td>\n<td>".$a."</td>";
                    print 
"\n<td>".$parameter."</td>\n<td>\n";
                    
$parameter=="type" ? print "<a target=\"_blank\" href=\"http://www.owfs.org/uploads/".$fc.".html\">".$fc."</a>" : print $fc;
                    print 
"\n</td></tr>\n";
                            
fclose($contents);
                                            }
                                    }
                    
closedir($owahdl);
                    }
                }
           }
    
closedir($owhdl);
}
print 
"\n</table>\n";
print 
"<br />";
highlight_file($docRoot."/inc/index.txt");
?>



Full PathDeviceFileFile Contents
/mnt/1wire/ 10.AF2551010800 address 10AF255101080023
/mnt/1wire/ 10.AF2551010800 alias
/mnt/1wire/ 10.AF2551010800 crc8 23
/mnt/1wire/ 10.AF2551010800 errata
/mnt/1wire/ 10.AF2551010800 family 10
/mnt/1wire/ 10.AF2551010800 id AF2551010800
/mnt/1wire/ 10.AF2551010800 locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 10.AF2551010800 power 0
/mnt/1wire/ 10.AF2551010800 present 1
/mnt/1wire/ 10.AF2551010800 r_address 230008015125AF10
/mnt/1wire/ 10.AF2551010800 r_id 0008015125AF
/mnt/1wire/ 10.AF2551010800 r_locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 10.AF2551010800 temperature 39.9875
/mnt/1wire/ 10.AF2551010800 temphigh 167
/mnt/1wire/ 10.AF2551010800 templow 158
/mnt/1wire/ 10.AF2551010800 type DS18S20
/mnt/1wire/ 10.AF2551010800 .
/mnt/1wire/ 10.AF2551010800 ..
/mnt/1wire/ 22.20F21A000000 address 2220F21A00000025
/mnt/1wire/ 22.20F21A000000 alias
/mnt/1wire/ 22.20F21A000000 crc8 25
/mnt/1wire/ 22.20F21A000000 errata
/mnt/1wire/ 22.20F21A000000 family 22
/mnt/1wire/ 22.20F21A000000 fasttemp 62.6
/mnt/1wire/ 22.20F21A000000 id 20F21A000000
/mnt/1wire/ 22.20F21A000000 locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 22.20F21A000000 power 1
/mnt/1wire/ 22.20F21A000000 present 1
/mnt/1wire/ 22.20F21A000000 r_address 250000001AF22022
/mnt/1wire/ 22.20F21A000000 r_id 0000001AF220
/mnt/1wire/ 22.20F21A000000 r_locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 22.20F21A000000 temperature 63.275
/mnt/1wire/ 22.20F21A000000 temperature10 63.05
/mnt/1wire/ 22.20F21A000000 temperature11 63.5
/mnt/1wire/ 22.20F21A000000 temperature12 63.275
/mnt/1wire/ 22.20F21A000000 temperature9 62.6
/mnt/1wire/ 22.20F21A000000 temphigh 167
/mnt/1wire/ 22.20F21A000000 templow 158
/mnt/1wire/ 22.20F21A000000 type DS1822
/mnt/1wire/ 22.20F21A000000 .
/mnt/1wire/ 22.20F21A000000 ..
/mnt/1wire/ 22.650E1B000000 address 22650E1B000000B3
/mnt/1wire/ 22.650E1B000000 alias
/mnt/1wire/ 22.650E1B000000 crc8 B3
/mnt/1wire/ 22.650E1B000000 errata
/mnt/1wire/ 22.650E1B000000 family 22
/mnt/1wire/ 22.650E1B000000 fasttemp 45.5
/mnt/1wire/ 22.650E1B000000 id 650E1B000000
/mnt/1wire/ 22.650E1B000000 locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 22.650E1B000000 power 1
/mnt/1wire/ 22.650E1B000000 present 1
/mnt/1wire/ 22.650E1B000000 r_address B30000001B0E6522
/mnt/1wire/ 22.650E1B000000 r_id 0000001B0E65
/mnt/1wire/ 22.650E1B000000 r_locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 22.650E1B000000 temperature 45.3875
/mnt/1wire/ 22.650E1B000000 temperature10 45.5
/mnt/1wire/ 22.650E1B000000 temperature11 45.5
/mnt/1wire/ 22.650E1B000000 temperature12 45.3875
/mnt/1wire/ 22.650E1B000000 temperature9 45.5
/mnt/1wire/ 22.650E1B000000 temphigh 167
/mnt/1wire/ 22.650E1B000000 templow 158
/mnt/1wire/ 22.650E1B000000 type DS1822
/mnt/1wire/ 22.650E1B000000 .
/mnt/1wire/ 22.650E1B000000 ..
/mnt/1wire/ 29.A29F09000000 LCD_H
/mnt/1wire/ 29.A29F09000000 LCD_M
/mnt/1wire/ 29.A29F09000000 PIO.BYTE 0
/mnt/1wire/ 29.A29F09000000 PIO.ALL 0,0,0,0,0,0,0,0
/mnt/1wire/ 29.A29F09000000 PIO.0 0
/mnt/1wire/ 29.A29F09000000 PIO.1 0
/mnt/1wire/ 29.A29F09000000 PIO.2 0
/mnt/1wire/ 29.A29F09000000 PIO.3 0
/mnt/1wire/ 29.A29F09000000 PIO.4 0
/mnt/1wire/ 29.A29F09000000 PIO.5 0
/mnt/1wire/ 29.A29F09000000 PIO.6 0
/mnt/1wire/ 29.A29F09000000 PIO.7 0
/mnt/1wire/ 29.A29F09000000 address 29A29F090000005A
/mnt/1wire/ 29.A29F09000000 alias
/mnt/1wire/ 29.A29F09000000 crc8 5A
/mnt/1wire/ 29.A29F09000000 family 29
/mnt/1wire/ 29.A29F09000000 id A29F09000000
/mnt/1wire/ 29.A29F09000000 latch.BYTE 255
/mnt/1wire/ 29.A29F09000000 latch.ALL 1,1,1,1,1,1,1,1
/mnt/1wire/ 29.A29F09000000 latch.0 1
/mnt/1wire/ 29.A29F09000000 latch.1 1
/mnt/1wire/ 29.A29F09000000 latch.2 1
/mnt/1wire/ 29.A29F09000000 latch.3 1
/mnt/1wire/ 29.A29F09000000 latch.4 1
/mnt/1wire/ 29.A29F09000000 latch.5 1
/mnt/1wire/ 29.A29F09000000 latch.6 1
/mnt/1wire/ 29.A29F09000000 latch.7 1
/mnt/1wire/ 29.A29F09000000 locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 29.A29F09000000 por 1
/mnt/1wire/ 29.A29F09000000 power 1
/mnt/1wire/ 29.A29F09000000 present 1
/mnt/1wire/ 29.A29F09000000 r_address 5A000000099FA229
/mnt/1wire/ 29.A29F09000000 r_id 000000099FA2
/mnt/1wire/ 29.A29F09000000 r_locator FFFFFFFFFFFFFFFF
/mnt/1wire/ 29.A29F09000000 sensed.BYTE 255
/mnt/1wire/ 29.A29F09000000 sensed.ALL 1,1,1,1,1,1,1,1
/mnt/1wire/ 29.A29F09000000 sensed.0 1
/mnt/1wire/ 29.A29F09000000 sensed.1 1
/mnt/1wire/ 29.A29F09000000 sensed.2 1
/mnt/1wire/ 29.A29F09000000 sensed.3 1
/mnt/1wire/ 29.A29F09000000 sensed.4 1
/mnt/1wire/ 29.A29F09000000 sensed.5 1
/mnt/1wire/ 29.A29F09000000 sensed.6 1
/mnt/1wire/ 29.A29F09000000 sensed.7 1
/mnt/1wire/ 29.A29F09000000 set_alarm 0
/mnt/1wire/ 29.A29F09000000 strobe 0
/mnt/1wire/ 29.A29F09000000 type DS2408
/mnt/1wire/ 29.A29F09000000 .
/mnt/1wire/ 29.A29F09000000 ..

Monday, October 17, 2011

Tiny Ad-Hoc CMS

index.php

<?php
$time_start = microtime(true);
error_reporting(E_ALL);
$pagetitle="hello world";
$needDB=1;
isset($needDB) ? include("db.inc.php") : $linkReport="\n>!--- no database connection used ---<\n" ;
include("inc/lists.txt");
foreach($anatomyA as $part) {include(${part});}
print $linkReport;
showSource();
$base=explode("/",$_SERVER['PHP_SELF']);
$num=count($base);
$base2=explode(".",$base[$num-1]);
$content=$docRoot."/inc/".$base2[0].".txt";
include($content);
foreach($anatomyB as $part) {include(${part});}
die();
?>

db.inc.php

<?php
if (isset($needDB)) {
${server}="localhost";
${username}="someuser";
${password}="somepassword";
${database}="somedatabase";
$link=mysql_connect(${server},${username},${password});
@mysql_select_db($database);
}
else {
$to = "example@example.com";
$subject = "alert db.inc.php";
$body = "a direct access to db.inc.db was just attempted.";
mail($to, $subject, $body);
die();
}
$link ? $linkReport="\n<!---mysql_connect was successfull-->\n" : $linkReport="\n<!---mysql_connect was not successful-->\n";
?>

inc/lists.txt

<?php
putenv("TZ=America/Los_Angeles");
$homepage="/index.php";
${docRoot}=getenv('DOCUMENT_ROOT');
${anatomyA}=array(
      "${docRoot}/inc/header.txt",
      "${docRoot}/inc/showsource.txt",
      "${docRoot}/inc/BANNERS.genPublic.txt"
      );
${anatomyB}=array(
      "${docRoot}/inc/footer.txt"
      );
?>

inc/header.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<?php isset($linkReport) ? print $linkReport : print "\n>!--- no \$linkReport ---<\n"; ?>
<title>
<?php
print ${pagetitle};
?>
</title>


<?php
// grabs google api key for javascript
if (${needMaps}) {include("inc/googleMapsKey.js.txt");}
?>


<script type="text/javascript"> 
/*
http://code.google.com/apis/ajaxsearch/signup.html
http://code.google.com/apis/ajaxlibs/documentation/
*/
  // google.load("jquery", "1");
  // google.load("jqueryui", "1");
  // google.load("prototype", "1");
  // google.load("scriptaculous", "1");
  // google.load("mootools", "1"); TURNED OFF
  // google.load("dojo", "1");    TURNED OFF
  
</script>  

<link type="text/css" rel="stylesheet" href="/css/dos.css">
  </link>

<script type="text/javascript" src="/js/getElementsByClassName.js">
  </script>

<script type="text/javascript" src="/js/main.js">
  </script>

</head>
<body>
<table id="debugtable" style="display:none">
<tr>
<td class="boxstyle1" id="debugbox">debug output</td> 
<td class="spanstyle1">>></td>
<td>
<textarea id="debug" cols="80" rows="15" wrap="soft">
Welcome to the debug output textarea!
Vital info for the debug textarea :
1) the ID for this output field is: debug
2) is loaded into every page
3) defaults to style.display=none
4) area is shown by setting style.display=block
5) area is shown by javascript command debug() 
6) area is shown by debug=y query string at end of any URL
</textarea>
</td>
</tr>
</table>
<form name="formMain" id="formMain" method="POST">

inc/showsource.txt

<?php
// never include this in php files that show passwords
function showSource() {
if (isset($_GET['source'])) { highlight_file($_SERVER["SCRIPT_FILENAME"]); phpinfo(); }
}
function hilite($me) { print "\n<h3>${me}</h3>\n"; highlight_file(${me});}
?>

/inc/BANNERS.genPublic.txt

<div id="banner" style="border: solid 1px #000000">
<center>
<h1>Hello World</h1>
</center>
</div>

inc/footer.txt

</form>
<hr width="50%" />
<div id="footer">
<center>
</body>
</html>
<?php 
${time_end} = microtime(true);
isset(${time_start}) ? ${processTime} = $time_end - $time_start : ${processTime}=0 ;
print "\n<p><center>server process time: ".${processTime}."</center></p>\n";
include("${docRoot}/inc/insertAccesslogs.txt");
?>
</center>
</div>
</body>
</html>
<?php
die();
?>

inc/insertAccesslogs.txt

<?php
$page=getenv("SCRIPT_NAME"); 
$query=getenv("QUERY_STRING");
$referer=getenv("HTTP_REFERER"); 
$ip=getenv("REMOTE_ADDR");
isset(${processTime}) ? ${processTime}=${processTime} : ${processTime}=0 ; 
$query = "INSERT INTO `mydatabase`.`accesslogs` (`id`, `page`, `query`, `time`, `referer`, `ip`, `processTime`) 
          VALUES (NULL, '${page}', '${query}', NOW(), '${referer}', '${ip}', '${processTime}');";
print "\$sql = ".${query};
mysql_query(${query}) or trigger_error(mysql_error(),E_USER_ERROR);
?>

inc/accesslogs.sql.txt

--
-- Table structure for table `accesslogs`
--

CREATE TABLE IF NOT EXISTS `accesslogs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `page` varchar(64) DEFAULT NULL,
  `query` varchar(64) DEFAULT NULL,
  `time` date NOT NULL,
  `referer` varchar(64) DEFAULT NULL,
  `ip` varchar(64) DEFAULT NULL,
  `processTime` varchar(64) DEFAULT NULL,
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

css/main.css

body {
background-color:black;
color:gray;
font-family:Megazeux, monospace;
font-size:28px;
line-height:28px;
}

pre {
font-family:Megazeux, monospace;
}

a:link
{
text-decoration:none;
color:gray;
}

a:visited
{
text-decoration:none;
color:gray;
}

a:hover, a:active
{
text-decoration:none;
color:gray;
}

.blinker {
text-decoration:blink;
}

#footer {
font-size:75%;
}

Saturday, October 15, 2011

Unix Command Line Prompts


PS1="\n#--[\[\e[1;36m\]\u@\h\[\e[m\]]-[\[\e[1;34m\]\w\[\e[m\]]-[\$(date)]-->\n#>"