Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python os.system error: "global name 'output' is not defined"

Newbie here. any help would be appreciated..

I am writing a cgi script that runs a tcp pcap diagnostic tool. if I would run the command in bash it would look like:

/home/fsoft/cap/capnostic -r 38350 /home/fsoft/brad.pcap > 38350

So I am trying to do it in python:

output = os.system('/home/fsoft/cap/capnostic -r' + port + directory+filename '>' + jobdir+filename

I have a feeling the '>' is messing things up.. But I can't seem to find the right syntax.. Also once I get the command correctly will I just be able to print the output variable?

 print '%s' % (output)

the output may be 3 pages of data..

Thanks for the help.

Here is my full code:

#!/usr/bin/env python

import cgi, os
import cgitb; cgitb.enable()
import subprocess


form = cgi.FieldStorage()
port = form.getvalue("port")
filename = form.getvalue("filename")
directory = form.getvalue("directory")
jobdir = '/var/www/jobs/' + filename


def createdir():
 os.system('mkdir /var/www/jobs/' + filename)
createdir()

def capout():
 output = os.system('/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, directory,     filename, jobdir, filename))
capout()

def htmlout():
 print 'Content-type: text/html\n'
 print '<html>'
 print '<head>'
 print '<title>Capnostic Output</title>'
 print '</head>'
 print '<body>'
 print '<BR><BR><BR><center>'
 print '<table border=0>'
 print '<TR>'
 print '<TD><center>port = %s<BR>filename = %s<BR>Directory = %s<BR>Job Directory = %s</TD>' % (port,filename,directory,jobdir)
 print '</TR>'
 print '</table>'
 print '<BR><BR><BR>'
 print '%s' % (output)
 print '</body>'
 print '</html>'

htmlout()

It is now telling me:

<type 'exceptions.NameError'>: global name 'output' is not defined 
  args = ("global name 'output' is not defined",) 
  message = "global name 'output' is not defined"
like image 315
Cade Nelson Avatar asked May 24 '26 00:05

Cade Nelson


1 Answers

You are missing a + for concatenating your string and the spaces between your strings. You can use string formatting to simplify the task or just add a + and spaces where needed:

output = os.system('/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, 
                   directory, filename, jobdir, filename))

Note: %s is used to treat each variable as a string.

Usage of os.system is replaced with the subprocess module:

sts = os.system("mycmd" + " myarg")
# becomes 
sts = call("mycmd" + " myarg", shell=True)

To capture the output, you will want to use Popen, which would be translated as follows:

def capout():
   cmd = '/home/fsoft/cap/capnostic -r %s %s%s > %s%s' % (port, 
                       directory, filename, jobdir, filename)
   process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   output, error = process.communicate()
   return output

output = capout()
like image 118
Robert Avatar answered May 25 '26 15:05

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!