I wrote a port scanner in python years ago and i'd like to start using it in C++ i just dont know how i can translate it from python to C++
Python code :
#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime
def menu() :
print """
1)Find Ip
2)Port Scan
3)exit
"""
choices = raw_input("Please choose one of the following: ")
if choices == "1":
findIP()
if choices == "2" :
ipScan()
if choices == "3" :
exit()
def findIP() :
web_address = raw_input("Enter the name of the website you want a ip-address for: ")
if web_address == web_address :
subprocess.call(["nslookup", web_address])
choice = raw_input("would you like to go back to the menu? Y/N: ")
if choice == "y" :
menu()
else:
exit()
def ipScan() :
// clears the screen
subprocess.call("clear", shell = True)
// asks user for a Url
openports = []
remoteServer = raw_input("Enter a Ip-address to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)
// athestic use for a banner with the information inside : P
print "-" * 60
print "Please wait, scanning Webserver", remoteServerIP
print "-" * 60
// checks the time the scan started
t1 = datetime.now()
//in range specifies my ports and scans all ports between 1 and 1025
// error handling to make it robust by catching errors!!
try :
for port in range(1, 1025) :
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, port))
if result == 0 :
print "port {}: \t Open".format(port)
openports.append(port)
except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit()
except socket.error :
print "Couldnt connect to server."
sys.exit()
// checks the time again
t2 = datetime.now()
//calcs the difference in time to see how long it took to run the scan
total = t2 - t1
// prints the information so we can see it
print "Scanning completed in: ", total
print "The Open Ports Are %s" % (openports)
versions = raw_input("Would you like the versions of the open ports? Y/N: ")
if versions == "y":
web_address = raw_input("Enter ip-address: ")
if web_address == web_address :
subprocess.call(["nmap", "-sSV", web_address])
with a hardware tap and internet url stuff, its not going to translate so much as be a total rewrite, and it may depend on what OS you want to use it on what library you need to do those things.
your best bet is to reverse this back into requirements in English and then write a c++ program from those, rather than any sort of attempt to translate it.