-1

A script to authenticate to a Cisco Systems firewall needs to be converted from perl to Python.

This perl script needs to be converted to a python equivalent.

#!/usr/bin/perl

$login=$ARGV[0];
$pass=$ARGV[1];

# load necessary perl modules
use HTTP::Request::Common;
use LWP::UserAgent;

$url="https://<ip>/netaccess/loginuser.html";

my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });

# POST credentials

my $res = $ua->request(POST "$url", [username => "$login", password => "$pass", sid => "0"]);
$res->is_success or die "Failed to POST credentials to '$url': ", $res->status_line;
#!/bin/python
import requests
import urllib3
import sys

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

session = requests.Session()

payload = {'sid' : '0', "username" : sys.argv[1], "password" : sys.argv[2]}

r_post = session.post("https://<ip>/netaccess/loginuser.html",
   data=payload,
   verify=False,
   )

print(r_post.text)

The result is actually not the same. I looks like python is sending this as a parameters in the URL, while with perl this is in the form.

UPDATE :

The page that I try to use is :

</style>
</head>
<body bgcolor=white alink="#333366" vlink="#333366" link="#333366">

<script language="JavaScript1.1">
function doRefresh()
{
    location.replace("/netaccess/connstatus.html");
}
</script>

<FORM method=post ACTION="/netaccess/connstatus.html">

<INPUT type=hidden name=sid VALUE="0">



<h3>Network User Authentication</h3><p><p>Network User Authentication is <i>required</i>.</p> <table border cellpadding=5><tr><td><input type=submit width=120 style="width:120" name=login value="Log In Now"></td><td><b>You are not logged in.</b><br>User IP: <br></td></tr></table><script language="JavaScript">document.forms[0].login.focus()</script>

</FORM>
</body>
</html>
8
  • 1
    Tip: It's not safe to pass passwords as arguments. Other users on the machine can see them. Commented Jul 17, 2019 at 8:25
  • Please elaborate on "The result is actually not the same." The code looks okay but perhaps you need some special form encoding or something. Commented Jul 17, 2019 at 8:32
  • Possible duplicate of How to send a "multipart/form-data" with requests in python? Commented Jul 17, 2019 at 8:33
  • Both LWP::UserAgent and requests use the application/x-www-form-urlencoded MIME by default. So I've no idea what is "not the same". Commented Jul 17, 2019 at 8:39
  • Re "I looks like python is sending this as a parameters in the URL", For a POST request? I don't know Python, much less this particular module (or whatever Python calls them), but that seems unlikely. How did you reach that conclusion? Commented Jul 17, 2019 at 9:53

2 Answers 2

1

I was trying to do the same from similar perl script to Python . In the end , I managed to get it to work by first doing GET and then POST

url = "https://ip_address/netaccess/loginuser.html"

auth = {"username":user ,"password": passw, "sid": 0}

login = requests.Session()

req_login = login.get(url, verify=False)
send_login = login.post(url, data=auth, verify=False)

If you want to authenticate periodically before session timeout , you need to add this before req_login :

 status_url = "https://ip_address/netaccess/connstatus.html"
 logout = {"sid": 0, "logout": "Log+Out+Now"}
 req_logout = login.post(status_url, data=logout, verify=False)
Sign up to request clarification or add additional context in comments.

Comments

0

The python program works fine. I tested on requestbin, have a look at results here, by creating a bin on request bin, and looking at the results of both programs.

On the other hand, the form you pasted has a different URL for the form submit (/netaccess/connstatus.html).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.