Python script to wait for bash prompt to appear - python

I want to automate putty , i know there is paramiko and other ssh libraries available.
But i want to get logs as if i typed commands manually.
Is it possible to make python script wait till [user#server~]# appears then enter the command.
I am using pyautogui...
I have tried searching pyautogui docs but could not find any .
Is there anything subprocess module can do . I am new to python sorry.
I have tried using image but it is not a good option.
import pyautogui
image = pyautogui.locateOnScreen("image.png")
#Searches for the image
while image == None:
image = pyautogui.locateOnScreen("image.png")
print("still haven't found the image")

Related

Automate Google chrome

I hope you are ok.
I'm trying to make a script open chrome for me and then click on a tap :
import os
import subprocess
import pyautogui
cmd = 'google-chrome'
if (subprocess.call(cmd)):
pyautogui.click(618, 671)
#use it to know your screen position you may need to install scrot
# pyautogui.displayMousePosition()
but when I run the script I get an error, the error said :
[6869:6869:1111/121828.562003:ERROR:sandbox_linux.cc(374)] InitializeSandbox() called with multiple threads in process gpu-process.
[6815:6848:1111/121828.628223:ERROR:media_history_store.cc(363)] Failed to create or update the media history store.
Any ideas about how I can solve it.
subprocess.call(cmd) is a blocking call and your program will not continue.
Use subprocess.Popen(cmd) instead
Anyways to overcome that error, you should change your command to google-chrome --disable-gpu --disable-software-rasterizer
and don't run in sudo :)

python PIL show image - file system error

I try to take a screenshot and display it with the following code:
import pyautogui
im = pyautogui.screenshot()
im.show()
but it says in the console Access is denied. and pops a windows error:
same error if i save the picture and try to double click it from the explorer (not with python), but I can open it with sublime text for some reason.
code used for saving:
import pyautogui
import PIL
im = pyautogui.screenshot()
im.save(r'screenshot1.png')
im = PIL.Image.open(r'screenshot1.png')
im.show()
how do i solve this? should i change permissions on taking the screenshot somehow?
You need to change the startup type of your Windows license management service.
Search for "Services" in your computer, and open the Services Management. Find the Windows license management service, and change the startup type to "Automatic". Press "apply" and "ok".
Refer to this for visualization of the process.

Python automate open application

Please give me idea regarding how to tackle this problem. I am not able to find any resource regarding this. Your help will be immensely valuable. So we have one limited license software. And want to reiterate the python invoking the application. If the application gives the error that licence is not available it should close the application and wait for sometime say 1 min and again invoke the process, it should do so endlessly until a licence is available and the application is finally open.
I am able to open the application using
Import os
os.startfile('application executable')
After this I want the application to know if there is an error window popping , it should close the window and wait for sometime and again open the application
os.startfile returns as soon as the associated application is launch so use Popen instead.
As you are using windows use these steps.
To Open a Shortcut using Popen on Windows first install pywin32
Step one:
python -m pip install pywin32
Step two:
Navigate to your python Scrips folder something like
C:\Users\Name\AppData\Local\Programs\Python\Python38-32\Scripts then type the command.
pywin32_postinstall.py -install
Then the code to use Popen is.
import subprocess
import win32com.client, win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r'path to shortcut')
long_path = shortcut.Targetpath
p = subprocess.Popen(long_path)
p.wait()

automating, "opening a windows app and logging in" with python; which module?

I'm trying to write a script that automates opening a windows software and then type the login information (username and password) to fully open said software. Ideally, I would like these actions to be automated to run daily at a certain time.
An example of such a software would be Skype but there are many other softwares that I want to try this one.
I'm looking for suggestions for which modules I should look into.
I have been browsing python automation tutorials on youtube any the only thing I came upon was the module Pywinauto. Most of the tutorials seem to be focused on opening notepad and using notepad specific commands, so I"m not sure how much freedom I have with other windows applications. I visited the Pywinauto offical site, but the homepage wasn't as detailed as I hoped.
I may plan to automate some more down the road ( executing some commands down after opening a program), so ideally I want a module that has a lot of flexiblity.
I'm looking for general suggestions and recommendations for modules.
Thanks!
for opening programs there is os.system and subprocess.call some people also use win32api.
import os
os.system('notepad.exe')
import win32api
win32api.WinExec('notepad.exe')
import subprocess
subprocess.Popen(['notepad.exe'])
subprocess is "cross-platform".
import subprocess
program = 'C:\\Program Files (x86)\\Skype\\Phone\\skype.exe'
subprocess.Popen(program)
this opens skype already, so the first step is done.
Next you want to interract with skype. win32api is windows only but ill use it for this example. If you save the login in skype, you can already save the login, press enter, put in the password and press enter again. We do this with keyboard commands. Here is a working example which opens skype and logs you in automatically
import subprocess
import win32api
import win32com.client
program = 'C:\\Program Files (x86)\\Skype\\Phone\\skype.exe'
subprocess.Popen(program)
shell = win32com.client.Dispatch("WScript.Shell")
win32api.Sleep(5000)
shell.SendKeys("{ENTER}")
win32api.Sleep(6000)
shell.SendKeys("tsadfdsgr")
shell.SendKeys("{ENTER}")

How to run an autohotkey script in Python (2.7)

I'm trying to run an autohotkey (ahk) script in Python 2.7 but nothing seems to work. All online sources I've found are either outdated or overly complicated.
Has anyone found a way of doing this? I just want to run a couple of simple scripts that activates windows and opens applications. E.g:
IfWinExist, Command Prompt - python ...
WinActivate
Update:
I've tried downloading pyahk:
ahk.start() # Ititializes a new script thread
ahk.ready() # Waits until status is True
ahk.execute(mw._['cwd']+"winActivate_cmd.ahk") # Activate cmd window
error: can't load autohotkey.dll
as well as trying this:
import win32com.client # Import library / module
dll = win32com.client.Dispatch("AutoHotkey.Script") #Creating DLL object?
dll.ahktextdll() #no idea what this is doing...
dll.ahkExec("WinActivate, Command Prompt - python")
pwintypes.com_error invalid class string
It seems like you should be able to just launch autohotkey with the script as a parameter using subprocess:
subprocess.call(["path/to/ahk.exe", "script.ahk"])
You'd have to check the autohotkey docs but this seems like it ought to work.

Categories

Resources