Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

HELP: Script to gather IP Address from .RDP then compile to .exe for VNC

authentication

  • Please log in to reply
69 replies to this topic

#37 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 26 July 2009 - 07:31 PM

Thanks for your reply.

Still having trouble fully understanding the code.

#!/usr/bin/env perl

use strict;

my (@iplist,$template,$hostname,$currip) = ((),'','','');

#open ip list file
open(IPFILE,"<iplist.csv") or die "Failed to open IP list file.";
#read all lines into array
my (@ipfile,$i) = ((),0);
@ipfile = <IPFILE>;
#for each line in the ip file...
foreach(@ipfile)
{
	#strip leading and trailing spaces off the line
	chomp($_);
	#use regular expressions to pull the 
	#ip address from the line. I have to
	#check to see if the regex will do
	#what it's supposed to.
	($hostname,$iplist[$i]) =~ split(/,/);
	chomp($iplist[$i]);
	++$i;
}
#close ip list file
close(IPFILE);

#open template file
open(CPPTEMPLATE,"<VNC.cpp") or die "Failed to open template file.";

#load entire file into single string...
#undefine end-of-record variable, which is \n by default. this will result
#in the file being divided up into an array of lines instead of one single
#string like we want.
undef $/;

#read the file into $template.
$template = VNC.cpp;

#reset $/ to avoid problems.
$/ = "\n";

#for each ip address in the list...
foreach(@iplist)
{
	#get the current ip
	$currip = $_;
	#open the output file
	open(CPPOUT,">$currip.cpp") or die "Failed to open $currip.cpp for writing.";
	#replace all instances of __PUTIPHERE__ in the template file with the
	#current ip address.
	$template =~ /__PUTIPHERE__/$currip/g;
	#print the result to the output file.
	print CPPOUT $template;
	#close the output file
	close(CPPOUT);
}

#close template file
close(CPPTEMPLATE);

#exit
1;

I fully understand the above, however I can't work out how to put it into practice, loop & all.

Perhaps some examples of similar implementations/scripts could be shown?

Thanks in advance,

Panarchy
  • 0

#38 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 27 July 2009 - 04:17 PM

You just need to pass the script two files on the command line; the first is the IP list file that has entries like so:

google.com,192.168.1.151
forum.codecall.net,14.194.12.52

The second is your source code file, with the symbol __PUTIPHERE__ wherever you want the IP address. By the way, the line

#read the file into $template.
$template = VNC.cpp;

should read

#read the file into $template.
$template = <CPPTEMPLATE>;

I don't remember writing that...did you change it?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#39 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 28 July 2009 - 01:21 AM

Thanks, and yes I did put that there...

I thought it needed to be replaced?

Am a little confused...

Anyway, the cpp name is 'VNC.cpp' and the IP List name is 'ip-list.csv'...

Please complete the script.

Thanks in advance,

Panarchy
  • 0

#40 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 28 July 2009 - 04:58 AM

The <> operator is Perl's equivalent of calling ifstream::getline. The identifier in between the brackets is the file handle. It's complete, Panarchy. Just put it in the same directory as all your files and run it.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#41 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 28 July 2009 - 10:21 PM

Oh!!!

Thanks, I'll give it a go.

Hopefully this line doesn't need correcting;

#open template file
open(CPPTEMPLATE,"<VNC.cpp>") or die "Failed to open template file.";

  • 0

#42 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 28 July 2009 - 10:32 PM

Unfortunately the script still doesn't work. :(

Thanks, however there are still some missing features.

To be clearer, I have listed them in 4 steps;


Step 1. Process IP Address from CSV
Step 2. Process Name from CSV
Step 3. Input IP Address into VNC.cpp
Step 4. Compile with Name



It seems that Step 4 has been left out... please include it.

To confirm, Step 4 should correspond the following;
[B]cl[/B] /clr "super.cpp"
[B]link[/B] super-resource.res main.obj /[B]out[/B]:"%name%".exe


Please help complete the script.

Thanks in advance,

Panarchy

Edited by Panarchy, 29 July 2009 - 07:46 PM.

  • 0

#43 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 29 July 2009 - 06:06 PM

Change this:
open(CPPTEMPLATE,"<VNC.cpp>") or die "Failed to open template file.";

To this:
open(CPPTEMPLATE,"<VNC.cpp") or die "Failed to open template file.";

...and change this:
$template =~ /__PUTIPHERE__/$currip/g;

to this:
$template =~ s/__PUTIPHERE__/$currip/g;

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#44 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 29 July 2009 - 07:45 PM

Thanks.

The script now runs successfully... however...

It doesn't work. This may make things a little clearer;

Posted Image

Please complete this script.

Thanks in advance,

Panarchy

EDIT: Althought the screenshot doesn't show it, I ran the script in between. It didn't say 'ran successfully' or anything like that, but didn't give errors.
  • 0

#45 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 29 July 2009 - 08:18 PM

I'm confused.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#46 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 29 July 2009 - 08:30 PM

Thanks, however there are still some missing features.

To be clearer, I have listed them in 4 steps;


Step 1. Process IP Address from CSV
Step 2. Process Name from CSV
Step 3. Input IP Address into VNC.cpp
Step 4. Compile with Name



It seems that Step 4 has been left out... please include it.

To confirm, Step 4 should correspond the following;
[B]cl[/B] /clr "super.cpp"
[B]link[/B] super-resource.res main.obj /[B]out[/B]:"%name%".exe


Thanks in advance,

Panarchy
  • 0

#47 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 29 July 2009 - 08:39 PM


#!/usr/bin/env perl



use strict;

use warnings;

use POSIX;



#declare whitespace-trimming function defined later

sub trim($);



my (@iplist,@hostlist,$template,$hostname,$currip,$currhost) = ((),(),'','','','');



#open ip list file

open(IPFILE,"<iplist.csv") or die "Failed to open IP list file.";

#read all lines into array

my (@ipfile,$i,$total) = ((),0,0);

@ipfile = <IPFILE>;

#for each line in the ip file...

foreach(@ipfile)

{

	#strip leading and trailing spaces off the line

	chomp($_);

	#use regular expressions to pull the 

	#ip address from the line. I have to

	#check to see if the regex will do

	#what it's supposed to.

	($hostlist[$i],$iplist[$i]) =~ split(/,/);

        #remove leading and trailing whitespace

        $hostlist[$i] = trim($hostlist[$i]);

        $iplist[$i] = trim($iplist[$i]);

	++$i;

        ++$total;

}

#close ip list file

close(IPFILE);



#open template file

open(CPPTEMPLATE,"<VNC.cpp") or die "Failed to open template file.";



#for each ip address in the list...

for($i = 0; $i < $total; ++$i)

{

    #load entire file into single string...

    #undefine end-of-record variable, which is \n by default. this will result

    #in the file being divided up into an array of lines instead of one single

    #string like we want.

    undef $/;



    #read the file into $template.

    $template = <VNC.cpp>;



    #reset $/ to avoid problems.

    $/ = "\n";



    #get the current ip & host

    $currip = $iplist[$i];

    $currhost = $hostlist[$i];

    #open the output file

    open(CPPOUT,">$currhost.cpp") or die "Failed to open $currhost.cpp for writing.";

    #replace all instances of __PUTIPHERE__ in the template file with the

    #current ip address.

    $template =~ /__PUTIPHERE__/$currip/g;

    #print the result to the output file.

    print CPPOUT $template;

    #close the output file

    close(CPPOUT);

    #compile

    `cl /clr $currhost.cpp`;

    `link super-resource.res main.obj /out:$currhost.exe`;

    #rewind to beginning of template file to read again

    seek(CPPTEMPLATE,0,SEEK_SET);

}



#close template file

close(CPPTEMPLATE);



#exit

1;



####

sub trim($)

{

    my $str = shift;

    $str =~ s/^\s+//;

    $str =~ s/\s+$//;

    return $str;

}




Edited by dargueta, 29 July 2009 - 09:25 PM.

  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#48 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 29 July 2009 - 10:49 PM

Thanks a heap!

Looks pretty much complete now!

Hate to disappoint you, but I'm getting this error;

Posted Image

Please fix!

I feel that it's very close to completion!

Thanks,

Panarchy
  • 0





Also tagged with one or more of these keywords: authentication

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download