Myopenhab connection state

Thanks again for your help.
the print returns the login page.
So, you mean it’s different?

I will send you the file in a PM soon.
And I will look into both - the German and English site to be on the safe side :wink:

That’s awesome.
I start to understand what your script is doing :slight_smile:

EDIT:
Using the shell to try it step by step works until I hit submit :frowning:
(the feature of generating the script of the manually executed steps is brilliant!)

The error refers to this part
my $res = $self->agent->submit;
in

/usr/share/perl5/WWW/Mechanize/Shell.pm

@Wolfgang_S Is it maybe a problem, that the form shows UNDEF?

https://myopenhab.org/login>form 1
POST https://myopenhab.org/login
  _csrf=xxxxx (hidden readonly)
  username=                      (text)
  password=                      (password)
  submit=<UNDEF>                 (submit)

When I fillout user and password like 'password' the error does not appear with click submit.
However creating a script out of this input, leads of course to ''password''

So, could it be possible that special characters cause the issue?
(my password contains a & and the email-adress of course @)

EDIT II:
If I replace ‘@’ with ‘%40’ in my email / password the following error disappears:
Error GETing https://myopenhab.org/: Internal Server Error at ./MyOHCld_State.pl line 20.

However, I get new errors:

Use of uninitialized value $status[0] in substitution (s///) at ./MyOHCld_State.pl line 27.
Use of uninitialized value $status[0] in substitution (s///) at ./MyOHCld_State.pl line 28.
Use of uninitialized value $status[0] in print at ./MyOHCld_State.pl line 30.

Which is possibly related to the fact, that the login did not succeed.
I can see this when running the shell manually, because after submitting, I am still on /login and cannot open / get /events?source=openhab

Is it possible that submit does not work, because there are two submit on the same page (log in and register confirmation)

Both buttons look the same and I don’t know how to “click” / identify the right one:
Sign in:
<button name="submit" type="submit" class="btn" id="submit">Sign in</button>

Register:
<button name="submit" type="submit" class="btn" id="submit">Register</button>

In case I remeber right you can enter forms when you are in the Mechanize shell.This should show both forms. It is possible to select one of them ( should be described in the articles ).
Normally submit buttons have a name or identifier that you can use to ‘click’ on the correct one.
But I think they are in different forms.

In case you are behind the submit you again can use the print content statement to check what was sent back by the sever. It should contain the Online / Offline part. If that is not in or the regular expression that is used in the parsing part does not match then this will end up in messages saying that something is undefined. Using the print statement you should be able to check if the login/submit was successfull or not.

That’S correct.
Sign in is in form 1 and Register is in form 2.
I have succesfully gone into form 1 (in shell), used fillout and the form itself showed the right credentials when calling the form 1 again.

However - as you stated above - the submit is not successful, so the REGEX does not work.

So I am wondering how to ensure that the submit button from form 1 is used.
How to specify the button with like:
$agent->click(value => "Sign in");

I don’t get it wo work (in the shell)

I can use fillout to update the username and password:

https://myopenhab.org/login>form 1
POST https://myopenhab.org/login
  _csrf=NPi3N3Ul-NrkTbcFYXfujfEETTpANipOsguw (hidden readonly)
  username=xxxxxx           (text)
  password=yyyyyy           (password)
  submit=<UNDEF>            (submit)

with “click submit” it’s send somewhere, because the filled-in data is gone.
But I can not go beyond login (it just goes back to login :frowning: ):

https://myopenhab.org/login>get https://myopenhab.org/events?source=openhab
Retrieving https://myopenhab.org/events?source=openhab(200)
                                                                                                                                  https://myopenhab.org/login>form 1
POST https://myopenhab.org/login
  _csrf=xxxx (hidden readonly)
  username=                      (text)
  password=                      (password)
  submit=<UNDEF>                 (submit)

I also did several trials after I had registered an account at myopenhab.org.
I remember that in the past I got such a internal server error in case some headers in http are not set as the server expects them i.e. if the accepted language is not set.
So I had a look to what the browser send using firefox and browser plugin.
Setting these headers in the perl script did not help either.

What could be done to further inspect the headers would be to use a web debugging proxy like burp, fiddler, zap. As it’s a https server this might not work.

An other way - which should work as a full browser is running in the background; thus more system resources are being used - is to run a selenium script. Selenium is a framework that can be used for automated browser / web testing.

Thank you very much for your strong support.
I will check out your suggestions to see if this is within my range of capabilities. :wink:

This topic in general is not a high priority, but I assume that it would be interesting for others as well to know if myopenhab.org is correctly connected the openhab instance.

Again, Thanks for your help!

Try to get the status of your instance by using this python script.

#!/usr/bin/python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup



account = "myaccount@nobody.com"                                                # account name
secret = "ThisIsNotMyPassword"                                                  # password
status = "Unknown"                                                              # default value

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")

#browser = webdriver.Firefox()                                                  # use this to remote control firefox
browser = webdriver.Chrome( options=chrome_options )                            # use this to remote control chrome in headless mode

browser.get("https://myopenhab.org")                                            # navigate to this page
assert "openHAB Cloud - Home" in browser.title                                  # wait until title is loaded

username = browser.find_element_by_id("username")                               # find the username entry field
password = browser.find_element_by_id("password")                               # find the password entry field

username.send_keys( account )                                                   # fill the fields
password.send_keys( secret )

browser.find_element_by_xpath("//*[contains(text(), 'Sign in')]").click()       # sign in
browser.get("https://myopenhab.org/events?source=openhab")                      # navigate to status part
content = browser.execute_script("return document.body.innerHTML;")             # get the page's content

soup = BeautifulSoup( content, 'html5lib' )                                     # parse it with BS4
for a in soup.find_all('a'):                                                    # find all anchors ( a href )
        if (( a.text ==  "Online" ) or ( a.text == "Offline" )):                # find status ( Offline / Online )
                status = a.text

print( status )                                                                 # return the status
browser.quit()                                                                  # close the browser

It is not foolproof yet but should work basically work. Tried it with a test account ( always returns Offline ).

1 Like

Thanks a lot.
I will give it a try.

By the way:
I also played around with mechanize with pylon, because the documentation seems to be better and forms (seem) to be handled slightly diffferent (but that’s just a guess, because I am neither familiar with perl nor python :wink: