how to run the admin command prompt using python? - python-3.x

I want to run a script on the admin cmd prompt using python os.system
import os
import sys
import keyboard
import time
os.system('cls')
print('+'* 60)
os.system('netsh wlan set hostednetwork mode=allow ssid=test3 key=987654321')
time.sleep(2)
p = input('')
keyboard.press_and_release('enter')
print('#' * 60)
os.system('netsh wlan start hostednetwork')
time.sleep(2)
keyboard.press_and_release('enter')
v = input('')
How can I call the admin cmd prompt ?
Thank you in advance

I found a way to make this happen
so my idea is to to create script to share wifi using python, and the problem is you have to execute the script using admin command prompt. and to solve that, follow my steps:
1 - create the python script for the commands(copy mine in the original question)
2 - create a simple text file like this one
pause
python name.py
pause
3 - save it under the extension of " .bat " (example.bat)
4 - go to your system directory (mine is "C:\Windows\System32")
5 - copy the the batch file there
6 - create a shortcut for this batch file (it will ask to be in your desktop, click OK)
7 - go to the properties of this shortcuted file click on advanced and give it the admin
access - click OK
8- copy or cut your script.py file to the same directory (mine is "C:\Windows\System32")
and that's it, just launch your batch file in the desktop and here you go everything works fine.

Related

How to make Midnight Commander open file with default application in Windows

I recently started using Midnight Commander on my Windows 10 system, but I can't seem to get it to handle images or pdf files properly. When I select the file and press Enter, nothing happens. I've tried altering the extension file to the following.
default/*
Open=(%f)
After making that change, I get this error.
C:/Users/MyUser/AppData/Local/Temp/mc-MyUser/mcext2509Z1: line 13: C:/Test.txt: Permission denied
I am using the Powershell v7.3.2 in Windows Terminal v1.15.3465.0 on Windows 10 v.19044.2486
Try
default/*
Open=start %f
Try start C:\Test.txt from the cmd command line to test.
See https://superuser.com/q/550138. Try
start "" %f
For Powershell try
Start-Process %f

How to check if the environment variable "PROJ_LIB" is defined and how to unset it ? (PyQGIS Standalone Script Executer)

I just tried the standalone PyQGIS application by running the custom script "Proximity.py"* in a VS Code project without the need of a GUI (such as QGIS).
But, when I run the python-program I get the following message:
proj_create_from_database: C:\Program Files\PostgreSQL\14\share\contrib\postgis-3.2\proj\proj.db contains DATABASE.LAYOUT.VERSION.MINOR = 0 whereas a number >= 2 is expected. It comes from another PROJ installation. (see also: Error Message after launching the configuration (launch.json) from VS Code (when pressing F5))
I'm trying this online example with the following installations:
PostgreSQL 14
Python39
.vscode\extensions\ms-python.python-2022.4.1\pythonFiles\lib\python\debugpy\launcher
osgeo4w-setup.exe (including QGIS LTR)
I read that there is a solution by undefining [PROJ_LIB] before importing pyproj or osgeo: del os.environ ['PROJ_LIB'] as described under this link. If this is also supposed to be the correct solution in this case, can someone help me with step-by-step instructions (for dummies)?
. * The "Proximity.py" script is a pyqgis standalone example from "https://github.com/MarByteBeep/pyqgis-standalone"
Finally, I got a solution to be able to run the "standalone PyQGIS"* example "Proximity" (provided by MarByteBeep).
This solution was possible without needing to launch the configuration file "launch.json" as above described. And so, avoiding the need to make any configuration to the environment variable "PROJ_LIB" by trying to circumvent the above issue.
I just first added the following two code-lines (see here line 2 and 3) in the python file "main.py" so as to be able to use the plugin "PROCESSING" (initially line 8 of the "main.py" file), then I store it and finally I ran it.
Line 1: from qgis.core import
Line 2: import sys
Line 3: sys.path.append('C:\Program Files\QGIS 3.24.1\apps\qgis\python\plugins')
Line 4: qgs = QgsApplication([], False)
Line 5: ...
The Proximity example is based on the answer of "Mar Tjin" to the following Question: "Looking for manual on how to properly setup standalone PyQGIS without GUI"
. * By "Standalone PyQGIS" I refer to code/scripts that can be run outside the QGIS-GUI (=> QGIS-Desktop/Server Application). In my case under the external Editor VS Code

Raspberry Pi config - read parameters from a seperate file

I have a raspberry pi configured to boot chromium full screen which works great. However, I want my non-techy user to update the website by simply editing a file on the desktop.
/etc/xdg/lxsession/LXDE-pi/autostart:
# Link to the settings file on the desktop
source /home/pi/Desktop/websetup.config
# start chromium
#chromium --noerrdialogs --kiosk $website --incognito
I would like to have a setup file on the desktop which my user can update the website and overscanning etc without directly editing any config files above:
i.e.
websetup.config:
# Enter website to show
webbsite=https://newWebsite.com
# Overscanning on = 1, turn off = 0
overscan=0
I, however, can output $website from a .sh bash but I cannot work out how to pass the value $website to chromium as I cannot pass the actual value
ok, I have managed to work it out. I created a script called run_chromium.sh:
##
## this file is referenced in: /etc/xdg/lxsession/LXDE-pi/autostart
##
## website to show
website=https://www.website.com
## 0=overscanning off, 1=on
overscanning=1
and I linked to this script in autostart:
/bin/bash /home/pi/Desktop/run_chromium.sh

How can I allow a Python code to open "%temp%" folder for any user?

I have some users on my PC and I try to create a Python code to open %temp% folder, but the problem is that it works under my account only. When I use the same code on a different account it does not work on the same PC.
My folder path >> C:\Users\MyAccount\AppData\Local\Temp <<,
the problem error with this user 'MyAccount'
This is my code:
import webbrowser
webbrowser.open('C:\Users\MyAccount\AppData\Local\Temp')
I need to pass the correct userFolder to my code to work with.
Example:
my account the path >> **C:\Users\MyAccount\AppData\Local\Temp**
on different account >> C:\Users\ **?** \AppData\Local\Temp
**?** = it should be the name of the user.
Could you please advise me?
If pathlib is an option (comes with Python 3.4+) you can use
from pathlib import Path
Path.home() / 'AppData' / 'Local' / 'Temp'
if not, try
from os import path
path.expanduser('~/AppData/Local/Temp')

Controlling a minecraft server with python

I've searched a lot for this and have not yet found a definitive solution. The closest thing I've found is this:
import shutil
from os.path import join
import os
import time
import sys
minecraft_dir = ('server diectory')
world_dir = ('server world driectory')
def server_command(cmd):
os.system('screen -S -X stuff "{}\015"'.format(cmd))
on = "1"
while True:
command=input()
command=command.lower()
if on == "1":
if command==("start"):
os.chdir(minecraft_dir)
os.system('"C:\Program Files\Java\jre1.8.0_111\bin\java.exe" -Xms4G -Xmx4G -jar craftbukkit-1.10.2.jar nogui java')
print("Server started.")
on = "0"
else:
server_command(command)
When I launch this program and type 'start' the CMD flashes up and closes instantly. Instead I want the CMD to stay open with the minecraft sever running from it. I'm not sure why this happens or what the problem is, any help would be greatly appreciated.
p.s. I have edited this to my needs (such as removing a backup script that was unnecessary) but it didn't work before. The original link is: https://github.com/tschuy/minecraft-server-control
os.system will simply run the command then return to your python script with no way to further communicate with it.
On the other hand using subprocess.Popen gives you access to the process while it runs, including writing to it's .stdin which is how you send data to the server:
def server_command(cmd):
process.stdin.write(cmd+"\n") #just write the command to the input stream
process = None
executable = '"C:\Program Files\Java\jre1.8.0_111\bin\java.exe" -Xms4G -Xmx4G -jar craftbukkit-1.10.2.jar nogui java'
while True:
command=input()
command=command.lower()
if process is not None:
if command==("start"):
os.chdir(minecraft_dir)
process = subprocess.Popen(executable, stdin=subprocess.PIPE)
print("Server started.")
else:
server_command(command)
you can also pass stdout=subprocess.PIPE so you can also read it's output and stderr=subprocess.PIPE to read from it's error stream (if any)
As well instead of process.stdin.write(cmd+"\n") you could also use the file optional parameter of the print function, so this:
print(cmd, file=process.stdin)
Will write the data to process.stdin formatted in the same way that print normally does, like ending with newline for you unless passing end= to override it etc.
Both of the above answers do not work in the environment I tried them in.
I think the best way is to use RCON, not sending keys to a window.
RCON is the protocol used by games to run commands.
Many python libraries support Minecraft RCON, and the default server.properties file has an option for RCON.
We will use the python module: MCRON.
Install it. It works for windows, mac, linux.
Type:
pip install mcrcon
Lets configure your server to allow RCON.
In server.properties, find the line 'enable-rcon' and make it look like this:
enable-rcon=true
Restart and stop your server.
Find the line 'rcon.password' and set it to any password you will remember.
You can leave the port default at 25575.
Now, open your terminal and type:
mcron localhost
Or your server ip.
You will be prompted to enter the password you set.
Then you can run commands and will get the result.
But we are doing this with python, not the PYPI MCRON scripts - so do this.
from mcrcon import MCRcon as r
with r('localhost', 'insertyourpasswordhere') as mcr:
resp = mcr.command('/list')
print(resp) #there are 0/20 players online: - This will be different for you.

Resources