I have tiny little python code which logs in to a remote machine and gets the output of a command and prints to a file..
Here for a particular command, the server asks for Y or N, how do i pass an Yes to it and get the desired output?
Here is the sample output from the server:
root#nnodee11cc40c:/home/usr/redsuren# controlport rst 0:3:3
WARNING: Port 0:3:3 may have active Smart SAN target driven zones that may be disrupted. If changing the port configuration, remove the Smart SAN zoning by using the removehost command. This must be done before changing the port configuration; otherwise, you will not be able to manage the zone on the switch associated with this port.
Are you sure you want to run controlport rst on port 0:3:3?
select q=quit y=yes n=no: ---------> Here i have to tell the program to enter y
How can I achieve this?
Thanks!!
If I understand your question correctly, you need to automate pressing 'y' & 'enter' with Python. You can easily do this by PyAutoGui.
First, execute pip install pyautogui in the command prompt.
Then import it to your code by using import pyautogui
Now for achieving this you have to put the following code where you want to press 'y' & 'enter':
pyautogui.press('y')
pyautogui.press('enter')
But this may not be timed according to when it asks so you may need to time it yourself
by adding time.sleep(<numberOfSeconds>) after importing it by import time
Now here is the full answer:
import pyautogui, time
# Your code here
time.sleep(3)
pyautogui.press('y')
pyautogui.press('enter')
But if my answer is not what you asked for, then you have to give us your code so we can understand your question better thus answer it to your needs.
Related
I managed to install windows based network printer with python3 on ubuntu.
For better coding, I want to check first if the file with the drivers in it exists after the download. I know it is possible with os.path.isfile or something like that but I would like to do that with subprocess although os will not be supported in the future anymore.
So how do I do it? With subprocess.call or something like that?
to check for a file to be present, You ideally use pathlib, which is the pythonic and portable way to interact with the filesystem.
But to avoid Time of Check to Time of Use Errors (TOCTTOU) You should consider:
Instead of :
if check_printer_present():
# now, after checking the printer is present,
# the printer might go offline
use_printer()
better use:
try:
use_printer()
except PrinterError():
printer_error_cleanup()
see:
https://de.wikipedia.org/wiki/Time-of-Check-to-Time-of-Use-Problem
You might remember that idiom as :
it's better to ask forgiveness than permission
(t is better to act decisively and apologize for it later
than to seek approval to act and risk delay, objections, etc.)
We have a python script that needs to trigger the open of the Microsoft Store. We believe that the easiest way to do that is to use the ms-windows-store:// protocol.
We're currently doing that like this
import subprocess
ret = subprocess.call(["start", "ms-windows-store://pdp/?ProductId=9WZDNCRFHVJL"], shell=True)
Is that the recommended way to do this? I'm not sure if using start is correct here, or if there's something better?
Use os.startfile("ms-windows-store://pdp/?ProductId=9WZDNCRFHVJL"). This calls WINAPI ShellExecuteW directly. If you use subprocess, you have the expense of starting a child process. Plus CMD's start command will first search PATH to find a file that it can execute. Presuming nothing is found (and nothing likely will be, given this name), it hands the request off to ShellExecuteExW to let the OS shell handle it.
I'm coding a program to do some action with webdriver and Autoit in python, I want to do two things before I start selling my code:
Add onetime activation code to my software on one PC, like to make the program works only on one pc.
Make my program able to receive updates from the internet once I add to the code some more features or correct some others.
is it possible only with Python? or what is the method statement to do it?
On client side you need use hard-disk serial or/and uuid of partition or Operational System timestamp + something to generate serial code .
On server side , you need a API to store hard disk serial to validate if this is a computer valid . And you client on load check if activaction is valid .
The second question i can't answer .
Regarding the second part of your question:
Create a text file with the latest version of your application and put it on your webserver e.g. http://download.example.com/example-app-version.txt to get and read its value later.
On your python code download and read the text (for Python 3+ use 'import urllib.request' and urllib.request.urlretrieve) when your app runs and compare it against the installed version (if statement).
E.g.
if latestVer > installedVer:
#update
else:
#application continues
I would like to know, how would I, in Python, be able to ignore certain/all keypresses by the user while a certain function in the program is executing? Cross platform solutions are preferred, but if that is not possible, then I need to know how to do this on Macintosh. Any help is greatly appreciated! :)
EDIT: Right now, I am processing keypresses through the turtle module's onkey() function since I did create a program using turtle. I hope this helps avoid any confusion, and again, any help is greatly appreciated! :)
You might want to modify your question to show how you're currently processing key-presses. For example is this a command-line program and you're using curses?
You could use os.uname to return the os information or sys.platform, if that isn't available. The Python documentation for sys.platform indicates that 'darwin' is returned for OSX apple machines.
If the platform is darwin then you could, in your code, ignore whatever key-presses you want to.
Edit (Update due to changed question):
If you want to ignore key-presses when a certain function is being called you could either use a lock to stop the key-press function call and your particular function being executed together.
Here is an example of using a lock, this may not run, but it should give you a rough idea of what's required.
import _thread
a_lock = _thread.allocate_lock()
def certainFunction():
with a_lock:
print("Here's the code that you don't want to execute at the same time as onSpecificKeyCall()")
def onSpecificKeyCall():
with a_lock:
print("Here's the code that you don't want to execute at the same time as certainFunction()")
Or, depending on the circumstances, when the function which you don't want interrupting with a key press is called, you could call onkey() again, with the specific key to ignore, to call to a function that doesn't do anything. When your particular function has finished, you could call onkey() again to bind the key press to the function that processes the input.
I found similar problems, maybe it will help you with your problem:
globally capture, ignore and send keyevents with python xlib, recognize fake input
How do I 'lock the keyboard' to prevent any more keypresses being sent on X11/Linux/Gnome?
https://askubuntu.com/questions/160522/python-gtk-event-ignore
I'm fairly new to python, and this question might be fairly specific to the situation. I'm using IPython to create graphs of physics simulations.
The problem I'm running into is when I try and create a program that avoids having to retype the same code over and over. I'm trying to get a user input to execute a command in IPython without having to type everything out yourself.
For example, there is a program I am using called pynbody.what I am trying to do is get it so when I open my test program with ipython, it prompts the user for an input of what the user would like to import. However, I have only been able to get it in as a string, which won't execute.
What is the syntax involved in getting a user input, ie
input= raw_input("What would you like to import? ")
to execute as if it were a command you were typing into IPython, ie
import pynbody
It works fine if you create a program, ie test.py, and in it you have import pynbody. Any ideas on getting this to work from a user input?
Not sure I fully understand what you are trying to do, but you could try
imported_module = __import__(<string>)
or
eval('python expression') see http://docs.python.org/2/library/functions.html#eval
for more complicated, look at the 'ast' module (http://docs.python.org/2/library/ast.html)
But for a beginner I would avoid going this road of prompting user for package to import, or enter expression by hand with raw_input.