I created a Python script to execute Linux commands ...

About programming and getting involved with Linux Mint development
Post Reply
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

I created a Python script to execute Linux commands ...

Post by Mintmann »

I got tired of using scraps of papers to save some usable commands so I created a script to run a lot of the Linux commands.
It does not change anything in the system. It just creates text files on the desktop with the results of the commands that are run.
I am not a Python scripter but I did my best to make a usable script. I put the script on github.
https://github.com/AriIris/LinuxEasyCommandCenter

Click on the green CODE button and download as a .zip file.
After unpacking the zip file go to that directory and edit the LinuxEasyCommandCenter.py file and replace the text 'put your password here' with your password. The password is necessary for many of the commands to run.
To run this script create a Launcher on the Linux desktop and point to the script file.

This script is free to use and share. You may change it to your liking.
If changed please rename it to not cause confusion with the original file name.
This script was developed on Linux Mint 20.2 with Python3 using PyCharm.
https://www.jetbrains.com/pycharm/

I am sure that the gurus could make a better script, however I did the best I could ... my first script.
I followed the download, unpack, edit and create launcher instructions and all worked okay.

There will be tabs to go through to look for commands. Some day I might put them in order.
You will see Run and Man buttons. The Man button is to get the manual documentation for that command.
Both buttons will put text files on the desktop starting with a lower case 'z' to keep them together.
Please give me some feedback.

I forgot to mention that many of the commands have comboboxs to choose switches.
Also when making a Launcher you give it a name and comment and then browse for my script, but
you then, when returned to the launcher creation, in front of the script name you must insert
'python3' without the semiquotes and a space.

UPDATE: I added some images at github
User avatar
Drugwash
Level 5
Level 5
Posts: 680
Joined: Fri Jun 07, 2019 6:40 am
Location: Ploieşti, RO
Contact:

Re: I created a Python script to execute Linux commands ...

Post by Drugwash »

Haven't yet checked your script, got sidetracked by your mentioning of PyCharm as I'm in search myself for a nice - but preferrably smaller and less resource-hungry though - Python tool/IDE.
Just a couple ideas for the script based on what you said above:
- take into consideration setups that do not ask for a password when running with sudo
- better create a folder somewhere in user's home dir, create a hardlink to it on the desktop and have the script save all those text files in that folder

Reasoning:
Some users (such as myself) may run with password requirement disabled so the script should be able to pass that stage.
Littering the desktop with text - or any kind of - files is not such a good idea. Keeping them all together is much easier and cleaner when they are all stored away in a dedicated folder accesible from the desktop.

Hope this helps somehow.
mikeflan
Level 12
Level 12
Posts: 4254
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: I created a Python script to execute Linux commands ...

Post by mikeflan »

Here is another idea for you.

I have been using a Perl script for about 2 decades now that opens Nemo in a particular folder. One line in my explore.txt file is this:
desktop|/usr/share/applications
If I enter desktop in the program (from within the terminal) it opens a Nemo window to /usr/share/applications

If I enter term desktop then it opens a terminal window pointed to /usr/share/applications

That explore.txt file has 1,360 lines in it! Some are set to open a file name in a program:
hf|/media/mike/disk1/Outdoors/HOC/Finances/HOC Financial Records.xls

A very handy program to have. Like I say I have been using it for almost 20 years now.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

Good ideas guys.
I was very ill for quite some time.
I did alter the script to run on Windows to display many pages of information.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

I have updated this script to create a desktop directory which will contains all the output text files.
Also removed the leading file name characters 'z_', not necessary now.

I added page displays for Windows which are kept in a text file that is loaded into the script.

This script does not changed anything in the operating systems.
It does not access the Windows registry.
It does not send any information to me.

It would be nice to have someone check it out before I post it on Github.
reticulatus

Re: I created a Python script to execute Linux commands ...

Post by reticulatus »

You might consider having your user password in a python script is acceptable. However it could be discovered by anybody with access to the computer (in person or online) and thus is a definite security risk.
StrangeAngel
Level 1
Level 1
Posts: 8
Joined: Mon Oct 24, 2022 12:24 am

Re: I created a Python script to execute Linux commands ...

Post by StrangeAngel »

i'm trying press button "man fdisk -i" and terminal write this:

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/home/oleg/Загрузки/LinuxEasyCommandCenter-main/LinuxEasyCommandCenter.py", line 634, in fdisk1b
    Popen_Function_Common('man fdisk')
  File "/home/oleg/Загрузки/LinuxEasyCommandCenter-main/LinuxEasyCommandCenter.py", line 572, in Popen_Function_Common
    log = open(logpath + cmd2  + '.txt', 'w')
FileNotFoundError: [Errno 2] No such file or directory: '/home/oleg/Desktop/z_man fdisk.txt'

What's a problem? And many others buttons cause this problems too.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

reticulatus wrote:
Mon Oct 24, 2022 6:50 am
You might consider having your user password in a python script is acceptable. However it could be discovered by anybody with access to the computer (in person or online) and thus is a definite security risk.
Great idea!
I will change the script to accept a password when it first starts.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

StrangeAngel wrote:
Tue Oct 25, 2022 10:33 am
i'm trying press button "man fdisk -i" and terminal write this:

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3.10/tkinter/__init__.py", line 1921, in __call__
    return self.func(*args)
  File "/home/oleg/Загрузки/LinuxEasyCommandCenter-main/LinuxEasyCommandCenter.py", line 634, in fdisk1b
    Popen_Function_Common('man fdisk')
  File "/home/oleg/Загрузки/LinuxEasyCommandCenter-main/LinuxEasyCommandCenter.py", line 572, in Popen_Function_Common
    log = open(logpath + cmd2  + '.txt', 'w')
FileNotFoundError: [Errno 2] No such file or directory: '/home/oleg/Desktop/z_man fdisk.txt'

What's a problem? And many others buttons cause this problems too.

Do you have PyCharm & Python installed along with 'tkinter'?
I'm learning Python on the fly. I think that the problem is that 'tkinter' is not found.
I don't know a way to use 'tkinter' without PyCharm.
I will be releasing a newer version of the script soon at Github.
StrangeAngel
Level 1
Level 1
Posts: 8
Joined: Mon Oct 24, 2022 12:24 am

Re: I created a Python script to execute Linux commands ...

Post by StrangeAngel »

I found the reason. Here it is necessary to write

Code: Select all

logpath = '/home/' + findme + '/Рабочий стол/z_'
but not 'Desktop' for me. i'm using Russian version LInux mint.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

I changed the script to prompt for a password.
If desired a password can be coded into the script and the password prompt will not appear.

However if the password is incorrect the Popen will hang, hence I had to add a timout for a
few seconds for specific commands and a couple of seconds for the remaining commands.
I bit tacky, but I don't know any other way to handle the hang.

I removed all the Windows code.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

StrangeAngel wrote:
Wed Oct 26, 2022 3:19 am
I found the reason. Here it is necessary to write

Code: Select all

logpath = '/home/' + findme + '/Рабочий стол/z_'
but not 'Desktop' for me. i'm using Russian version LInux mint.
The new script release will put a folder on the desktop and the command results in text files within that folder without the 'z_' characters.

What does the path to the desktop look like in the Russian version?

I wonder if the space is causing the problem? '/Рабочий стол/z_'
StrangeAngel
Level 1
Level 1
Posts: 8
Joined: Mon Oct 24, 2022 12:24 am

Re: I created a Python script to execute Linux commands ...

Post by StrangeAngel »

Mintmann wrote:
Sat Oct 29, 2022 3:22 pm
StrangeAngel wrote:
Wed Oct 26, 2022 3:19 am
I found the reason. Here it is necessary to write

Code: Select all

logpath = '/home/' + findme + '/Рабочий стол/z_'
but not 'Desktop' for me. i'm using Russian version LInux mint.
What does the path to the desktop look like in the Russian version?
/home/oleg/Рабочий стол
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

Lets forget this version of the script. I have a new script and it works differently.
I plan to release it soon, like maybe tomorrow on Github. I'm currently testing it.

What I do to run the next script release named: 'LECC.py'.
Place the script somewhere on disk such as in Documents.
To make the script usable change its permissions to allow running it as a program
Start Terminal, cd to were you put the script and type: 'python3 LECC.py' without the semiquotes.
There might be a way to create a shortcut, but I have no idea how to do that yet.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

I have an idea: edit the script and change all 'Desktop' entries to 'Рабочий стол'.

Where did you get the Russian version of Mint 21 Cinnamon?
User avatar
Drugwash
Level 5
Level 5
Posts: 680
Joined: Fri Jun 07, 2019 6:40 am
Location: Ploieşti, RO
Contact:

Re: I created a Python script to execute Linux commands ...

Post by Drugwash »

According to this accepted reply at StackOverflow the correct code would be:
import subprocess
subprocess.check_output(['xdg-user-dir', 'DESKTOP'])


Please note the returned value is bytes and ends with a new line character.
StrangeAngel
Level 1
Level 1
Posts: 8
Joined: Mon Oct 24, 2022 12:24 am

Re: I created a Python script to execute Linux commands ...

Post by StrangeAngel »

Mintmann wrote:
Sun Oct 30, 2022 2:31 pm
I have an idea: edit the script and change all 'Desktop' entries to 'Рабочий стол'.

Where did you get the Russian version of Mint 21 Cinnamon?
[/quote/
On the official Linux mint website. There's installing depending on your choose language
User avatar
Drugwash
Level 5
Level 5
Posts: 680
Joined: Fri Jun 07, 2019 6:40 am
Location: Ploieşti, RO
Contact:

Re: I created a Python script to execute Linux commands ...

Post by Drugwash »

@StrangeAngel: could you please try the code above and post the result?
Just open a terminal, type python3 and hit Enter (or if it fails type python and hit Enter), then type each of the two lines above and press Enter. It should yield the full path to the Desktop in system's language.
When finished press Ctrl+D to exit the Python environment.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

Drugwash wrote:
Mon Oct 31, 2022 7:22 am
@StrangeAngel: could you please try the code above and post the result?
Just open a terminal, type python3 and hit Enter (or if it fails type python and hit Enter), then type each of the two lines above and press Enter. It should yield the full path to the Desktop in system's language.
When finished press Ctrl+D to exit the Python environment.
Works great, thanks!

below code gives me exactly what I need:
global_folder_path = subprocess.check_output(['xdg-user-dir', 'DESKTOP']).decode()
global_folder_path = global_folder_path.strip('\n')


I wish I could get a return code when using the Popen statement ... it's always 'None'.

Update:
I have the returncode working now.
I have been testing all the command and their options.
Mintmann
Level 3
Level 3
Posts: 114
Joined: Mon Aug 09, 2021 6:36 am

Re: I created a Python script to execute Linux commands ...

Post by Mintmann »

Well here it is. :mrgreen:
Hopefully it will work on everyone's Linux Mint computer.
Works on three of mine. Two laptops and one desktop.
I posted a few screen shots.

I would like someone to try is out to see if it's working correctly.
I think that it should work on computers not using English ... I can't test that situation.

Download:
https://github.com/AriIris/Linux-Easy-Command-Center

Give me some feedback good or bad.
Post Reply

Return to “Programming & Development”