The script for termux (Android only), monitors the specified server. If someone has logged in to the server, the script issues a notification in the phone.
- Added support for multiple servers.
- Attention: Now it is necessary to specify the notification-id, the fourth parameter. Any numeric value that is not repeated on other servers.
- termux, termux-api
- Permissions termux and termux-api to access the network and files.
- Generate ssh-key to access the server without a password.
-
Install termux
-
Install termux-api
-
Enter termux, do the following:
apt update apt upgrade apt install termux-api
This will install updates, and establish termux communication with termux-api.
-
Install git
apt install git
-
Clone watch-srv
git clone https://git.tuxnix.ru/nixscript/watch-srv
Everything is ready for configure.
Note: by default, termux has only vi, so if you are not friends with it, set the preferred one. For example, I use Midnight Commander ( mc ), which has mcedit, but I like micro more. Both mc and micro are available, so I think there are other editors too.
There are many options to leave the script in the current location, move to the system directory, for example /usr, or make the most logical. Create a ~/bin directory and copy the script there. I will describe the setup using the example ~/bin.
-
Make directory
mkdir ~/bin
-
Copy script
cp ./watch-srv/watch-srv.sh ~/bin/watch-srv.sh
-
Open script with our favorite editor, uncomment the last line and replace the login server.ru 22 and 0 with the required parameters.
check "login" "server.ru" 22 0
0 - is a unique identifier for the notification. Each server should be different if you monitor several. For example 0, 1, 2 ...
-
Generate ssh-key and upload to server
ssh-keygen cd .ssh rsync ./id_rsa.pub [email protected]:/home/login/.ssh/termux_key ssh [email protected] cat ~/.ssh/termux_key >>~/.ssh/authorized_keys
The last two commands will require a password. After, no password is required.
-
Add a task to cron. Remember, the default editor is vi. If you want to use another, here is an example:
export EDITOR="mcedit"; crontab -e # or that export EDITOR="micro"; crontab -e
The task in cron is easy to add. For example, to check every minute in the editor, we write the following line:
* * * * * ~/bin/watch-srv.sh
I decided to do a check every 30 seconds, so I had to use hack:
* * * * * ~/bin/watch-srv.sh * * * * * (sleep 30; ~/bin/watch-srv.sh)
Here are two calls to the script, both act in a minute, but the first one works, and the other with a delay of 30 seconds.
Save and exit
-
Run cron
crond
Now, if someone logs in on the server, a notification of this type will appear in the smartphone:
As you can see in the screenshot, there are three buttons:
- All right - prevents the permanent notification to appear until the user logs out on the server. After all users have logged out, the script runs in standard mode.
- Copy - puts the notification text into the smartphone's system clipboard.
- Get full log - gets the whole log and saves it to Downloads/srv-login.log
Everything is quite simple. If you have any suggestions, suggestions, ideas, write to VK@Nixscript or to me [email protected] Instagram@grigruss e-mail.