⬆️ ⬇️

Zenity in the service of a system administrator

Somehow one company decided to switch to free software. And they downloaded Linux to all computers, and they set up a server with overseas disks, and made user accounts. And he ordered the head to all employees set themselves intricate passwords. And even though they listened to stories about what kind of beast is in front of them, they don’t know where to change the password. Or they cannot be found in the settings. And run from the console headlong. And they begin to run to the admin with the words "admin admin, help me find a button," and the admin begins to run and be nervous, distracted from important thoughts ...


It is no secret that for many users (especially those not directly connected with IT), the transition to a new operating system and software is often painful. And this is largely due to habits. People remember what is on their computer and, if you rearrange everything, they are lost. As a result, many simple actions can cause difficulties, such as loss of time and nerves.



An idea emerged: what if, for a task that requires immediate user intervention (such as changing the password from the epic above) who is not yet familiar with the system interface, he will obviously be given a window with the ability to solve this problem? In search of a simple solution, I stumbled upon zenity . This is a small utility that allows you to display dialog boxes. It uses GTK + and, as it turned out, in most popular Linux distributions comes bundled. In addition to this, it can be used directly in scripts on BASH, which makes it possible to quickly create a script “on the lap” and start it from the user at a certain time. Consider an example:



function change_password() { local passwords=$(zenity --forms --title="Change your password" \ --text="Set new password (minimum length 8 characters)" \ --separator="," \ --add-password="Your password" \ --add-password="Repeate password") local password1=$( echo $passwords | awk -F ',' '{print $1}' ) local password2=$( echo $passwords | awk -F ',' '{print $2}' ) if [[ $password1 == $password2 ]]; then if [[ ${#password1} == 0 ]]; then zenity --warning --text "Please, select Your password" change_password return fi if [[ ${#password1} < 8 ]]; then zenity --warning --text "Your password too easy." change_password return fi local username=$(whoami) #     #    Ctrl+C - Ctrl+V      #echo "$username:$password1" | chpasswd local expire_time=$(chage -l $username | awk 'FNR==2{print $4}') local full_name=$(cat /etc/passwd | grep "^$username" \ | awk -F ':' '{print $5}' | awk -F ',' '{print $1}') zenity --info --text "$full_name, Your password has been changed. Your new password expires: $expire_time" else zenity --warning --text "Passwords does not match" change_password return fi } change_password 


As a result, we get a window with a form for the password, which “will seem” by itself, probably using the at utility or its equivalent to the user, and he will only have to enter his new password, saving time for him and the administrator. It looks like this (using the standard design of windows from your system):

')

image



Zenity not only shows the window, but also outputs to the standard output what the user did in the window. In our case, he filled out two fields in the form. We need to capture this very conclusion. Thus, we can get the values ​​of all fields from the dialog box in one line through the separator - by default, the symbol "|", in the example, we replace it with a comma. The assignment of zenity parameters speak for themselves, especially if you immediately look at the result.



 passwords=$(zenity --forms --title="Change your password" \ --text="Set new password (minimum length 8 characters)" \ --separator="," \ --add-password="Your password" \ --add-password="Repeate password") 


Then we just have to process the received data (checking for a length of less than 8 characters for the password is for example, never do that!), And inform the user about the results. For this, windows with messages that are created even easier than with forms turned out to be very helpful:



 zenity --warning --text "Your password too easy." --width=300 


image



In conclusion, we can reassure the user with the help of another message, already referring to him by name:



 username=$(whoami) expire_time=$(chage -l $username | awk 'FNR==2{print $4}') full_name=$(cat /etc/passwd | grep "^$username" \ | awk -F ':' '{print $5}' | awk -F ',' '{print $1}') zenity --info --width=400 --text "$full_name, Your password has been changed. Your new password expires: $expire_time" 


image



Alternatively, standard notifications can be used:



 zenity --notification --window-icon="info" \ --text="$full_name, Your password has been changed. Your new password expires: $expire_time" 


image



WARNING ! In older versions of the zenity package, there is a bug that prevents the process creating the notification window from completing. This is solved by adding a non-zero timeout:



 zenity --notification --window-icon="info" --text="..." --timeout=1 


The idea of ​​using such windows was well received and soon the thought arose of asking the user to evaluate the performance of their computer on a five-point scale. This is generally a very useful activity - it allows you to track the rate of increase of satisfied people and to highlight those who have the most difficulties in mastering the new environment, and, accordingly, take measures to eliminate difficulties. In its simplest form, it looked like this:



 mark=$(zenity --scale \ --text "Rate your experience" \ --value=3 \ --min-value=1\ --max-value=5 \ --step=1) if [[ $? == 0 ]]; then zenity --notification --window-icon="info" --text="Thank You!" --timeout=1 fi 


The names of the options are intuitive and do not require explanation.



image



Unfortunately, zenity does not know how to replace such a scale with asterisks (it is believed that asterisks are more intuitive), but soon there was an analogue with a list:



 zenity --list --text="What do you think about your PC?" \ --radiolist --column "Pick" --column "Opinion" \ TRUE "All is good" \ FALSE "All is good, but..." \ FALSE "I don't know" \ FALSE "Too dificult to use it" \ FALSE "AAA \(O_O)/ AAA" \ --height=350 


image



Using cronʻa, you can run and watch the results once a day. It remains only to solve the problem of transferring the collected data to the server. It seems to be a good idea to introduce a monitoring system that already has an agent on each computer as part of the experiment - and draw graphs for user surveys.



Despite the fact that the examples in this article are clearly not the most relevant, I hope that they will guide someone to interesting thoughts about the use of graphical interfaces in small scripts to communicate with users. Perhaps it will be important messages for all workers (in the spirit of the wall team, only with a window), notifications about the beginning of the lunch break or a vote about the place of the corporate party . I would also like to note that if you know ready-made software solutions for such tasks, you can share them in the comments, and my scripts “on the knee” are nothing more than an interesting experiment that can later grow into something more.



In conclusion, once again I provide a link to the zenity manual: https://help.gnome.org/users/zenity/stable/

Source: https://habr.com/ru/post/281034/



All Articles