6

I am trying to send sms through Way2sms using Perl LWP. The login part is being successful, after which I save the cookies to a local file. The welcome page after being logged in shows a Send SMS link, clicking on which one is redirected to another page with two inputs for mobile number and sms text and a button for submitting and sending the sms. Firebug reveals the page structure as shown in the figure. From the Iframe url and the form's action attribute, I constructed the form action's absolute URL and submit the form accordingly, with the cookie stored in the file. However, the sms isn't sent. What I am doing wrong here? The code is as follows. (The name attributes for the two text inputs are correct, taken by observing the source code in Firebug, although that's not included in the image)

use LWP::UserAgent;
open f, "> way2sms.txt";
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new(
file => "cookies.txt",
autosave => 1,
);

my $ua = LWP::UserAgent->new(
    agent =>
      'Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
    cookie_jar => $cookie_jar,
);
my $response = $ua->post(
    'http://site2.way2sms.com/contentt/bar/Login1.action',
    {
        username => $user,
        password => $pass,
    }
);

if ( $response->is_redirect ) {
    $response = $ua->get( $response->header('Location') );
    print 5 if $response->decoded_content =~ /Kaustav Mukherjee/i; #prints it, showing that the login is successful
}   
my $smsresp = $ua->post("http://site5.way2sms.com/jsp/quicksms.action",[MobNo=>$mob,textArea=>'Hello World']);

enter image description here

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • 1
    A quick Google search revealed at least two scripts (one in Ruby, one in Python) that make use of Way2SMS from the command line. Are you sure you want to re-invent them? – Moritz Bunkus Aug 26 '12 at 09:27
  • I know the scripts you referred to. The Python script doesn't work. There is a similar Perl script too, in CPAN, but that has got an error in the makefile, so that doesn't install properly. I don't know Ruby, that's why I want to do it myself. – SexyBeast Aug 26 '12 at 09:32
  • There is a limit of 100 sms per day through way2sms. So, if you were thinking it was unlimited, you were wrong. – Prasanth Sep 05 '12 at 08:09
  • I just whipped up a php script that works, for you: https://github.com/gprasanth/PHP-Way2SMS it works. But I don't know perl. Perhaps someone could port? – Prasanth Sep 05 '12 at 11:32

8 Answers8

3

I don't have your login info so that I can't test it for you.

but you can use some Firefox addon like TamperData or HttpFox to get which url is posted and what params are sent.

use Perl to make the same requests as browser and that will be good.

BTW, you can use one $ua to send two requests, no need to create another LWP::UserAgent instance.

Thanks

Fayland Lam
  • 1,016
  • 8
  • 11
3

You don't have to set up a new user agent, because you can simply reuse the previous one. AFAIK, the cookies will be in that user agent already.

Please note: the following is slightly OT.

I had a look at https://metacpan.org/pod/Net::SMS::WAY2SMS

It installed fin on my pc. Maybe you want to try it again? Open a new question here with your concrete problem. Please feel free to notify me, so we may have a look at it.

sid_com
  • 24,137
  • 26
  • 96
  • 187
capfan
  • 817
  • 10
  • 26
  • That's the very one I tried to install, yet it failed. I will try again. In the meantime I will see whether using the same user-agent works. – SexyBeast Aug 26 '12 at 13:33
  • I updated the code. Still it is not sending the sms. I studied the source code of the module you mentioned. Couldn't understand it much, plus he's using Mechanize. I guess it's acceptable to reinvent the wheel if you are trying a different approach! – SexyBeast Aug 26 '12 at 13:48
  • Of course, TIMTOWTDI :) I have to admin, that it is difficult to analyse the problem without credentials. Can you post the html code you are getting, after the first request please? – capfan Aug 26 '12 at 14:12
  • I am getting the correct HTML, that of the welcome page, as evidenced by it containing my name. – SexyBeast Aug 26 '12 at 14:15
  • Why don't you open an account by yourself? It will take 2 minutes, plus unlimited free sms as a bonus! – SexyBeast Aug 26 '12 at 14:16
  • Honestly, I don't want to give my number to just some site I can't use anyway (I have no contacts in india). WWW::Mechanize has a way to dump what you are getting from a request. Your script can probably do it as well. I can only give you the hint to inspect exactly this page. I'm sorry. – capfan Aug 26 '12 at 16:11
  • No problem. FYI, you can use Way2sms to send sms in your country too. It supports 160 countries. – SexyBeast Aug 26 '12 at 16:25
  • Can anyone help me please? I am still stuck! – SexyBeast Aug 27 '12 at 07:43
3

Have you tried using the Perl module Net::SMS::WAY2SMS for sending messages through way2sms.com? It works great for me.

To install try:

C:\> perl -MCPAN -e "install Net::SMS::WAY2SMS"

Here is a code sample for sending SMS:

use strict;
use warnings;
use Net::SMS::WAY2SMS;

my $sms = Net::SMS::WAY2SMS->new(
    'user' => 'user_name' ,
    'password' => 'secret_password',
    'mob' => ['1234567890', '0987654321']
);

# multi line sms
$sms->send(q[testing
sending
sms]);
Ashish Kumar
  • 811
  • 4
  • 8
  • Yeah I know (read the comments for the answer above). I am trying do it through `LWP`, this does it through `Mechanize`. – SexyBeast Aug 26 '12 at 14:45
  • not working now gives following error C:\Users\manish\Desktop>perl test.pl Error GETing http://wwwl.way2sms.com/jsp/InstantSMS.jsp?val=0: Not Found at lib/ Net/SMS/WAY2SMS.pm (autosplit into lib/auto/Net/SMS/WAY2SMS/send.al) line 113. – pkm Jan 24 '14 at 17:48
3

Can't really see what's wrong but here are the things I'd start looking at:

  • Check your real post request from the browser. Make sure javascript or something else isn't adding a variable not visible in the html.
  • Tripple check that MobNo and textArea are really the only needed parameters. Often the submit button is checked for instance, to see what action was done.
  • See what output you get when doing your post. Does it say something about a failure?
  • Try to make the post directly from your browse (there are FF plugins for that) to make sure you're not busted on the lack of http-referrer

I've done a lot of scraping/pretending to be a browser and it's often very tedious. People add "security" checks. On common is that require you to post back a session variable for instance.

Good luck!

*edit: The site requires you to have a valid +91 number to register so I can't login and try for myself. Considering the amount of ads on that page do I really think they are adding some sort of variable since they live off ads and you're trying to avoid them.

Jon
  • 188
  • 1
  • 8
1

You are not including all the POST fields required

MobNo=
textArea=
HiddenAction=instantsms
login=
pass=
Action=

These all need to be included. There is this script in PHP that definitely works and you can refer to.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • I have absolutely no idea about Curl. Can you please elaborate what line 113-122 does? – SexyBeast Sep 05 '12 at 12:22
  • It looks through the response of your login page, the one you have in `$response->decoded_content` and finds a match for the action string. – Anirudh Ramanathan Sep 05 '12 at 12:48
  • I did, there were some matches, but none regarding an `id` being assigned a value of `action`, they all dealt with classes named `action`. – SexyBeast Sep 05 '12 at 12:51
  • Can you tell me how to get the value of the `value` clause for the element with `id` of **Action**? – SexyBeast Sep 05 '12 at 13:48
1

WORKING SCRIPT: (Tested on 6/Sep/2012 3:00 PM GMT+5:30)

    use LWP::UserAgent;
    use HTTP::Cookies;

    my $ua = LWP::UserAgent->new(agent=>"Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1");

    $ua->cookie_jar({ file => "/absolute/path/to/cookies.txt", autosave => 1 });
    # if windows, use \ instead of /

    # $ua->proxy(['http', 'ftp'], 'http://localhost:9666/');

    # below line seems to have done the trick
    push @{ $ua->requests_redirectable }, 'POST';

    my $response = $ua->post(
            'http://site5.way2sms.com/Login1.action',{
            "username" => "1234567890", # set your username
            "password" => "passwd0123", # set your password
            "userLogin" => "yes",
            "message" => "",
            "mobileNo" => "",
            }
    );

    if($response->is_success && $response->decoded_content =~ /Logout/i){ # you can have your name too in place of Logout
            print "Logged in!\r\n";
    }

    my $mob = "1234567890"; # mobile to send message to
    my $mes = "Hello! 123."; # message

    my $smsresp = $ua->post(
            "http://site5.way2sms.com/quicksms.action",
            {
                    'Action' => 'dsf45asvd5',
                    'HiddenAction' => 'instantsms',
                    'catnamedis' => 'Birthday',
                    'chkall' => 'on',
                    'MobNo' => $mob,
                    'textArea' => $mes,
            });

     if ($smsresp->is_success && $smsresp->decoded_content =~ /Submitted/i) {
         print "Sent!\r\n";
     }

P.S. I wish I could get that expired bounty O_o :)

Prasanth
  • 5,230
  • 2
  • 29
  • 61
  • Tough luck, I tried exactly the same thing, not working. Probably I am neglecting some of the CURL configurations. – SexyBeast Sep 05 '12 at 12:43
  • can you set referer? you must set it to `http://site5.way2sms.com/jsp/InstantSMS.jsp` – Prasanth Sep 05 '12 at 12:48
  • @Cupidvogel EDITED My post see. you were making request to `/jsp/quicksms.action` it should be just `/quicksms.action` please try now. – Prasanth Sep 05 '12 at 12:52
  • Yeah Perl provides an option to set the referer. I just did, still no result! – SexyBeast Sep 05 '12 at 12:56
  • I edited my post while you were setting referer. You were making request to wrong url. Please try my code again. – Prasanth Sep 05 '12 at 13:01
  • No no, i didn't copy your earlier code. Your current code is the one I am trying (without the `jsp` bit in the URL), still no result. – SexyBeast Sep 05 '12 at 13:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16300/discussion-between-goldenparrot-and-cupidvogel) – Prasanth Sep 05 '12 at 13:04
  • Nah, still not working! I mean, the login is okay, but the sms isn't being sent as usual. No issue with username and passwords, I have checked them 1000 times. – SexyBeast Sep 06 '12 at 10:14
  • 1
    No problem, I am awarding you the bounty, just stick with me through this problem! – SexyBeast Sep 06 '12 at 10:16
  • well it's working surely for me. may be someone else can confirm it too? something might be wrong with the number you are trying to send the message to? registered with [dnd](http://site5.way2sms.com/jsp/dnd.jsp) perhaps? – Prasanth Sep 06 '12 at 10:18
  • No, you take my number (9051457277. It's insecure to give my phone number in public, but I am too determined to sort this out!) and send me a sms through your script, let's see whether it comes to me! – SexyBeast Sep 06 '12 at 10:35
  • sent. can we continue over chat? http://chat.stackoverflow.com/rooms/16300/discussion-between-goldenparrot-and-cupidvogel – Prasanth Sep 06 '12 at 10:57
0

Well

  1. Way2SMS.com is changing the value of hidden parameter 'catnamedis' monthly once.
  2. Way2SMS.com is adding new hidden parameters less frequently, like now they have added new parmeter called 'token' which varies depending on the user, and the same with 'Action' hidden parameter which also varies depending on the user.

So a script works for someone, does not works for other, because of this changing values, You have to manually login into your way2sms.com account in a real browser and inspect element and find the value for Token and Action parameter for your account (this value varies for every user).

Here is my Python Script through which I am able to make it achieve. You can use it as a reference for your Perl program

Note:- Before trying out my script, change the value of the variable 'act' in the script according to the value of Action parameter for your account, that you can find with the inspect element or Firebug.

Prashere
  • 167
  • 1
  • 1
  • 7
0

An answer here using Python

To send sms using way2sms account, you may use below code snippet.

Before that you would be required to create an API Key from here

import requests
url = "https://smsapi.engineeringtgr.com/send/"
params = dict(
    Mobile='login username',
    Password='login password',
    Key='generated from above sms api',
    Message='Your message Here',
    To='recipient')

resp = requests.get(url, params)
print(resp, resp.text)

N.B: There is a limit of approx 20 sms per day

Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34