2

I am attempting to call netcat/nc from inside a loop, passing the lcv as the last octet in the command. I am seem to be running into an issue due to the fact that the variable is in the middle of the command. What I would like to do is:

for i in 1 to 50
   print i
   nc -nvv 192.168.1.i 21 -w 15 >> local_net.txt

So far I have tried:

os.system("nc -nvv 192.168.1.",i,"21 -w 15 >> local_net.txt")

and

subprocess.call(["nv","-nvv 192.168.1.",i,"21 -w 15 >> local_net.txt")

Is there an easier way to reference an LVC from within a command executing a system command?

1
  • I'm not sure what you mean by either "lcv" or "LVC". Are they the same thing? Commented Sep 16, 2013 at 3:02

1 Answer 1

7

The reason this doesn't work:

os.system("nc -nvv 192.168.1.",i,"21 -w 15 >> local_net.txt"

… is that os.system takes a single string, as a shell command. You're giving it three separate arguments. (Plus, even if this worked as you hoped, it would have to either put a space before the i or not put one after the i.) You could fix it by any of the usual ways of combining strings:

os.system("nc -nvv 192.168.1.{} 21 -w 15 >> local_net.txt".format(i))

os.system("nc -nvv 192.168.1." + str(i) + " 21 -w 15 >> local_net.txt".format(i))

However, your intuition to try subprocess.call instead was a good one. The reason this doesn't work:

subprocess.call(["nv","-nvv 192.168.1.",i,"21 -w 15 >> local_net.txt")

… is two-fold.

First, you're joining up multiple arguments. This is equivalent to the shell command:

nc '-nvv 192.168.1.' '1' '21 -w 15 >> local_net.txt'

When what you want is the equivalent to these:

nc -nvv 192.168.1.1 21 -w 15 >> local_net.txt
nc '-nvv' '192.168.1.1' '21' '-w' '15' >> 'local_net.txt'

You need to split up the arguments properly to get that.

Second, >> local_net.txt isn't part of the command itself at all; it's a shell redirection directive. You can't do that without the shell.

If you really want to use the shell, you can do it the same way as with system, but putting together a single single:

subprocess.call("nc -nvv 192.168.1.{} 21 -w 15 >> local_net.txt".format(i), shell=True)

But a better way to do this is to break the arguments up properly, and do the redirection Python-style instead of shell-style:

with open('local_net.txt', 'a+b') as f:
    subprocess.call(["nc", "-nvv", "192.168.1.{}".format(i), "21", "-w", "15"],
                    stdout=f)

In the file mode, the a means to append to the end of the file; the + means to update the file if it exists, create it otherwise; the b means to open it in binary mode (so you don't translate newlines, if this is Windows—I'm assuming Python 2.x here).


Meanwhile, the Python version of for i in 1 to 50 is for i in range(1, 51):. (Note that 51 at the end, because Python ranges are half-open.) So, putting it all together:

for i in range(50):
    with open('local_net.txt', 'a+b') as f:
        subprocess.call(["nc", "-nvv", "192.168.1.{}".format(i), "21", "-w", "15"],
                        stdout=f)

It's worth noting that you can do this in most sh-like shells, including bash, without needing Python at all. And for something this simple, it may be easier:

for i in {1..50}; do
    nc -nvv 192.168.1.${i} 21 -w 15 >> local_net.txt
done
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, not only do you have multiple solutions. You'ev explained what I was doing wrong. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.