2

I'm trying to create a TCP port scanner in Python that accepts a number of arguments (-all (displays all ports, both open and closed for the target), -open (only displays the open ports on the target), -target (specify target IP, subnet or hostname) and -range (specify port range).

Currently I've only managed to code the options used in the program, my code is as follows:

import optparse
parser = optparse.OptionParser()
parser.add_option('-all', dest='allPorts', help='displays all ports regardless of status')
parser.add_option('-open', dest='openPorts', help='displays only open ports')
parser.add_option('-target', type='string', dest='targetIP', help='specify target IP address, subnet or hostname')
parser.add_option('-range', type='string', dest='portRange', help='specify port range')
(options, args) = parser.parse_args()

I'm unsure how to continue with the program, particularly with the -all / -open options, any help would be greatly appreciated.

asked Feb 6, 2017 at 15:59
3
  • start with getting a filtered sub-list, containing all words for which the condition 'e' not in word is true. Then, you'll need to do some Cartesian multiplication of this list. You'll have N^3 possible strings, where N is number of words after filtering Commented Feb 6, 2017 at 16:06
  • I think the most intuitive way to implemented is to use 3 nested for loops that all run on your filtered words, then just concatenate them and make the "find-replace" operations Commented Feb 6, 2017 at 16:08
  • When trying to crack (brute force) a password taking three random words from your file won't be of much use, you systematically need to try all possible combinations. Also is randomwords.txt the file with the story? If so, you should start by creating a set of candidate words (no duplicates, no words containing e, replace o) Commented Feb 6, 2017 at 16:13

4 Answers 4

3

Normally you use str.join to build a new string from several different strings:

''.join(data[:3])

and to replace o with 0 use str.replace:

''.join(data[:3]).replace('o', '0')

Note that you can get 3 samples with random.sample(data, 3), you don't need to shuffle the complete data:

''.join(random.sample(data, 3)).replace('o', '0')

To exclude words containing "e" you can only keep words that do not contain "e" in the input:

with open('randomwords.txt', 'r') as f:
 # a conditional list comprehension
 data = [word for word in f.read().split() if "e" not in word]
[...]
answered Feb 6, 2017 at 16:05
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

"{}{}{}".format(w1,w2,w3).replace("o","0")
ᴀʀᴍᴀɴ
4,5368 gold badges41 silver badges61 bronze badges
answered Feb 6, 2017 at 16:03

Comments

1

I have modified one answer posted here, Actually i wanted to edit that answer but deleted by the author.
Try Following:

res = ""
for x in data[:3]:
 res += x
 res.replace("o", "0")
print res

OR

res = ""
for x in data[:3]:
 res = res + x
print res.replace("o", "0")

Try Last one.

answered Feb 6, 2017 at 16:05

Comments

1

First, your assignement seems to allow repetitions (e.g. passwords like "passw0rdpassw0rdpassw0rd") while your method doesn't. It is also inefficient. You can use random.choice three times instead.

Concatenation of strings is done with + operator, e.g. concatenation = str1 + str2 + str3, or join function. Replacing o with 0 is done with string class method replace, e.g. concatenation.replace('o', '0').

answered Feb 6, 2017 at 16:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.