NATION

PASSWORD

Python autologin script

Bug reports, general help, ideas for improvements, and questions about how things are meant to work.
User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Python autologin script

Postby Darcania » Wed Aug 23, 2017 10:51 pm

Just thought I'd share a small Python autologin script I made. It has no external dependencies, so just download and install Python 3 and it should work fine.

It includes a .bat file for executing the script easily on Windows.

Feel free to view the source code here: https://github.com/zephyrkul/autologin
You can download it by git cloning it if you're familiar with git, or by downloading this .zip file and extracting it: https://github.com/zephyrkul/autologin/ ... master.zip

Note that this script locally saves your autologin token (essentially an encrypted version of your password). While people can't log in with it normally, it's possible to log in through the API with it, so keep that file safe.

How to use
  1. Enter your main nation name and maybe even your email when the script prompts you for a User Agent. The more informative your User Agent, the better. The script will add a note that you're using my autologin script.
  2. The script will then continuously ask for your nation / password combinations until you input nothing when it asks for your nation.
  3. The script will then save your data for easy autologin runs in the future. If the script notices that your nation doesn't exist or the password is no longer valid, it will delete that nation from its data.
  4. After the script runs through its saved data, it will ask for nation / password combinations as in step 2 in case you'd like to add more.

Notes
  • As the script runs, it will print out any new notices that nation has, for easily checking up on your nations.
  • If you'd ever like to delete your data, just delete the .json file that the script creates, or put "RESET" (without the quotes) when the script prompts you for a nation.
  • This script follows the Rate Limit of 50 requests per 30 seconds, but it does so naïvely: if it hits 40 requests, it will sleep for 30 seconds.

That should be it. Feel free to let me know of any bugs, etc.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Thu Aug 24, 2017 12:05 am

Changed a few small things.

  1. Banner and issue notices are now ignored when the script outputs notices.
  2. The script no longer erroneously marks nation names with hyphens as invalid.
  3. Removed some debugs.

Thanks for the feedback, Altmoras.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sun Jan 27, 2019 1:49 pm

Since I've seen some confusion and no one decided to talk to me about their issue...

No, the script is not broken because you can't see your passwords. It's still taking your passwords, just not showing them back at you for security. It's like how password fields in your browser replace all the characters with dots - it's still taking the real password, not what it's showing.

That is all.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sun Jan 27, 2019 8:31 pm

In other news I've just pushed a rather large update to this - now it acts more like a command-line based utility (with a menu sparkles) with various requested options. It also has a new format for saving autologin tokens but the script will detect and convert the old format automatically if it finds it.

To update:
  1. Copy nsping.json to somewhere outside the folder.
  2. If you downloaded by cloning the git repo, just run git pull and skip to the last step.
  3. Otherwise, delete the entire folder and download the script again - see the first post for instructions on that. You won't need to install Python again.
  4. Move nsping.json back into the folder.
And you're done.

User avatar
Konar
Civil Servant
 
Posts: 6
Founded: Jun 03, 2015
Corrupt Dictatorship

Postby Konar » Sat Feb 02, 2019 9:12 pm

Bluie here. I tried copy-pasting into the password field, but when I pressed enter, it just told me it got an error. I tried it again, and this time it closed on me. I ran the program and tried to have it list the nations it had, closed on me. Ran it again and tried to run the autologin script, closed again. I could change the user agent, add/edit nations and remove nations in the list. Also, if you try to exit from the add/edit nations script by pressing enter on an empty nation field, instead of going back to the menu, it closes.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sat Feb 02, 2019 11:55 pm

Konar wrote:Bluie here. I tried copy-pasting into the password field, but when I pressed enter, it just told me it got an error. I tried it again, and this time it closed on me. I ran the program and tried to have it list the nations it had, closed on me. Ran it again and tried to run the autologin script, closed again. I could change the user agent, add/edit nations and remove nations in the list. Also, if you try to exit from the add/edit nations script by pressing enter on an empty nation field, instead of going back to the menu, it closes.

Pretty sure I know the root of most of your errors, and I've fixed that issue (there were a few places in the script where it assumed you have a tokens file, but now it realizes when you don't and gives a more user-friendly warning). I'm not sure about every issue, however, so I've also added more robust logging. From now on, if there's an error with the script, it'll be logged to an errors.log file. This should help with further troubleshooting if you get more errors.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sat Sep 14, 2019 8:26 pm

I intended to get around to making this script more friendly to running solely from the command line, but sadly I've been too busy to get to it. So if you do want to run this from the command line without dealing with the menu (for cron jobs, for instance), for now you can use this:
Code: Select all
echo 1 | python3 nsping.py

Or, if you're on Windows:
Code: Select all
echo 1 | nsping.bat

Be sure whatever outer script (e.g. cron) is set to call this has its working directory set to the folder containing nsping.py. You can, naturally, use the cd command for this.

If none of this makes sense to you you probably should stick to running this manually every now and then.
Last edited by Darcania on Wed Sep 18, 2019 11:41 pm, edited 1 time in total.

User avatar
RantSpot
Secretary
 
Posts: 36
Founded: Feb 15, 2010
Civil Rights Lovefest

Postby RantSpot » Wed Sep 18, 2019 1:04 pm

I just put together a quick bash script which I put in the same folder as the Python script and settings files. (named as nsping.sh to remain consistent)
Code: Select all
#!/bin/bash
cd $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )
echo 1 | python3 nsping.py > lastlogin.txt

This does the cd to the script/settings directory. Above you recommended > rather than |, which would just output the command to a file called python3 in the working directory. I did actually > the output to a file in that folder for future review, given that I am using it for a cron job.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Wed Sep 18, 2019 11:42 pm

RantSpot wrote:This does the cd to the script/settings directory. Above you recommended > rather than |, which would just output the command to a file called python3 in the working directory. I did actually > the output to a file in that folder for future review, given that I am using it for a cron job.

My mistake, I misread my crontab. That's what I get for posting shell when I'm distracted

User avatar
RantSpot
Secretary
 
Posts: 36
Founded: Feb 15, 2010
Civil Rights Lovefest

Postby RantSpot » Tue Sep 24, 2019 10:24 am

I've been using this for just a few days, but I discovered that the script was failing about halfway through. I deleted the offending nation from the .tokens file, and upon attempting to re-add, got the following error:
ERROR nsping add_nations 205: undefined entity: line 15, column 33
Traceback (most recent call last):
File "nsping.py", line 183, in add_nations
data = _log(agent, nation, password=password)
File "nsping.py", line 319, in _log
root = ElementTree.fromstring(response.read())
File "/usr/lib/python3.5/xml/etree/ElementTree.py", line 1344, in XML
parser.feed(text)
File "<string>", line None
xml.etree.ElementTree.ParseError: undefined entity: line 15, column 33

I don't have that issue with any other nation. I can log into that nation easily through a browser. The nation's name does not include spaces, and the password doesn't have special characters.

There was also a considerable gap between the 40th login and the 41st. I assume this was an intentional timeout built in.

EDIT And for troubleshooting, the error before I deleted and attempted to re-add the nation:
ERROR nsping run 136: undefined entity: line 16, column 33
Traceback (most recent call last):
File "nsping.py", line 109, in run
_log(agent, nation, autologin=autologin)
File "nsping.py", line 319, in _log
root = ElementTree.fromstring(response.read())
File "/usr/lib/python3.5/xml/etree/ElementTree.py", line 1344, in XML
parser.feed(text)
File "<string>", line None
xml.etree.ElementTree.ParseError: undefined entity: line 16, column 33
Last edited by RantSpot on Tue Sep 24, 2019 10:38 am, edited 1 time in total.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Tue Sep 24, 2019 8:17 pm

Something in NationState's response is malformed XML in some way. Sadly there isn't anything I can do about that. All I can suggest is that you first read all your notices from the browser and try again, and if that doesn't help then all you can do is wait a day or so and try again.

User avatar
RantSpot
Secretary
 
Posts: 36
Founded: Feb 15, 2010
Civil Rights Lovefest

Postby RantSpot » Thu Sep 26, 2019 8:35 am

I manually curled with both X-Password and X-Autologin (separately, of course) and got a perfectly formed XML response with proper headers. Alas, I restarted the machine, got a new notice, and was able to successfully re-add the nation through the automated mechanism. The login process for that nation is now working fine. The issue shall remain a mystery, I guess. It would be hard to troubleshoot further now that the issue has disappeared in the one instance I observed it.

I'm thinking about adding command line options to this script. In addition to CLI-ing the existing options (e.g. -1 to just run the autologin), I am interested in options to request more private shards, to format the output as a single XML document (nesting all the <nation>s into one <nations>, plus an <errors> to hold one <error> per failed login for whatever reason) that is saved as a file (which can then be interpreted/viewed in any number of ways; it would be easy to parse and determine e.g. total current notices or tgs, total errors, etc), to allow users to specify the location of their tokens file, to allow users to specify a passwords file (e.g. nation:pass instead of a token) to use rather than tokens, and maybe some other things. I was initially going to write my own new script using the NationStates library, but I prefer your no-dependencies strategy. I previously worked on a Windows JScript autologin script which was moving in that direction, but I lost a newer version of it with a hard drive crash and I'd prefer advancing this platform-independent option. If you want to keep these bells and whistles out of your script, I can write a script which can do these things and use your settings and token file formats.

User avatar
Androdium
Secretary
 
Posts: 27
Founded: Aug 27, 2019
New York Times Democracy

Postby Androdium » Sat Nov 02, 2019 10:10 am

Could there really be an apply WA setting? It would be very useful for defenders and raiders that need to keep their puppets in check. Also is there a limit on how many nations can be in the database?
The Republic is Eternal! (Wait that sounded super communist. My nation is democratic I swear!!)

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sat Nov 02, 2019 2:31 pm

Androdium wrote:Could there really be an apply WA setting? It would be very useful for defenders and raiders that need to keep their puppets in check.

That isn't supported by the API, so no.
Edit: That said, it's a cool idea, so I requested such an API shard here. If it's made available I'll add a setting to my script.
Androdium wrote:Also is there a limit on how many nations can be in the database?

Your disk space. Though it is worth noting the length of time the script will take. It will take 30 seconds for each group of 40, so it can take a while to run if you have a lot of puppets.
Last edited by Darcania on Sat Nov 02, 2019 2:33 pm, edited 1 time in total.

User avatar
Androdium
Secretary
 
Posts: 27
Founded: Aug 27, 2019
New York Times Democracy

Postby Androdium » Sun Nov 03, 2019 3:30 pm

Darcania wrote:Your disk space. Though it is worth noting the length of time the script will take. It will take 30 seconds for each group of 40, so it can take a while to run if you have a lot of puppets.


Then that would beg the question. how much space does each individual nation take?
The Republic is Eternal! (Wait that sounded super communist. My nation is democratic I swear!!)

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sun Nov 03, 2019 4:33 pm

Androdium wrote:Then that would beg the question. how much space does each individual nation take?

The number of characters in the nation's name, including spaces, plus approximately forty, in bytes, per nation. I can only approximate it due to the storage of autologin tokens.

User avatar
Gorundu
Spokesperson
 
Posts: 180
Founded: May 02, 2019
Left-wing Utopia

Postby Gorundu » Sat Feb 29, 2020 3:43 am

Would it be possible to add an option to revive a CTE nation when the script comes across one, instead of deleting it from the database?
Minister of Communications of The North Pacific
Posts do not represent my region's views unless stated otherwise.

User avatar
Darcania
Spokesperson
 
Posts: 150
Founded: Dec 29, 2014
Ex-Nation

Postby Darcania » Sun Mar 01, 2020 3:41 pm

Gorundu wrote:Would it be possible to add an option to revive a CTE nation when the script comes across one, instead of deleting it from the database?

Not possible.


Advertisement

Remove ads

Return to Technical

Who is online

Users browsing this forum: Destructive Government Economic System, Preusses, Sebero Sree

Advertisement

Remove ads