This article will focus on automating the management of YOTA tariffs using curl, OpenWRT and MR-3020. The proposed scripts can be used in another * nix environment with curl installed.
Prologue
It so happened that about a month ago I became the happy (or not) owner of the YOTA LTE modem. In the absence of other alternatives, one has to use it as the main home source of access to the global web.
To distribute the Internet around the apartment (due to the high cost of Yotov devices), it was decided to use the YOTA + TP-LINK MR-3020 modem with
OpenWRT on board. The choice fell on the MR-3020 as a compact and cheapest router with a USB port and support for OpenWRT.
')
At the stock MR-3020 curl will not, not enough space. More specifically, there is not enough space for libopenssll, where the libcrypto library weighs more than 1 MB. In this situation, the easiest way out is to load libopenssll into RAM after loading the router, the installation instructions below. I use an additional usb hub + usb-hdd, which is mounted to rootfs, minidlna, samba and transmission are also installed on the hdd, respectively, for downloading torrents and watching them on TV and facing the problem of lack of space.
It should be noted that setting up OpenWRT to work with the YOTA modem is very simple - you just need to install additional packages for working with USB and the network, as well as add an additional network interface in the settings.
Automation and OpenWRT
In cases where there is an opportunity to jump from tariff to tariff with impunity, questions of automation-optimization arise by themselves. In addition, after reading the
article linx56 "automation itch" (thanks to the author for the term) did not give rest.
The task was as follows - instead of installing the proposed
linx56 script on each home PC, you need to “force” the router to independently switch the tariffs depending on the time of day and day of the week, the benefit of Internet consumption seemed to be quite definite. The above cross-platform Yota script did not meet the requirements for the reason that it uses phantom.js, the installation of which on the stock MR-3020 with OpenWRT is not possible.
Thus, the need for writing a script without using heavy headless browsers has matured. The output was found almost immediately (according to the comments to the above article) - it was decided to use the
curl utility to send requests to the YOTA server.
Initially, curl was installed on the router:
opkg install curl
After that, it was necessary to find out how the browser communicates with the Yota site, namely, which requests with which parameters.
With the help of
FireBug, the first authorization request was caught in my account. The obtained results were sent by curl to the server and ... failure - authorization error !!! I checked the parameters and values ​​- again an error, and again, and again ...
As a result, it turned out that for authorization and for setting parameters in the personal account, the YOTA website actively uses cookies. Fortunately, curl does an excellent job with handling cookies - the issue of authorization has been resolved:
curl -c cook.txt -s -k -L -d "IDToken1=$1&IDToken2=$2&IDToken3=$2&goto=https%3A%2F%2Fmy.yota.ru%3A443%2Fselfcare%2FloginSuccess&gotoOnFail=https%3A%2F%2Fmy.yota.ru%3A443%2Fselfcare%2FloginError&old-token=&org=customer" https://login.yota.ru/UI/Login
Here we are in your account. Again, we use FireBug to intercept a tariff change request. The received parameters and cookies are sent to the server and ... again failure !!!
After analyzing several requests, I was interested in the value of one of the parameters transmitted to the server, namely “ProductID”, each time this value was new after changing the tariff. As it was found out, the ProductID value is returned by the server in the page code and changes every time the tariff changes. The tariff itself is an offerCode parameter, taking values ​​from POS-MA14-0002 to POS-MA14-0022.
curl -b cook.txt -s -k -L -d "product=$pr&offerCode=$OCODE&homeOfferCode=&areOffersAvailable=false&period=&status=custom&autoprolong=0&isSlot=false&resourceId=¤tDevice=1&username=&isDisablingAutoprolong=false" https://my.yota.ru/selfcare/devices/changeOffer
As a result, the algorithm is as follows:
- Log in to your account;
- Go to the page with the "regulator" speed;
- Select the ProductID from the resulting page;
- We set a new tariff.
As a result of the research, a small script was born that can be obtained
here .
The application is simple:
yota.sh <> <> <>
The result of the performance is the established tariff (speed) and the number of days (hours) left on the tariff.
With the help of crontab, the intended mode of Internet consumption was configured, namely:
For working days:
- the inclusion of speed in the morning - during the fees for work;
- off in the morning - when you go to work;
- inclusion in the evening - when you return from work;
- off at night - when you go to sleep;
for weekends - a separate schedule with a late morning start :).
It's been seven days
A week later, it became clear that the proposed method of changing tariffs on a schedule is not entirely effective, if the time of night and morning shutdown speed almost always coincides with the established one, then the switch-on time is often significantly different from the set one, sometimes the Internet is needed earlier, sometimes much later (and if you overslept, you don’t need it at all :)).
The first thought is to change the speed depending on the network load. Having a little thought about the implementation, I found this idea not very successful - firstly, the bandwidth set in the tariff does not always coincide with the actually available bandwidth. You can constantly raise the tariff, but the real band from this will change little and will be constantly loaded - respectively, losses, and secondly - the torrents on the media server will maintain constant high speed around the clock, and so on.
The next thought is to change the speed on and off parameters depending on the presence of “Internet users” in the home network, namely:
- Turn on speed when a stationary PC or laptop (or any other specified device) appears on the network.
- Turn off - when both disappear.
- Turn off - at a specified time (at night), because You can forget to turn off the computer at night :).
In this regard, a small pingsh.sh script was also born, which, by means of the ping command, determines the presence of “necessary” devices on the network, the IP addresses of these devices are listed in the pings file, and the router's dhcp is set to issue »Home devices fixed ip-addresses. If at least one address from the file is present in the network, the speed is turned on, in the absence of all, after 3 minutes, the devices are checked again in the network (in case wi-fi has fallen off), and if no devices are detected, the speed is turned off. In order not to annoy Yot with unnecessary requests, every time when changing the tariff, we set the speed on / off flag.
The use of the
pingsh.sh script
is simple - we enter the necessary ip-addresses of home devices into the pings file, edit the values ​​of login, psw, speedon and speedoff in the script itself.
Now I have only 2 lines in the crontab: the first is to start pingsh.sh every 6 minutes, and the second is to force off the speed at one in the morning in case I fell asleep and did not turn off the computer :)
The above scripts can be found
here .
PS
A little offtop. At the request of the workers, a simple Delphi program for Windows was written in parallel to quickly change the tariffs. In contrast to the
option proposed by
ratswolf, the browser is not used, we work with the Yota server directly by means of GET and POST requests with the above parameters.
Speed ​​selection option and display of current speed / balance:

And all the settings:

You can take it
here .
The most important thing
And the most important question - the real savings amounted to about 50%, at the base rate of 900 rubles / month (5.5 Mb / s), it was possible to work for almost a month and a half.
Instructions for installing curl
If there is not enough space on rootfs, do as follows.
Connect to the router via ssh and execute the following commands:
opkg update opkg install curl --nodeps opkg install zlib --nodeps opkg install libcurl --nodeps opkg install libopenssl -d ram ln -s /tmp/usr/lib/libcrypto.so.1.0.0 /lib/libcrypto.so.1.0.0 ln -s /tmp/usr/lib/libssl.so.1.0.0 /lib/libssl.so.1.0.0
Now we add lines to rc.local (can be done via the System Startup web interface):
sleep 180 opkg update opkg install libopenssl -d ram
Thus, even after a reboot, the router itself will load the missing packet into RAM.
UPD
As it turned out, the Yota provider uses different tariffs and parameters for different regions.
Presented by yota.sh for Khabarovsk. To get a script for your region
You must use the gen.sh script (located there):
gen.sh login password
At the end of the script, n_yota.sh will be created with the necessary parameters and tariffs.
Thanks for attention.