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 ***************")
Related
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
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'
My code:
for host in hostlist_split:
path = currentDIR + "/" + scriptRESULTS + runtimeSCRIPT
if os.path.exists(path):
print 'Executing Script...'
subprocess.check_call(['path', 'username', 'password'])
print(stdout)
sitesProcessed += 1
Script reads list of hosts from a file, iterates through them running another script on that host passing the required arguments for it to run, and then should ideally store the output of that subscript in a new file.
I'll name the file after the host the script ran on and store it in a directory named after the subscript.
Problem is I'm getting the message no such file or directory when executing this code.
I've verified the host is being read correctly and is stored in the list as a string.
I've verified the path to the subscript, the username, and the password are all being stored and supplied as expected.
But when using the check_call function the script will not run, and the stdout is not printed.
Error:
Traceback (most recent call last):
File "./pywrapper.py", line 136, in <module>
subprocess.check_call(['path', 'username', 'password'])
File "/usr/lib64/python2.7/subprocess.py", line 537, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
My script wasn't expanding the variables it was just reading them as strings.
I just switched from BASH so figuring out how to combine strings and variables in single line without adding spaces took me a while.
This is what I'm running now..
for host in hostlist_split:
path = currentDIR + '/' + scriptRESULTS + runtimeSCRIPT
if os.path.exists(path):
print 'Executing Script...'
sshARGS = ('{}/{} {} {} {} > {}/{}'.format(currentDIR, runtimeSCRIPT, host, username, password, path, host) )
SSH = subprocess.Popen(sshARGS, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = SSH.communicate()
SSH.wait()
sitesProcessed += 1
I'm trying to send a command line to command prompt (terminal) and read the output but I keep receiving the error:
self get_version(self)
File <folder path of my script>, line 39, in get_version
stdout = subprocess.PIPE
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Here's my code:
import subprocess
def get_version(self)
command = "wmic datafile where name='c:\\Drivers\\current_version\\GenericDriverSetup.exe' get version"
proc = subprocess.Popen([command],
stdout=subprocess.PIPE)
stdout_value = proc.communicate()[0]
print '\tstdout:', repr(stdout_value)
Can someone tell me what's wrong with this? Thanks so much
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.