Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 154 total  RSS 

WWAN Dialer

This script dials the built-in Verizon WWAN (mobile broadband) modem on my Thinkpad T61.

#!/bin/bash
#
# Dial the WWAN
#

hang_up () {
    echo 
    echo 'WWAN deactivated'
    echo
    exit
}

trap "hang_up" 2

echo 'Activating WWAN'
sudo modprobe -r airprime usbserial
sudo modprobe usbserial vendor=0x1199 product=0x0220
sudo modprobe airprime
sleep 0.5
sudo wvdial

One-line web server in Ruby


# From: http://www.ntecs.de/blog/articles/2008/02/09/the-worlds-smallest-webserver
# Author: Michael Neumann
# ... point your browser to http://localhost:3125/etc/motd

ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_<<"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'

Sort last 1000 shell commands by popularity

  history 1000 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

Bashy Perlness for generating Favicon text for OpenSearches

echo -n '<Image width="16" height="16">data:image/xicon,' ; perl -ne 's/(.)/"%".unpack("H2",$1)/egs; print' ~/Desktop/favicon.ico ; echo '</Image>' 

Changing the Bash prompt

The following shell command changes the bash prompt from this "james@cryton:~/projects/pear_housekeeping2/housekeeping$" to this "james@cryton> "
export PS1="\n\u@\H>"


I tried the above example on Ubuntu 7.10.

Reference:
Tip: Prompt magic [ibm.com]
Bash Prompt HOWTO [tldp.org]

Finding your WAN IP address

My server sits behind a NAT router, so finding out my public IP address is a non-trivial task. I can use curl to poll checkip.dyndns.org for my current address:
curl -s checkip.dyndns.org

The current IP check returns the information in this format: <html><head><title>Current IP Check</title></head><body>Current IP Address: 216.239.39.99</body></html>

Using cut, I can extract just the information that I need:
curl -s checkip.dyndns.org|cut -d ":" -f2|cut -d "<" -f1

That produces something a bit more readable: 216.239.39.99

-------------------------
This article snippet was copied from My sysadmin toolbox [linux.com] while I was googling for 'apt-cache search dyndns'.

Auto download pdf from www.estado.com.br

// This script downloads all pdfs from http://jpdf.estado.com.br
// Note, you must subscribe this newspaper in order to download the pdfs
// You must have wget installed

export http_proxy="http://10.1.1.1:8000"

cookie='sMkjwKA67H8FDcsZX5'
dd=`date +%d`
mm=`date +%m`
yyyy=`date +%Y`

index="http://jpdf.estado.com.br/menupdfi.php?E=SP&D=$dd/$mm/$yyyy&A=/estadopdf/sp/paginas/$yyyy/$mm/$dd/A01.pdf"

rm index.txt
./wget/wget -nc -k -S -U Mozilla --proxy --header "Cookie: User=$cookie " -O index.txt $index
if [ ! -f index.txt ]; then exit 1; fi

l=`gawk  'BEGIN {FS="\""} /option VALUE="\/estadopdf/ { print $2 }' index.txt`

for x in $l; do 

	# Ignora os classificados
	if [ ${x%01.pdf} -eq "Cl" ]; then continue; fi
	# Ignora o Guia
	if [ ${x%01.pdf} -eq "Q" ]; then continue; fi

	y=http://jpdf.estado.com.br${x%01.pdf}
	i=1
	flag=0
	
	while [ $i -lt 40 ]; do
		filename=`printf "%s%02d.pdf\n" $y $i`

		echo "=================================================================="
		echo $filename
		echo "=================================================================="
		./wget/wget -P estado -nc -k -S -U Mozilla --proxy --header "Cookie: User=$cookie " $filename
		if [ $? -eq 1 ]; then
			let flag=flag+1
			if [ $flag -gt 1 ]; then 
				flag=0
				echo "Proximo caderno..."; break;
			fi 
		fi
		
		sleep 1
		let i=i+1
	done
done

Adding new files to your github repository

Here is a set of instructions to apply new files to my github repository which is called projectx.

Before you start make sure you don't already have the repository name listed as a directory in the local current directory.

1) copy the remote repository to your local machine
syntax: git clone [uri] # eg.
git clone git@github.com:jrobertson/projectx.git

1.5) cd into the newly created local repository eg.
cd projectx

2) copy the local file to the local repository directory
eg.
cp ../projectx2/feed.rb .

3) add the local files to the local repository
syntax: git add [file] # eg.
git add feed.rb

4) Inform the git system that you have completed the required changes for this session.
git commit -a # add a message associated with this file revision

5) copy the new local repository files back to the remote repository.
git push # updates the changes back to the server

Note: The text with the square-brackets should be replaced with your own values.

Reference: A tour of git: the basics [cworth.org]

Twitter and Jaiku from the command line

The following instructions make it easy to post to Twitter and Jaiku from the command line. The instructions were copied from the article "Ubuntu Unleashed: Howto Twitter From the Command Line in Ubuntu!" [ubuntu-unleashed.com] and modified to post via Rorbuilder's ProjectX API.

sudo apt-get install curl

sudo gedit /usr/bin/jaitwit

Now Paste this in gEdit and simply replace "YourUsername" with your username and "YourPassword" with your twitter passwd, then replace the Jaiku variables (YourUsername, YourPassword, YourCity, YourAccessKey) and ctrl-s to save, then alt-F4 to exit!
curl http://rorbuilder.info/api/projectx.cgi?xml_project=%3Cproject%20name=%22micro_blog%22%3E%3Cmethods%3E%3Cmethod%20name=%22post2jaiku%22%3E%3Cparams%3E%3Cparam%20var=%22user%22%20val=%22YourUsername%22/%3E%3Cparam%20var=%22msg%22%20val=%22`echo $@|tr ' ' '+'`%22/%3E%3Cparam%20var=%22location%22%20val=%22YourCity%22/%3E%3Cparam%20var=%22apikey%22%20val=%22YourAccessKey%22/%3E%3C/params%3E%3C/method%3E%3Cmethod%20name=%22post2twitter%22%3E%3Cparams%3E%3Cparam%20var=%22user%22%20val=%22YourUsername%22/%3E%3Cparam%20var=%22msg%22%20val=%22`echo $@|tr ' ' '+'`%22/%3E%3Cparam%20var=%22password%22%20val=%22YourPassword%22/%3E%3C/params%3E%3C/method%3E%3C/methods%3E%3C/project%3E -o /dev/null
echo Message Sent!

Then chmod for exec privileges:
chmod +x /usr/bin/jaitwit

Then from the CLI type jaitwit followed by your message.
jaitwit "message here without the quotes"

Using Espeak

Espeak is a Text-to-speech program which I have used on Ubuntu. A sample of the audio can be found from my twittergram [twittergram.com]. Note: I made a few spelling mistakes in the audio, listen and tell me if you can hear them.

espeak -f hello_jr -s 120
« Newer Snippets
Older Snippets »
Showing 1-10 of 154 total  RSS