SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2009-04-20 05:55:35

kurzundknapp
New Member
Registered: 2009-04-20
Posts: 3

HOWTO: Send SMS via Terminal

Hi everybody,

I'm new to this forum and to the CB-community but I also wanted to add a little gift to everyone in sharing one of my ideas that came recently to my head. smile Feel free to give some response and some new thoughts to this little project.

---

In this HOWTO I'll show you how you can send SMS via your Terminal. I try to keep it simple but maybe I fail wink So please tell me what you don't understand.

---

What you need:

- IMPORTANT: You need to know a website where you can send SMS (for free/with costs or with/without login doesn't matter).
For me it was my university's hp where they offer a nice little SMS-service for the students. I'll also explain the little script with this example. Maybe you can use for yourself your mobile-provider's hp or otherwise you search the internet for some cost-free services.

- Newest version of ruby. You can download it in your package manager or simply enter

sudo apt-get install ruby

in your terminal.

- Newest version of mechanize for ruby. Same here: package manager(search for mechanize) or enter

sudo apt-get install libwww-mechanize-ruby

(I also recommend you to download the doc-files for mechanize. There's a simple tutorial which will be very helpful in a short minute)

- Some very basic knowledge in html and programming. cool

---

If you have everything then let's start:

Step 1

Make a new file called "sms.rb" in your home directory. You can do this by typing

gedit "sms.rb"

in the terminal or in another preferred way.

Step 2

Fill it with:

require 'mechanize'
number   = ARGV.shift
message  = ARGV * " "

if message.length > 160 || message.length < 1
  $stderr.puts "Message too short or too long.."
  exit 1
end

agent = WWW::Mechanize.new

page = agent.get('EnterHereTheExactURLtoTheLoginSite')
hp_formlogin = page.form('FormNameOfTheUsernameAndPasswordForm')
hp_formlogin.formFIELDNAMEofthesitefortheloginname = 'YourLoginName'
hp_formlogin.formFIELDNAMEofthesiteforthepassword = 'YourLoginPassword'
page = agent.submit(hp_formlogin)

#ONLY USED IF YOUR SITE REDIRECTS YOU AFTER LOGIN
#page = agent.click page.links.text('EnterExactTextOfTheRedirectLinkFromTheSite') 


page = agent.get('EnterTheExactURLtoTheSMSSendingSite')
#ONLY USED IF THERE'S NO NAME FOR THE FORMFIELD.
# SO IF YOU HAVE ONE, COMMENT OUT THE LINE BELOW AND ENTER THIS hp_formsms = page.form('FormNameOfTheSMSFormWithTheMessageBoxAndPhoneNumber')
hp_formsms = page.forms.first
hp_formsms.formfieldnameofthesitefortheMOBILENUMBERBOX = number
hp_formsms.formfieldnameofthesitefortheMESSAGEBOX = message
page = agent.submit(hp_formsms)

Step 3

Save it wink

Step 4(Big Step, duh!)

So now let's take a closer look at the file. You can see some very weird names for the functions and some nonsense at first look. But this will fade away, I promise. You just have to replace these names with the good ones you find on the site where you will send the sms. You do that in the following way(More experienced users who understand the code can skip this part):

Open up your SMS-Sending-Site (gooood name for that oO). If you have a loginpage go to that one, if not then go directly to the site with the SMS-Send-Box and comment out the formlogin section.
Rightclick on the page and view the page source. This should normally open a long file.
Now search in the editor for "form" and look closely to the parameters after the form tag. There could be for example something like this:

<form action="/translate_t" method=post id="text_form" name="text_form"  onsubmit="this.action = '/translate_t?sl=' + this.old_sl.value + '&tl=' +this.old_tl.value;">

Aha, there we have a name called "text_form".  Copy this name and replace "FormNameOfTheUsernameAndPasswordForm" in the sms.rb with it.
Reproduce this with the following names:

  • EnterHereTheExactURLtoTheLoginSite

  • FormNameOfTheUsernameAndPasswordForm

  • formFIELDNAMEofthesitefortheloginname

  • formFIELDNAMEofthesiteforthepassword

  • EnterTheExactURLtoTheSMSSendingSite

  • (Maybe)FormNameOfTheSMSFormWithTheMessageBoxAndPhoneNumber

  • formfieldnameofthesitefortheMOBILENUMBERBOX

  • formfieldnameofthesitefortheMESSAGEBOX

You can find the 4 last names in the html file of your loginarea(the page with the messagebox for the sms).

Step 5

Now replace "YourLoginName" and "YourLoginPassword" with the ones you normally use to enter the site.

Step 6

If you've done everything correct your file should look somewhat similar like mine:

 
require 'mechanize'
number   = ARGV.shift
message  = ARGV * " "

if message.length > 121 || message.length < 1
  $stderr.puts "Enter it again ^^"
  exit 1
end

agent = WWW::Mechanize.new
page = agent.get('https://webapps.unibe.ch/a3ublogin2/login.jsp?appid=sms4ub')
unibe_formlogin = page.form('tinu')
unibe_formlogin.a3ubloginuser = 'XXXXXXX'
unibe_formlogin.a3ubloginpassword = 'XXXXXXX'
page = agent.submit(unibe_formlogin)

page = agent.click page.links.text('Go forward to: sms4ub')
pp page
page = agent.get('https://webapps1.unibe.ch/sms4ub/send.jsp')

unibe_formsms = page.forms.first
unibe_formsms.natel = number
unibe_formsms.text = message
page = agent.submit(unibe_formsms)

Maybe you recognized that I added "pp page". This command prints out the whole page which mechanize fetched. It's very useful when you debug and look for an error. You can find more to this in the doc for mechanize.

Step 7

Save it again wink

Step 8

Test your sms.rb by entering

ruby sms.rb 00123456789 your message comes here...bllaaaa

. If there aren't any errors, CONGRATULATIONS! You can now send sms via your terminal. Don't overuse it wink and don't let sms.rb meet crontab tongue

Step 9(Fine-tuning and additional comment)

Open up your .bashrc (it's hidden in your home folder) and type at the end

alias sms="ruby sms"

So now every time you want to send a sms you only have to write

sms 00123456789 SPAMSPAMSPAM

and your co-workers will turn red very fast ...

*If you don't have to login a site then you can delete the code for the login:

 page = agent.get('EnterHereTheExactURLtoTheLoginSite')
hp_formlogin = page.form('FormNameOfTheUsernameAndPasswordForm')
hp_formlogin.formFIELDNAMEofthesitefortheloginname = 'YourLoginName'
hp_formlogin.formFIELDNAMEofthesiteforthepassword = 'YourLoginPassword'
page = agent.submit(hp_formlogin) 

---

You can modify this simply script as you want. Maybe you want to send your emails via a terminal like this or maybe you want to translate words in a easy way like this(use for example googletranslate and search for the according formnames). The possibilities are endless...

---

I hope you enjoyed this little HOWTO and please excuse my bad english. I'm not a native speaker as you possibly already noticed roll

Greez from Switzerland

kurzundknapp

(Short question for the mods: Is this the right forum for this or should it be under "Tips, Tricks & Scripts" ? If so please move it to the right place.Thx)

Last edited by kurzundknapp (2009-04-23 23:11:22)

Offline

Be excellent to each other!

#2 2009-04-23 02:37:17

fhsm
#! Junkie
From: New Hampshire, USA
Registered: 2009-01-05
Posts: 445

Re: HOWTO: Send SMS via Terminal

kurzundknapp wrote:

- IMPORTANT: You need to know a website where you can send SMS (for free/with costs or with/without login doesn't matter).


Suggestions on such a site?

Offline

#3 2009-04-23 23:08:32

kurzundknapp
New Member
Registered: 2009-04-20
Posts: 3

Re: HOWTO: Send SMS via Terminal

Mhm, it depends on the country you live in... Try to google some sites using "free sms" or "free sms service yourcountry" as keywords. This way I found some sites for the german-speaking area: http://free-sms-ohne-anmeldung.ch/free-sms-liste.php , http://www.gratis-ecke.de/kostenlos/han … eite1.html and http://server.smsform.de/index.php?i=79 … 69881fe482

On two of these sites are lists to free sms services. Some of them also offer a worldwide service, but I don't know how fast they deliver the sms. Try for yourself which is the best for you.
If you have a mobile then visit the homepage of your provider and look there for a sms service. Usually they provide such a service for they customers.

I hope this helps a bit.

Offline

#4 2011-10-21 05:59:13

Blisk
New Member
Registered: 2011-10-21
Posts: 1

Re: HOWTO: Send SMS via Terminal

I have installed this and trying to work out. But it doesn't work. Is there someone tryed this recently? Is there a better way to sending SMS from command line?

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo