42 users online (0 members and 42 guests)  

View Poll Results: Can this be done

Voters
7. You may not vote on this poll
  • easily

    2 28.57%
  • could be

    4 57.14%
  • doubt it

    0 0%
  • no chance in hadees

    1 14.29%

  Results 1 to 6 of 6

Related

  1. Replies: 1
  2. Help with my perl script    Forum: CGI Perl Forum
    Replies: 1
  3. Please Help me (Perl Script)    Forum: CGI Perl Forum
    Replies: 1
  4. How to make a nms by Perl?    Forum: CGI Perl Forum
    Replies: 2
  5. cgi script/perl problem    Forum: CGI Perl Forum
    Replies: 4
  1. #1
    jdouglas's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2004
    Posts
    1

    Make a perl script log into a site

    Hello,

    Here is my problem. I sometimes help program alarm panels for my employer. The process just to login requires me to use a web browser to log into a website. Once I am logged in it gives me a port number. Then I log into a server via ssh and enter this port number. I was trying to make a shell script that uses lynx to automate this process a bit. Lynx will not work and I believe this is because the website is in jsp. I asked around and a buddy told me to try a perl script. I did some research and found this perl module "HTML::FillInForm" but i do not know how to make it work. I have never written a perl script.

    Here is the form in question. I notice that the sessionid changes each time as I presume it should.

    <form name="loginForm" method="POST" action="/portsecure/do/login;jsessionid=C4A9F9B5048491A7F0F85CE9D4F05DF4">
    <table>
    <tr>
    <td><b>User ID:</b></td>
    <td><input type="text" name="id" value=""></td>
    </tr>
    <tr>
    <td><b>Password:</b></td>
    <td><input type="password" name="password" value=""></td>
    </tr>
    <tr>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr></table>

    So how do I make a perl script to login to this site enter the username and pass and neatly echo the port number to me on my console? It would be nice to automate this becasue the next step is to make a shell script "once I have the port" to automatically spawn ssh and enter it all by itself. Can it be done? Any help would be greatly appreciated. Thanks

  2. #2
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Wow. Wicked question!

    Hmm. I am not a Perl programmer, so I cannot give you code examples, but I think I can point you in the right direction.

    This will be a two-step process, mainly because we need to get the sessionid before we attempt login.

    What your going to have to code is a mini-browser, in effect. First, it makes a HTTP GET request to the correct address. This should return, basically, the header information, which we can ignore, and the HTML of the page itself. All we want from this output is the session ID. You will need to parse this from the output.

    Once we have the session ID, we can then do a HTTP POST request to

    /portsecure/do/login;jsessionid=SESSIONID

    Again, this should return HTML Including the port you need. This can then be parsed out and you can go onto the next step.

    Good luck with it, please let us know of progress as this is a very interesting little project.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  3. #3
    dlongnecker's Avatar
    Tired Student...(Z_z)

    Status
    Offline
    Join Date
    Aug 2004
    Location
    Wichita, KS
    Posts
    86

    Talking Wheee!

    Hehehe... this actually looks like a fun project. Then again, I'm a geek. Please forgive a bit of rusty coding... I don't have a unix box around to try this on as I type.

    Here's something you could try. Since you're running Lynx; I'm 'assuming' you're on a *nix-based platform and running Bash.

    A. Capturing your session ID:

    Lynx output can be dumped to the console (or piped) using the --dump command.

    Code:
    lynx --dump http://myurl.com
    So, to grep (find) that, you could...

    Code:
    lynx --dump http://myurl.com | grep jsessionid
    That would toss the session ID line to your console, or into a text file.

    Code:
    lynx --dump http://myurl.com | grep jsession > jsession.tmp
    or into a variable (I think this would work, would need tested)...

    Code:
    var_jession=lynx --dump http://myurl.com | grep jsession
    That should capture the approprate session ID into a text file or variable, which we'll use later.

    B. Plugging in the user name and password.

    This is going to be kinda tricky.. and any of you Bash programmers, jump in.

    You could create the HTML in a preset file, e.g.:

    htmllogin1.tmp

    Code:
    <table>
    <tr>
    <td><b>User ID:</b></td>
    <td><input type="text" name="id" value="PUTYOURUSERNAMEINHERE"></td>
    </tr>
    <tr>
    <td><b>Password:</b></td>
    <td><input type="password" name="password" value="PUTYOURPASSWORDINHERE"></td>
    </tr>
    <tr>
    <td><input type="submit" name="submit" value="Submit"></td>
    </tr></table>
    <SCRIPT LANGUAGE="Javascript">
    <!--
    document.forms("loginForm").submit()
    //--></SCRIPT>
    Not sure on the JavaScript... found it hunting the web that will automagically submit a form on loading the page.

    Then:

    Code:
    cat jsession.tmp > login.htm
    cat htmllogin1.tmp >> login.htm
    lynx --dump http://login.htm
    That should put the session variable at the top and create the opening form statement, then append the rest of the completed HTML text below it and, if the JavaScript works, automagically submit the form. You might not be able to use the --dump command to dump it to the terminal, but you could just use lynx and exit out as soon as you have your port number.

    Hmm, in any case; that'd be a possible starting point. Let me know where and what breaks and I'll load up a quick VM with Gentoo or something and see if I can give you cleaner, more stable code. I'm sure I have an O'Reilly Bash Programming book around here somewhere.... now to find it!

    Good luck!

    -David

    Edited note: One thing to consider is the security of that password and username. You'll have it in plain text somewhere on your machine--weight the pros and cons.

  4. #4
    sloot's Avatar
    New User

    Status
    Offline
    Join Date
    Nov 2004
    Posts
    1
    this is kickass and close to something I need too.

    I don't think that javascript is working though ... at least for me, it didn't work.


    I don't need the session ID that was in this example, so I just hardcoded the html (for now... later, I'll write an awk script to generate the .html based on incoming information)

    how else can I kickoff a HTTP form POST without using a web browser?

  5. #5
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Using Perl or PHP you can open a Socket, which is basically a connection.

    Through this socket you can then make a standard HTTP request and reap the results through the socket.

    Most programming languages can handle sockets. Your mention of awk leads me to suggest Perl or C in your case.
    Last edited by HTML; 03-09-2012 at 09:42 PM.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  6. #6
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    Haha, I forgot. You can also telnet port 80 and type in the HTTP commands manually. Good for testing if nothing else.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?



Tags for this Thread