Colored CLI Feedback

I make quit a few command line apps to help with daily tasks. It helps to have meaningful feedback and reporting while these are running. Adding color helps with quickly identifying issues or just keeping quick tabs on the running scripts.

run = '\033[1;97m[~]\033[1;m'
bad = '\033[1;31m[-]\033[1;m'
good = '\033[1;32m[+]\033[1;m'
info = '\033[1;33m[!]\033[1;m'
que = '\033[1;34m[?]\033[1;m'

GitHub Link

These escape codes go well beyond just simple colors though. Check this link below for more CLI goodies.

http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html

Python Check for 301 redirects

In web development, moving from staging to production servers, it’s useful to check that staging is pointing to production after the DNS changes.  Simple to check all urls if the site you’re working on only has few pages. A simple curl -I http://your.url.here.com will work just fine.

But what if you have a hundred or more pages to check? If I was paid by the hour, yeah I would copy/paste each url into a terminal and check, but I’m salary. So here’s a quick python script to check them for me.

You need to provide it a text file with all the urls on their own line. Easy if the urls are in a spread sheet, simple copy/paste into the text file and off to the races. This code will account for white-space and empty lines, however, it will not account for missing http/https prepends.  So far testing shows compatible with Python2 and Python3. Requires the requests library.

# requires requests lib
# - pip install requests
import requests
import sys
def redirectTest(url):
    with open("no_redirects.txt", "a") as no_redirect:
        try:
            r = requests.head(url, allow_redirects=False)
            if (r.status_code == 301):
                print("+ %s :: %d" % (url, r.status_code))
            else:
                print("- WARNING: %s :: %d" % (url, r.status_code))
                no_redirect.write("%s :: %d\n" % (url, r.status_code))
        except requests.exceptions.RequestException as e:
            print("! Error with request: %s :: %s" % (url, e))
def load_urls(urlfile):
    # clean urls of white space and remove empty lines
    # this does not prepend http/https to urls missing them
    clean_urls = []
    with open(urlfile) as f:
        urllist = f.readlines()
    for i in urllist:
        i = i.strip()
        if i:
            clean_urls.append(i)
    return (clean_urls)
args = sys.argv
if len(args) < 2:
    print("! Error: Missing arguments.\n\nUsage: python check301.py urls.txt")
else:
    try:
        l = load_urls(args[1])
        for i in l:
            redirectTest(i)
    except Exception as e:
        print("Something went wrong: %s" % e)

Feel free to change, modify, use, extend, and improve this code. You can find it on GitHub here.

This link will 301 to my About page.

XChat away plugin

My computer pretty much stays connected to an IRC server. If I’m not in  one of my favorite forums channels, I’m into something else.

If you do frequent IRC, you will want to register your nick with the nickserv so no one else steals your name. This is a great feature, but if your like me and registered two nick’s, you’ll have to identify with both every time. You might be wondering, “Two nicks? Why?” Well, I use one nick for my presence “techb” and “techb_away” when I’m not active. This may spark a new question, why not use the /away and /back commands? Well, I’m lazy, and for people who use a cli client like Irssi or rolled their own just cause, setting your nick to away might not show them your away. So, I use two nick’s to distinguish when I’m there and not.

Continue reading “XChat away plugin”

Hello Recursion!

Since I haven’t posted since last year :/ I figured why not show some recursion?

I know a lot of people try out something like a Fibonacci Sequence to help grasp the paradigm of recursion. I am going to do something a bit simpler. We will adapt the “Hello, World!” into a recursive function. We will play a bit with stdout in python as well.

Continue reading “Hello Recursion!”

Android ASE: WiFi Scan with UI

Playing more with the Android Scripting Environment, I wrote a simple script to display any access points in wifi range. Once you select an SSID, it will display some basic info on that AP, such as the MAC address and encryption type. If you have a rooted phone, you could theoretically brute force an APs password using *nix iwconfig commands. The next post I will show some quick and dirty code to brute force passwords. But for now here is the script for some UI WiFi scanning.

Continue reading “Android ASE: WiFi Scan with UI”

Android Twisted SpyCam

I have been playing around with Androids ASE(android scripting environment). ASE allows you to run scripts on your android powered device. You can use Python, PHP, Javascript, Pearl, Ruby and much more. To install ASE on your device you must either have root, allow third party apps install,, or know how to sideload apk’s using android’s SDK tools like adb.

Continue reading “Android Twisted SpyCam”