python - Cant't take the 0 from os.system() response - python

I am trying to check if t is equal to "HTTP/1.1 200 OK"
import os
t = os.system("curl -Is onepage.com | head -1")
print(t)
but the response I got from os.system is
HTTP/1.1 200 OK
0
I have no idea how to take that 0 away, I've tried x = subprocess.check_output(['curl -Is onepage.com | head -1']), but it gives me this error:
Traceback (most recent call last):
File "teste.py", line 3, in <module>
x = check_output(['curl -Is onepage.com | head -1'])
File "/usr/lib/python3.8/subprocess.py", line 411, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/lib/python3.8/subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'curl -Is onepage.com | head -1'

os.system only returns the exit code of the spawned process, with zero usually indicating success.
You had the right intuition with using check_output as it returns the standard output of the process, and handles non-zero exit codes by throwing an exception. Your example fails because the given command needs to run in a shell, which is not the default. As per the documentation:
If shell is True, the specified command will be executed through the
shell. This can be useful if you are using Python primarily for the
enhanced control flow it offers over most system shells and still want
convenient access to other shell features such as shell pipes,
filename wildcards, environment variable expansion, and expansion of ~
to a user’s home directory.
The following works as intended:
import subprocessing
output = subprocess.check_output("curl -Is www.google.com | head -1", shell=True)
print(output)
This gives:
b'HTTP/1.1 200 OK\r\n'

Related

How to write a loop of command line to run asynchronously in python

I'm trying to write a series of linux command to run asynchronously in python, i can print out the linux command, but when I try to asyncio.run then it keep throwing error, does anyone know what Im doing wrong?
import asyncio
import subprocess
def get_args():
parser = argparse.ArgumentParser(
description='Run bulk generator in Gen server')
parser.add_argument('--number_of_intercept', default='200',
help='Number of intercept going into MSOM')
return parser.parse_args()
async def bulk_generator (get_args):
outputlist = []
generator = "/bin/generator/./Generator.exe --direth -w 0000:84:00.1 -- -c 500 -d 62,598,62,1500,62 -r 500 -g eth+vlan:vlan_stream-id="+str(get_args)+"+vlan:vlan_stream-id="+str(get_args)+",span_number_vlan_ids=1+binseqn stream-id=100,eth_src=11:22:33:44:55:66"
print(generator)
p = subprocess.Popen([generator], stdout=subprocess.PIPE)
output = str(p.communicate())
outputlist.append(output)
return outputlist
if __name__ == "__main__":
args = get_args()
for i in range(1,int(args.number_of_intercept)):
asyncio.run(bulk_generator(i))
**********************Output******************************
Traceback (most recent call last):
File "/Users/jasonleung/generator.py", line 32, in <module>
asyncio.run(bulk_generator(i))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
return future.result()
File "/Users/jasonleung/generator.py", line 22, in bulk_generator
p = subprocess.Popen([generator], stdout=subprocess.PIPE)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 969, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 1845, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/bin/generator/./Generator.exe --direth -w 0000:84:00.1 -- -c 500 -d 62,598,62,1500,62 -r 500 -g eth+vlan:vlan_stream-id=1+vlan:vlan_stream-id=1,span_number_vlan_ids=1+binseqn stream-id=100,eth_src=11:22:33:44:55:66'
Process finished with exit code 1```
When the command argument to subprocess.Popen() is a list, the command and arguments must be separate list elements. You put the entire command line in the first list element, so it thinks that's the name of the command. generator should be a list, then you don't need to wrap it in another list.
async def bulk_generator (get_args):
outputlist = []
generator = ["/bin/generator/./Generator.exe", "--direth", "-w", "0000:84:00.1", "--", "-c", "500", "-d", "62,598,62,1500,62", "-r", "500", "-g", f"eth+vlan:vlan_stream-id={get_args}+vlan:vlan_stream-id={get_args},span_number_vlan_ids=1+binseqn", "stream-id=100,eth_src=11:22:33:44:55:66"]
print(generator)
p = subprocess.Popen(generator, stdout=subprocess.PIPE)
output = str(p.communicate())
outputlist.append(output)
return outputlist
it’s because you’re passing the whole command with it’s arguments. Only include the executable as the first argument than have the second parameter be a list of all the arguments you want to pass for the executable

subprocess.check_output Showing [WinError 2] The system cannot find the file specified

I've had this error for the past 2 or 3 days and I have no clue what I've done wrong or how to fix it
I'm starting to think it's not even the code problem its just my PC
because this was working the other day but no it's not
all it is trying to do is get my HWID then verify my HWID is in the Pastebin
I am on windows 11
and using python and subprocess
here is the error
File "C:\Users\myname\Desktop\python-stuff\dark\Dark.py", line 33, in <module>
hardwareid = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')[1].strip()
File "C:\Users\myname\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 420, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "C:\Users\myname\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\myname\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\myname\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
and here is the code
hardwareid = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')[1].strip()
site = requests.get('https://pastebin.com/raw/hMX2AiWf')
try:
if hardwareid in site.text:
pass
else:
os.system("cls & title cls & title Dark - HWID Error")
print()
print(f' {Fore.RED}[{Fore.LIGHTCYAN_EX}ERROR{Fore.RED}] HWID Not In Database.')
print(f' {Fore.RED}[{Fore.LIGHTCYAN_EX}HWID{Fore.RED}]: ' + hardwareid)
print(f' {Fore.RED}[{Fore.LIGHTCYAN_EX}Administrator{Fore.RED}] Send HWID To Administrator To Be Whitelisted.')
time.sleep(15)
os._exit(1)
except:
print(f' {Fore.RED}[{Fore.LIGHTCYAN_EX}ERROR{Fore.RED}] FAILED to connect to database')
time.sleep(5)
os._exit(1)
I have tried adding "shell=True" to the code but then I get this error
'wmic' is not recognized as an internal or external command,
operable program or batch file.
Traceback (most recent call last):
File "C:\Users\myname\Desktop\python-stuff\HWID\HWID.py", line 18, in <module>
hardwareid = subprocess.check_output('wmic csproduct get uuid', shell=True).decode().split('\n')[1].strip()
File "C:\Users\myname\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 420, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "C:\Users\myname\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 524, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'wmic csproduct get uuid' returned non-zero exit status 1.
I really really need help
I will pay if this gets fixed
Please, someone, help me
wmic has been deprecated according to the docs and may have been removed in some windows update (don't have 11 myself so can't confirm).
The WMI command-line (WMIC) utility is deprecated as of Windows 10, version 21H1 ...
With the intended interface now being powershell's CIM cmdlets. For the UUID, Get-CimInstance can be used with the Win32_ComputerSystemProduct class, from python this can be done by passing the command to powershell.exe like so:
>>> subprocess.check_output(
[
"powershell.exe",
"Get-CimInstance -Class Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID",
]
)
b'00000000-0000-0000-0000-D8CB8AC79XXX\r\n'
You can also pass the text argument to check_output to get a string directly instead of decoding it yourself
>>> subprocess.check_output(
[
"powershell.exe",
"Get-CimInstance -Class Win32_ComputerSystemProduct | Select-Object -ExpandProperty UUID",
],
text=True,
)
'00000000-0000-0000-0000-D8CB8AC79XXX\n'

Python3 / subprocess.check_output / Timeout does not work for every command

I'm running Debian 10.8 with Python 3.7.3 and running a subprocess inside a script which I would like to interrupt after some seconds. This works for cmd_1 but not for cmd_2 in the example below (timeout is never triggered):
import subprocess
import os, sys
# collect proxies and verify
try:
# cmd_1 = "while true; do sleep 1; done"
cmd_2 = "proxybroker find --types HTTPS -l 1000 --outfile proxybroker.txt"
timeout_sec = 3
subprocess.check_output(cmd_2, shell=True, timeout=timeout_sec)
except Exception as e:
print(e)
If I run cmd_2 in a bash, it works fine.
I did install the proxybroker package with pip3 install proxybroker.
On another system running Ubuntu 21.04 the timeout works for both commands. Thanks if someone can provide a hint.
I can't precisely explain why this happens, but getting rid of the gratuitous shell fixes it.
tripleee#debian-buster-docker$ cat >subn.py
import subprocess
# import os, sys # unused import
# collect proxies and verify
try:
subprocess.check_output(['proxybroker', 'find', '--types', 'HTTPS', '-l', '1000', '--outfile', 'proxies.txt'], timeout=3)
except Exception as e:
print(e)
^D
tripleee#debian-buster-docker$ python3 subn.py
Command '['proxybroker', 'find', '--types', 'HTTPS', '-l', '1000', '--outfile', 'proxies.txt']' timed out after 3 seconds
I notice that your original attempt does print the timeout message as part of the traceback when I interrupt it with a KeyboardInterrupt.
tripleee#debian-buster-docker$ python3 sub.py
^CTraceback (most recent call last):
File "/usr/lib/python3.7/subprocess.py", line 474, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/lib/python3.7/subprocess.py", line 939, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.7/subprocess.py", line 1682, in _communicate
self._check_timeout(endtime, orig_timeout)
File "/usr/lib/python3.7/subprocess.py", line 982, in _check_timeout
raise TimeoutExpired(self.args, orig_timeout)
subprocess.TimeoutExpired: Command 'proxybroker find --types HTTPS -l 1000 --outfile proxybroker.txt' timed out after 3 seconds
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sub.py", line 9, in <module>
subprocess.check_output(cmd_2, shell=True, timeout=timeout_sec)
File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/usr/lib/python3.7/subprocess.py", line 477, in run
stdout, stderr = process.communicate()
File "/usr/lib/python3.7/subprocess.py", line 939, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.7/subprocess.py", line 1681, in _communicate
ready = selector.select(timeout)
File "/usr/lib/python3.7/selectors.py", line 415, in select
fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt
As a workaround, timeout as shell command can be used:
cmd_2 = "timeout 3s proxybroker find --types HTTPS -l 1000 --outfile proxybroker.txt"

Find some specific words from terminal's output - Python

I'm gonna write a script and need to check the output to see if it was successful.
For example:
I want the script to find some specific words in terminal's output, let say words "password" and "key.txt"
I use subprocess.check_output but I get errors.
What is wrong with my code? How to fix it?
This is my code:
import subprocess
cmds=[]
# Add the command
cmds.append("ls -lah")
# The output
results=[]
# Execute the command
for cmd in cmds:
results.append(subprocess.call(cmd, shell=True))
# Check the terminal's output and print "Successful"
# if there is a specific word in the output
res = subprocess.check_output(['password', 'key.txt'])
if res in cmds:
print("SUCCESSFUL")
else:
print("NO SUCCESS")
And This is the error that I get:
Traceback (most recent call last):
File "test.py", line 17, in <module>
res = subprocess.check_output(['password', 'key.txt'])
File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/usr/lib/python3.7/subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'password': 'password'
I've found how it works:
import subprocess
from subprocess import check_output
kword = ('password')
# check the output
result = check_output(['ls', '-l'])
print(result)
# Show if it was successful or not
if kword in result:
print("*************** SUCCESSFUL ***************")
else:
print("*************** NO SUCCESS ***************")

Python SSH Commands

I have the following code :
def executeRemoteCommand(host, command):
cmd = "ssh " + host + " \'" + command + "\'"
print cmd
subprocess.check_call(cmd)
When I run the command:
java -Xss515m -Xms48g -Xmx48g -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar /local/experiments/helloworld/ro/server.jar /local/experiments/helloworld/ro/properties.json
using the function above, I get the following error
File "./util/ssh_util.py", line 85, in executeRemoteCommand
subprocess.check_call(cmd)
File "/usr/lib/python2.7/subprocess.py", line 506, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory.
However, when I type in the call directly in the command line, it works fine.
ssh foo92 'java -Xss515m -Xms48g -Xmx48g -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar /local/experiments/helloworld/ro/server.jar /local/experiments/helloworld/ro/properties.json'
Does anyone have any idea?
You need to set shell=True to execute that command through a shell:
subprocess.check_call(cmd, shell=True)
Rather than push this through a shell, you can execute it directly if you pass in the arguments as a list; that way you don't have to worry about quoting the command either:
def executeRemoteCommand(host, command):
subprocess.check_call(['ssh', host, command])
Note that both host and command here are single arguments passed to ssh. Normally, that is how the shell would pass the arguments into the ssh command, that's what the quoting around the java ... command line is for.

Categories