Created
July 23, 2012 12:45
-
-
Save AlexsJones/3163452 to your computer and use it in GitHub Desktop.
port scanner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <sstream> | |
#include <netdb.h> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <cstdio> | |
#include <vector> | |
using namespace std; | |
int current_port = 0; | |
void scan_open_ports(string hostname, vector<int>* ret_val) | |
{ | |
int _socket = socket(AF_INET, SOCK_STREAM, 0); | |
if(_socket < 0) { cout << "Error opening socket..." << endl; return;} | |
struct hostent *target_server = gethostbyname(hostname.c_str()); | |
struct sockaddr_in target_address; | |
if(target_server == NULL) { cout << "Error no such host exists" << endl; return;}; | |
bzero((char*)&target_address, sizeof(target_address)); | |
target_address.sin_family = AF_INET; | |
bcopy((char*)target_server->h_addr, (char*) &target_address.sin_addr.s_addr,target_server->h_length); | |
target_address.sin_port = htons(current_port); | |
if(connect(_socket, (struct sockaddr*) &target_address,sizeof(target_address)) < 0) | |
{ | |
//something went wrong... | |
close(_socket); | |
cout << "Cannot connect to " << hostname << " on port " << current_port << endl; | |
++current_port; | |
scan_open_ports(hostname, ret_val); | |
} | |
cout << "Established connection on port " << current_port << endl; | |
ret_val->push_back(current_port); | |
++current_port; | |
close(_socket); | |
scan_open_ports(hostname,ret_val); | |
} | |
vector<int>* scan_ports(string input) | |
{ | |
vector<int>* ret_val = new vector<int>(); | |
scan_open_ports(input, ret_val); | |
} | |
int main(int argc, char **argv) { | |
scan_ports("172.20.141.10"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment