I want to change the IP address of the machine continuously, SO after a lot of search i got this useful link https://github.com/jotyGill/openpyn-nordvpn . I have done the steps as per this URL and i tried the steps in terminal window(Mac) and it is successfully changing the IP.
But the issue is that need to manually run the script again to change the IP, So thought of automating it.
So what i want to do is ,
1. Open an terminal window in mac using Python and execute the following commands,
Enter this command :
sudo openpyn --init
it will prompt to enter password and i have to enter password manually,Is there a way where the program can do this password input automatically using subprocess or any other methods in Python?
After the user name and password is entered ,Need to enter the below command.
openpyn US --area “area”
This will change the IP and need to do the same again.While doing the same either we need to do it from the start or pass CTRL+C to end the current execution and pass
" openpyn US --area “area” "this command again.
I'm trying to execute few statements in terminal window from a python program.
These are the things i have done and no luck .Can anyone please guide me on how to do this?
import subprocess
import os
pid = os.fork()
os.system("""osascript -e 'tell application "Terminal" to do script "sudo openpyn --init;openpyn US --area “area”;os.kill(pid, signal.SIGINT)"'""")
subprocess.call(["sudo openpyn --init"], shell=True)
os.system("sudo openpyn --init")
I don't usually use mac but this might work:
import os
import time
os.system(Command)
time.sleep(0.25)
os.system(password)
Related
I run Windows 10 and I'm trying to execute linux sommands in CMD.
I have installed ubuntu so that I can run bash in CMD
Now I want to be able to run commands in this CMD window.
In this case a simple ping command.
First. I can not figure how to reuse the CMD window, I don't want to open a new CMD for every loop.
Second. I want to make sure that the command is done before executing the next in the loop.
cmd_shell = subprocess.Popen('start cmd /K bash', shell=True)
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = "ping -c4 "+site
# this is where I need help to execute the ping command in the already opend CMD window
After I have open and executed bash, I cant find a good way to write in the CMD window.
I did a not so nice solution using pyautogui but I think there's a much better way to do it, but I dont know how.
Help please.
/ BR BaconFlip
So I realized that I did not need a CMD window to do this.
I could just run it in the script like this:
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = '"ping -c4 '+site+'"'
command = check_output('c:\\Windows\\System32\\bash.exe -c '+cmd_line)
print(command)
Im trying to get python to run a terminal command which will change my desktop wallpaper. Running this command in a normal terminal will change my wallpaper. However when i try to make python run this command in terminal it doesnt work and gives me an error.
I can replicate this error when i open a terminal as root user. I logged in using su and typed my password. Typing in the command then gives me an error and it does not execute. The same when i try to execute the command with python using either the modules os or subprocess.
Is it because my command to change the wallpaper which starts with pcmanfm (which is the window manager) has probblems with its path and when i am root user the path is changed?
How can i make python open a terminal as "normal" user in my case "pi" on my raspberry pi, and run a command in it?
this is the terminal command which changes my desktop wallpaper:
pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg
this is what happens when i run my python code to open a terminal:
root#raspberrypi:/home/pi/Desktop#
instead of normally when it works:
pi#raspberrypi:~ $
here is my python code which is meant to open a new terminal with the command which changes my desktop wallpaper, however i end up as ROOT user as described above and get an error and nothing happens:
import os,random
import subprocess as sub
sub.call('lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"', shell=True)
executing the following from a normal terminal works perfectly:
lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"
this is the result after running the python code, which is the error i always get:
** Message: 12:11:08.734: x-terminal-emulator has very limited support, consider choose another terminal
root#raspberrypi:/home/pi/Desktop#
So how can i make python open a terminal as user "pi" not as root?
or is this not the problem?
thanks!
The problem was calling the python script as sudo.
Opening the python script normally as:
Python3 script.py
Will open a terminal as a normal user and. change my desktop wallpaper,
Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.
I have a python script that reboots the device at the end of the script. I got it working fine on my user account by giving the user rights to /sbin/shutdown in sudoers file. But the problem is that when I try to automate it with the user's crontab, I get the following error:
sudo: no tty present and no askpass program specified
Any ideas how to solve the problem? I thought the user's crontab would have same rights as the user, but it doesn't seem that way.
I can't use root crontab because part of the python script uses Selenium with geckodriver and geckodriver cannot be ran as root.
Here's the reboot part of the python script:
def restart():
command = "/usr/bin/sudo /sbin/shutdown -r now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
I'm using Ubuntu 18.10
I managed to solve the issue. If someone is having similar problems, try editing the sudoers file so that your user ALL=NOPASSWD: /sbin/shutdown is the last line in the file so it takes priority over conflicting lines.
Basically what I'm looking to do is to have python code that I can run from cmd that opens a new cmd window then types commands into it. I have been able to open and run commands but the commands always run in the first cmd window. I think I might need to switch the cursor or something like that, but I don't know how.
Here is my current code.
import subprocess
subprocess.call('start', shell=True)
import time
time.sleep(5)
cmd=['', ''] ### In the second '' you can type in cmd.
for each in cmd:
process=subprocess.Popen(each,stdout=subprocess.PIPE,shell=True)
output=process.communicate()[0]
print(output.decode('utf-8'))
- Thanks
EDIT :
NVM Its in the similar one
Ok, so when I run this command:
sudo python3.5 /path/to/my/script.py
It executes fine and returns:
select user:
Guest
My username
Choose the account that needs cleaning up (enter corresponding number):
After which I enter "2", press enter and the script runs great.
However, when I run:
osascript -e 'do shell script "python3.5 /Path/to/my/script.py" with administrator privileges'
I get the expected pop-up screen in which to enter my sudo-password, but afterwards it throws out the error:
OSError: [Errno 9] Bad file descriptor (1)needs cleaning up (enter
corresponding number):")
Anyone has any idea on this? Any help would be greatly appreciated!
PS; some background as to why I need this method --> I am using a pre-script (also written in python) from which myscript.py needs to be executed. My prescript.py does not need administrator privileges. Sometimes however, it will be required to call myscript.py which does need administrative privileges. Since I do not want to hard-code my sudo-password into any of my scripts I want to get a pop-up box asking me for it. I figured, telling my pre-script.py to run subprocess.Popen([]) on the osascript command I mentioned above would do the trick. However, for that, I first need to get the osascript command to work properly.
Ah, so I figured this one out myself. As expected it had to do with privileges. Somehow "do shell script" is run via other privileges then just entering the command in terminal. And "sudo command" in terminal does not equal (fully) to "do shell script [script] with administrator privileges".
For me, what fixed it, was navigating to:
System Preferences --> Security and Privacy --> Privacy tab --> Accessibility
And then dragging Python.app and Terminal.app to that window (you might need to unlock it first by pressing on the little lock in the lower-left corner of that window).