I'm making a script that runs a command line and then shows the output in flask webpage.
The problem I'm facing is that the output is malformed, it looks like this.
img
here's my what I have wrote so far:
import subprocess as sp
#app.route('/harvester/scan',methods=['GET','POST'])
def harv():
domain=request.values.get('domain')
outputt=sp.getoutput("theharvester" + " " + "-d" + " " + domain + " " + "-l 10 -b all" )
return outputt
Function subprocess.getoutput is a legacy one. Try using subprocess.run instead.
Here is the example:
import subprocess
command = 'printf "1\n2"'
out = subprocess.run(command, capture_output=True).stdout.decode()
Related
I build a script to remove Windows 10 Apps per Python.
I saved the apps to remove in a String Array and save the complete command in a variable.
Then I run the command, come a Error with: The Remove-AppxPackage command is either misspelled or
could not be found.
And I have coded following code:
win10Apps = ["3d", "camera"]
for app in win10Apps:
psCommand = "Get-AppxPackage " + app + " | Remove-AppxPackage"
pyautogui.press("Enter")
os.system("powershell.exe " + psCommand)
pyautogui.press("Enter")
As JosefZ mentioned in the comments, you have to format your parameter when calling other executable.
The fixed code looks like this:
win10Apps = ["3d", "camera"]
for app in win10Apps:
psCommand = "Get-AppxPackage " + app + " | Remove-AppxPackage"
pyautogui.press("Enter")
os.system('powershell.exe -c "{}"'.format( psCommand))
pyautogui.press("Enter")
Also for special characters you need to escape. Also, here is the documentation for Get-AppxPackage and for Remove-AppxPackage.
I have written basic port scanner for target ip and when I run it through kali vm it says sh: 1: nmap-F192.168.234.135: not found. but when I run nmap -F 192.168.234.135 ... its perfectly working. Can anyone point out the reason behind it. thanks
import os
def get_nmap(options,ip):
command = "nmap" + options + "" + ip
process = os.popen(command)
result = str(process.read())
return result
print(get_nmap('-F','192.168.234.135'))
Better, using the subprocess module:
def get_nmap(options, ip) :
return subprocess.check_output(["nmap", options, ip])
#end get_nmap
You need to add spaces in the command string. Change it to
command = "nmap " + options + " " + ip
I'm trying to execute a command using Popen and search through the output using a regex to find a UserID. When I run the code below and have if reg_ex.search(line) code block commented out, the command runs successfully (although I don't have my userID). As soon as I uncomment out the regex code, the command itself fails.
Any ideas why this regex if statement makes the command itself fail when I'm just iterating through output and any idea how to get the userId I need from the output? I've already confirmed the regex itself works when run separately (although for those that want it, the output line for regex will look like this u'userId': u'userID_in–question'}.
import re
import subprocess
#command I want to run
command = "add-user " + FirstName + " " + LastName + " " + Email + " " + Phone
#Regex to find userId in output
reg_ex_find = re.compile('userId')
reg_ex_replace = re.compile("(""u'userId""|\s|:|}|')")
#call command
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
for line in iter(process.stdout.readline, b''):
print line
if reg_ex_find.search(line):
UserId = line
reg_ex_replace.sub('',UserId)
process.stdout.close()
process.wait()
print process.returncode
The following code does not work properly in Python. The problem is that the output does not get redirected to output by using >:
command = toolpath + " " + query + " " + number + " > " + output;
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE);
output = process.communicate()[0];
The command is actually the following if you print it:
./subsetA.pl ./remainingqueries.fasta 100 > ./tmpQuery.fasta
So the perl script subsetA.pl takes two arguments and writes it to stdout which gets redirected to tmpQuery.fasta. But tmpQuery.fasta is empty after the command gets called.
If I run it direct on CLI then it works perfectly.
You don't need the shell's output redirection operator with Popen; that's what the stdout argument is for.
command = [toolpath, query, number]
with open(output) as output_fh:
process = subprocess.Popen(command, stdout=output_fh)
Since you are calling communicate to get the standard output, though, you don't want to redirect the output at all:
command = [toolpath, query, number]
process = subprocess.Popen(command, stdout=subprocess.PIPE)
output = process.communicate()[0]
You can try
command = toolpath + " " + query + " " + number + " > " + output;
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE,Shell=True);
output = process.communicate()[0];
Both current answers don't work!
This works (tested on Python 3.7):
subprocess.Popen(['./my_script.sh arg1 arg2 > "output.txt"'],
stdout=subprocess.PIPE, shell=True)
Note:
You do not need split or array in Popen.
BOTH stdout and shell parameters are needed.
I'm trying to use a PHP file on a server to transmit some variables into a Python script which will in turn start a raspistill timelapse on my Raspberry Pi.
I've so far managed to start taking pictures but I'd now like to have a button to kill the timelapse - i've tried many methods including .kill() and .terminate() but cant get it working.
Here is my current python code:
import sys, os, time, datetime
import subprocess
import signal
from time import sleep
tlfreq = int(sys.argv[1])
tltime = int(sys.argv[2])
dir = '/var/www/timelapse/' + sys.argv[3]
if not os.path.exists(dir):
os.makedirs(dir)
cmd = ('raspistill -t ' + str(tltime) + " -tl " + str(tlfreq) + " -o " + dir + "/photo_%04d.jpg")
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
print "Pictures are now being taken every" , tlfreq/1000 , "second/s for a total of", tltime/3600000 , "hours. These are being stored in", dir
Perhaps I need an "if variable = 1 then kill" command and then send the variable to python.
Any help would be greatly appreciated!
Many thanks,
Dan
You can create new python script kill_raspystill.py with this code
import os
os.system("pkill raspistill")
and call that script when you press a button.
I would suggest the signal library: http://docs.python.org/2/library/signal.html