Posts Tagged ‘automation’
Posted on April 26, 2010 - by Sarath
Send free SMS from commandline
Disclaimer: My intention of writing this post and publishing the code that uses a proprietary service is not to provide a derivative work based on way2sms.com, but for educational purpose only. The code given below clearly illustrates how python like intuitive programming language can be used to create interesting interactive web based applications. Use the following code for educational purpose only.
Last few days I had negative balance on my phone and I was very lazy to go out for a recharge. I couldn’t send any sms due to underbalance. So I had to rely on Way2SMS.com for free sms. The web interface with lots of ads and stuff irritated me. I thought it would be great if I have some utility/command that can perform the sms sending task for me
So I wrote a python script for sending free sms from commandline. I am enjoying it.
Here it is. Have a try
Instructions:
1. Go to www.way2sms.com, Create an account
2. Create a text file ~/.smsconf and write following lines in it
password=password_you_registered
3. Copy the following code to sms_send.py
#Author: Sarath Lakshman ( sarathlakshman [at] slynux.com )
import urllib2,urllib,re,os
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor())
customer_id=""
first_time=True
configs={}
config_handle = file(os.path.expanduser('~/.smsconf'),"r+")
config_lines = config_handle.readlines()
if len(config_lines) > 2:
first_time=False
for line in config_lines:
splits = line.strip().split('=')
configs[splits[0]]=splits[1]
customer_id_regex = re.compile("custfrom[0-9]*")
success_re = re.compile("Message has been submitted successfully")
print "Logging in"
login=urllib.urlencode({'username':configs["username"], 'password':configs["password"],'login':'Login'})
opener.open("http://wwwo.way2sms.com//auth.cl", login)
if first_time:
print "First run: Pulling customer identity..."
handle=opener.open("http://wwwo.way2sms.com//jsp/InstantSMS.jsp?val=0")
data=handle.read()
customer_id=customer_id_regex.findall(data)[0]
config_handle.write("custid=%s" %customer_id)
else:
customer_id=configs["custid"]
config_handle.close()
mobno=raw_input("Enter mobile no: ")
message=raw_input("Enter message: ")
print "Sending.."
send_msg=urllib.urlencode({'HiddenAction':'instantsms', 'Action':customer_id,'MobNo':mobno,'textArea':message})
handle=opener.open("http://wwwo.way2sms.com//FirstServletsms?custid=",send_msg)
if len(success_re.findall(handle.read())) == 1:
print "Message has been submitted successfully"
else:
print "Could not complete your request"
4. Try it out.
Logging in
Enter mobile no: xxxxxxxxxx
Enter message: cool
Sending..
Message has been submitted successfully
Looking forward to your comments
TODO: Here is your task. Write a GTK/Qt frontend for this.
Posted on January 5, 2010 - by Sarath
Asianet dataline internet autoconnect script
In my hostel, we use asianet dataline internet connection. The connection is established through a webpage authentication and it needs to keep that webpage always open in the browser to get the stable connection. Once we login and releave that page, after five minutes it will require a re-login. Its a real hurdle to keep a page open in the browser for internet authentication.
I just analyzed the http requests from that page and automated using the curl utility.
Here is the script it feel, it would be useful for many.
customer=CNXXXX
password=XXXXXX
if [ -n "`ifconfig | grep -A 1 wlan0 | grep "inet addr"`" ];
then
curl https://mwcp-ekm-04.adlkerala.com:8001 --data "auth_user=$customer&auth_pass=$password&accept=Login" &> /dev/null
echo "Connected :)"
while true
do
sleep 300
curl https://mwcp-ekm-04.adlkerala.com:8001 --data "alive=y&un=$customer" &> /dev/null ;
done
else
echo "Wifi not connected";
fi
Copy down the above script in a text file and rename to internet.sh
slynux@slynux-laptop:~$ ./internet.sh
Connected :)
This script has an initial check for ethernet interface. Here it is given Wifi interface, wlan0. If you use a LAN for connection change wlan0 to eth0 and the message as well

Solve real-world shell scripting problems with over 110 simple but incredibly effective recipes.

