14
  1 import subprocess
  2 raw = raw_input("Filename:").lower()
  3 ip = raw_input("Host:").lower()
  4 cmd = subprocess.call("tcpdump -c5 -vvv -w" + " raw " + " ip ",shell=True)

So this is my script. I everything works besides one key objective, using the raw input. It allows me to input anything i want, but when it goes to saving the file or using an ip/host doe doesn't actually do anything. Sure it gives me the packets, but from the localhost not the host i type in.

how i know this isn't working is cause my first raw input is the filename, so i put in test, when i look in the folder were my script is, it produces a file called "raw" meaning, its not actually taking my input only using whats inside my "X"...

So i make a few chances to come to this:

  1 import subprocess
  2 raw = raw_input("Filename:").lower()
  3 ip = raw_input("Host:").lower()
  4 cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw + "host" + ip,shell=True)

Which is great because it actually calls for the -w but it saves it now as rawhostip instead of "raw"s input. for reference this is what the command looks like in the terminal:

tcpdump -c5 -vvv -w savename host wiki2

the only two variabls are savename and wiki2 the rest are needed for the command to work.

with this script i get this error:

import subprocess
raw = raw_input("Filename:").lower()
ip = raw_input("Host:").lower()
cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw, "host" + ip,shell=True)

Error:

Traceback (most recent call last):
  File "te.py", line 4, in <module>
    cmd = subprocess.call("tcpdump -c5 -vvv -w" + raw, "host" + ip,shell=True)
  File "/usr/lib/python2.6/subprocess.py", line 480, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.6/subprocess.py", line 583, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

I am at a lost. Any help will be great, yes I know look at subprocess's doc's on site:X, I have I need a human to teach me, I don't understand what I am reading.

My question is how do I work with these variables.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
John Riselvato
  • 12,854
  • 5
  • 62
  • 89

2 Answers2

28

Don't use shell=True. That should be False.

You are making subtle mistakes with the input. Specifically, if you have two strings:

>>> s1 = 'Hello'
>>> s2 = 'Hi'
>>> s1 + s2
'HelloHi'

Notice, there is no space between Hello and Hi. So don't do this. (Your line 4)

You should do (the good way):

>>> raw = raw_input('Filename: ')
Filename: test
>>> ip = raw_input('Host: ')
Host: 192.168.1.1 
>>> command = 'tcpdump -c5 -vvv -w {0} {1}'.format(raw, ip)   # the command goes here
>>> subprocess.call(command.split(), shell=False)   # call subprocess and pass the command as a list using split

Now it should work.

user225312
  • 126,773
  • 69
  • 172
  • 181
  • 2
    oh gosh Sukhbir! :D Well i haven't tested it yet, but now i finally understand these {0} {1} things put in commands in examples. They are tables/lists right? i knew not to use shell=True, it is a security issue right. I didn't know False wasn't thanks for showing me that. let me test it ill brb – John Riselvato Dec 03 '10 at 18:31
  • Alright i am pretty sure the filename is working, but one thing that is not correct in this is calling the "host" the command with out python is: tcpdump -c5 -vvv -w test host 192.168.1.1 i am using your variables in your example – John Riselvato Dec 03 '10 at 18:35
  • I have just given you the basic outline, the rest is for you to check it out! Experiment! – user225312 Dec 03 '10 at 18:37
  • I'll ill be back in 40 minutes with my progress :D thanks sukhbir – John Riselvato Dec 03 '10 at 18:43
  • This will stop working as soon as `raw` contains spaces. Don't use a command line string at all, rather use a list directly. – Philipp Dec 03 '10 at 18:50
  • The "{0}{1} things" are used for [string formatting](http://docs.python.org/library/stdtypes.html#str.format). More on [format string syntax](http://docs.python.org/library/string.html#formatstrings) – Velociraptors Dec 03 '10 at 19:01
  • alright i think i figured it out but now i am having an issue with my tcpdump its getting hung on looking for anything at "got 0". i don't think tcpdump is working correctly, but thanks for everything. I don't think i need any more help on this question :D – John Riselvato Dec 03 '10 at 19:08
  • I have a question about this. I was reading the way you set it up was for python 2.6+ Which on my server (lenny), you can only get 2.5, or 2.6+ (but its been causing alot of issues cause its unstable). How do i force 2.5 to read this? – John Riselvato Dec 06 '10 at 19:19
24

You should not use the string form ob the subprocess functions. Try:

subprocess.check_call(["tcpdump", "-c5", "-vvv", "-w", raw, "host", ip])
Philipp
  • 48,066
  • 12
  • 84
  • 109