Implementing a "screen" command for windows machine using python - python

So I am trying to do following:
I have Cygwin enabled with screen and ssh daemon in Windows 7.
I create a new screen using the command screen -dmS "my_screen" on my Windows machine.
I ssh to the Windows machine from my Linux machine.
I attach to it from my unix machine using screen -d -r my_screen
Now I try to launch a Windows application, for example notepad.exe.
Now I want to a automate this using Python. The objective is to just manually ssh to Windows and then run a Python script which will do the above steps. I have written the following script but it is not working:
import shlex
import os
import time
import subprocess
cmdString = "screen -d -r default_screen"
cmdArgs=shlex.split(cmdString)
p=subprocess.Popen(cmdArgs)
cmds = "./notepad.exe"
cArgs=shlex.split(cmds)
pp=subprocess.Popen(cArgs)
This is not working. :( Basically to get the screen I will probably need to import pty package or tty. But pty & tty are not supported in Windows. I am able to attach to the newly created screen but then attempt to launch the Windows program like notepad for example fails. It hangs and the windows GUI is not launched as it would when down manually.
I am still exploring this but I will appreciate it if someone can point me to the right way to do it.

I put the screen command in the bash profile script of cygwin user. This is working now.

Related

To open on-screen keyboard from my Python code on macOS

open -a KeyboardViewer
I found that it's possible to launch on screen keyboard via the above terminal command.
I am trying to open the OSK from my python code. How can I launch it ( osX)
import os
cmd = 'open -a KeyboardViewer'
os.system(cmd)
the above code will simply launch the virtual keyboard.

How to run a python3 script remotely trough ssh

I am trying to run a python3 script remotely trough ssh, first of all i would like to know if this is even possible if the machine i am trying to run the script on doesnt have a python3 interpreter only a 2001 version of python.
And also i am using the following command to run the script , but its not working:
spawn sh -c "ssh -oPort=$port $ip /usr/bin/env < /home/pythonscript
Pythonscript contains a command meant to output the connected COM ports,it is the following:
import serial.tools.list_ports
print([comport.device for comport in serial.tools.list_ports.comport()])
The output that i get from this is a bunch of system information belonging to the machine i am connecting to, stuff like HOSTNAME,USER,MACHTYPE,MAIL,SHELL,OSTYPE
How would i get my intended output from the command that i am executing
All help appreciated
Firstly you will need to install python3 on each server you will be running this on. You will also need to install pip3 and install pyserial. You could also use virtualenv if you want but I'll leave that to you.
Found a small bug in that script. According to the latest version anyway it's "comports" not "comport: https://pyserial.readthedocs.io/en/latest/tools.html
Updated Version:
from serial.tools.list_ports import comports
print([comport.device for comport in comports()])
I was able to run that remotely simply by doing the following
ssh localhost "python3 /home/user/projects/stack_overflow/56900773_remote_python_ssh/pythonscript.py"
Change localhost to whatever server you want and swap my path for the full path to your script.
When I ran that command I just get a blank list, probably because I have no comports :)

gTTS Python Script in background getting tcgetattr(): Inappropriate ioctl for device

Im developing an application on my raspberry Pi 3, using gTTS for Python:
from gtts import gTTS
import os
import threading
def greet_thread(word):
tts_thread = threading.Thread(target = greet, args=[word])
tts_thread.start()
def greet(word):
tts = gTTS(text=word, lang='es')
tts.save("words.mp3")
print 'Reproduciendo audio'
os.system("mpg321 -q presilence.mp3")
os.system("mpg321 -q words.mp3")
This works perfectly if i run the python script directly from a shell. But if i execute the python script in background using:
python -u script.py > log.txt 2>&1 &
i get this error in my log:
tcgetattr(): Inappropriate ioctl for device
and don't know why. I think is the way it's called from a background process, but no idea how to solved it. Thanks for your attention and help
The problem is that the program needs to be executed using the same user that executes the GUI. So if you are going to execute it in a command shell, avoid using 'root' user.
In my case i need the program executes on start up too. So i solved it using "auto start" instead of a crontab
Navigate to ~/.config/lxsession/LXDE-pi
nano autostart
Edit the file:
#lxpanel --profile LXDE-pi
#pcmanfm --desktop --profile LXDE-pi
//your script.sh
//or #python script.py
#xscreensaver -no-splash
#point-rpi
Save and exit
Reboot
I was having a similar issue with calling mpg321 from a python script that was launched from a bash script via crontab on reboot. I was getting the vague error: tcgetattr(): Inappropriate ioctl for device
After digging thru numerous threads and trying everything I could, I changed to use omxplayer instead and it seems to have solved the issue.
Best I can tell it was some kind of permissions issue with launching from crontab because I could run it without any issue from a terminal session.

How can use Unix on Windows 10 by Python?

I'm learning Python by the book 'Think Python.'
My computer's OS is Windows 10.
I tried using os.popen ('14.8 Pipes' in http://www.greenteapress.com/thinkpython/html/thinkpython015.html) but the book provides an example on Unix.
I want to run Unix on Python script out of curiosity.
I already installed Git bash and Virtual box, but I don't know how to connect and to use Python.
The example provided by the book is:
14.8 Pipes
Most operating systems provide a command-line interface, also known as
a shell. Shells usually provide commands to navigate the file system
and launch applications. For example, in Unix you can change
directories with cd, display the contents of a directory with ls, and
launch a web browser by typing (for example) firefox.
Any program that you can launch from the shell can also be launched
from Python using a pipe. A pipe is an object that represents a
running program.
For example, the Unix command ls -l normally displays the contents of
the current directory (in long format). You can launch ls with
os.popen1:
>>> cmd = 'ls -l'
>>> fp = os.popen(cmd)
It appears you are getting tripped up converting the ls command which lists directory contents on *nix to a Windows command. If you search for "what is the windows version of ls" in a search engine, you will discover that Windows provides similar functionality through dir. For more useful conversions check out the conversion table on lemonda.net.
Changing the code to
>>> cmd = 'dir' and calling it via
>>> fp = os.popen(cmd)
Should enable the example to run on Windows.
if you have the latest update of windows 10 you can use Ubuntu, a version of Linux, from your command prompt by just typing the word 'bash' and waiting on it to download some files. if you see a $ at the end of your command line you got it. after that just type 'sudo apt-get install python' and enter your windows password when it asks for your password hit the 'Y' key when it asks if you are sure. then you should be able to go from the book from there.
EDIT: I believe you have to run cmd as administrator to install bash. You also may have to enable it by hitting the 'windows key + r' to open the run window then type 'appwiz.cpl to open the uninstall window and then click the button on the left of the screen that says 'Turn Windows features on or off', then wait for that to load and then go check the box by 'Windows Subsystems for Linux (Beta)' and then trying to type 'bash' in an elevated cmd prompt.

Permissions Error: Within a python script, I need to find a server file in unix system and open it to run commands for the user

In a Unix system, within a python script, I am trying to open a terminal window and start a server. It is my understanding that python has a subprocess module that is supposed to allow such a thing. So:
import subprocess
subprocess.Popen(['path to terminal'])
returns:
OSError: [Errno 13] Permission denied
How do I run this with the right permissions? Or, is there a better, secure way to do what I need?
I'm relatively new to programming, so please reorient the discussion if my question is misguided. Thank you!
Edit: you state that you would like to execute /Applications/Utilities/Terminal.app, so you are apparently running Mac OS X.
Mac OS X .app programs are directories. They can be started with the Mac OS shell command open.
To open the program /path/to/server in a fresh Max OS Terminal session:
import subprocess
termapp=['open','-a','/Applications/Utilities/Terminal.app']
sp=subprocess.Popen(termapp+['/path/to/server'])
There's also a shell-command version of the terminal, so you do not need open -a.
import subprocess
termapp=['/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal']
sp=subprocess.Popen(termapp+['/path/to/server'])
The two ways have subtle differences in how the windows are grouped by the window manager. Each time you do the above you get another terminal process and another icon in the tray. While with -a a new window is opened within the same Terminal main program.

Categories

Resources