44import  argparse 
55import  requests 
66from  random  import  random 
7- from  time  import  sleep ,  time 
7+ from  time  import  sleep 
88from  bs4  import  BeautifulSoup 
99
1010
@@ -15,29 +15,23 @@ def main(args):
1515    s .verify  =  False 
1616
1717    login_data  =  {
18-         "login" : 1 ,
1918        "username" : args .user ,
2019        "password" : encode (args .pwd ),
21-         "dummy " : current_time () 
20+         "login " : 'true' , 
2221    }
22+     # print("Logging in...") 
23+     login_step1  =  s .post (url , data = login_data )
2324    login_check_data  =  {
24-         "login_chk " : 1 ,
25-         "dummy " : current_time () 
25+         "authId " : login_step1 . text . strip () ,
26+         "login_chk " : 'true' , 
2627    }
27- 
28-     # print("Logging in...") 
29-     s .get (url , params = login_data )
3028    # implicitly wait for login to occur 
3129    sleep (1 )
32-     ret2  =  s .get (url , params = login_check_data )
33-     if  'OK'  not  in ret2 .text :
34-         raise  Exception ("Login failed: %s"  %  ret2 .text )
30+     login_step2  =  s .post (url , data = login_check_data )
31+     if  'OK'  not  in login_step2 .text :
32+         raise  Exception ("Login failed: %s"  %  login_step2 .text )
3533
3634    # print("Login successful, parsing cookie.") 
37-     cookie  =  parse_cookie (s .get (url , params = {"cmd" : 1 }))
38-     # print("Got COOKIE: %s" % cookie) 
39-     s .cookies .set ("XSSID" , cookie )
40- 
4135    ret  =  s .get (url , params = {"cmd" : 773 })
4236    if  ret .ok :
4337        soup  =  BeautifulSoup (ret .content , 'html.parser' )
@@ -69,7 +63,7 @@ def main(args):
6963            output  =  ret 
7064        print (output )
7165    else :
72-         xssid_content  =  soup .find ('input' , {'id ' : 'XSSID' }).get ('value' )
66+         xssid_content  =  soup .find ('input' , {'name ' : 'XSSID' }).get ('value' )
7367        print ("Executing command: Turn %s PoE Port %s."  % 
7468              ('on'  if  args .state  else  'off' , args .port ))
7569        command_data  =  {
@@ -91,19 +85,15 @@ def main(args):
9185            raise  Exception ("Failed to execute command: %s"  %  ret .text )
9286
9387
94- def  current_time ():
95-     return  int (time () *  1000.0 )
96- 
97- 
9888def  encode (_input ):
9989    # The python representation of the JS function with the same name. 
10090    # This could be improved further, but I can't be bothered. 
10191    password  =  "" 
10292    possible  =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" 
10393    _len  =  lenn  =  len (_input )
10494    i  =  1 
105-     while  i  <=  (320  -  _len ):
106-         if  0  ==  i  %  7  and  _len  >  0 :
95+     while  i  <=  (321  -  _len ):
96+         if  0  ==  i  %  5  and  _len  >  0 :
10797            _len  -=  1 
10898            password  +=  _input [_len ]
10999        elif  i  ==  123 :
@@ -119,12 +109,6 @@ def encode(_input):
119109    return  password 
120110
121111
122- def  parse_cookie (cmd_1 ):
123-     for  line  in  cmd_1 .text .split ("\n " ):
124-         if  'XSSID'  in  line :
125-             return  line .replace ('setCookie("XSSID", "' , '' ).replace ('");' , '' ).strip ()
126- 
127- 
128112if  __name__  ==  "__main__" :
129113    parser  =  argparse .ArgumentParser (
130114        description = 'Manage the PoE ports of a Zyxel GS1900-10HP switch.' )
@@ -138,7 +122,8 @@ def parse_cookie(cmd_1):
138122                        help = 'The port number. When querying information, 0 means all ports.' )
139123    parser .add_argument ('--state' , '-s' , dest = 'state' , type = int ,
140124                        choices = [0 , 1 ],
141-                         help = 'Turn the port on (1) or off (0). To query the state, rather than set it, omit this parameter.' )
125+                         help = 'Turn the port on (1) or off (0). To query the state, ' 
126+                              'rather than set it, omit this parameter.' )
142127    parser .add_argument ('--verbose' , '-V' , dest = 'verbose' , action = "store_true" ,
143128                        help = 'Return detailed information when querying the specified port state.' )
144129    main (parser .parse_args ())
0 commit comments