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

#49 dargueta

dargueta

    I chown trolls.

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

Posted 30 July 2009 - 01:12 PM

Well, for starters you didn't make the fix I mentioned earlier with prefixing 's' to the regex. And it looks like you're running a more recent version of Perl than I am. My fail.

#!/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 =~ s/__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;
}

  • 0

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


#50 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 30 July 2009 - 11:22 PM

Hi

Thanks, from the looks of things, it'll work now.

I'll try tomorrow, as I don't have the script, scripting engine, csv file or Visual Studio with me at the moment.

Thanks once again,

Panarchy
  • 0

#51 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 31 July 2009 - 06:06 PM

Hi

Unfortunately, the script still doesn't work.

A lot of output, so I've attached it into a logfile within this post.

Please take a look at it. If you'd like me to use a different version of Perl, please tell me which version.

Thanks in advance,

Panarchy

Attached Files


  • 0

#52 dargueta

dargueta

    I chown trolls.

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

Posted 31 July 2009 - 07:49 PM

1) The deprecated split error was my fault, I forgot to change =~ back to =
2) You changed the <VNC.cpp> back. It should be <CPPTEMPLATE>.
3) I don't understand why some variables aren't getting initialized. I did it the long way (now it works), but it used to the other way before. Dunno, maybe they changed it in the newest release of Perl, which I updated to.

I don't know if this'll work because I don't have the compiler or linker installed on my system, but the rest seems to work just fine.

#!/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);
@ipfile = <IPFILE>;
#for each line in the ip file...
$i = 0;
$total = 0;
foreach(@ipfile)
{
	#strip leading and trailing spaces off the line
	my $thisline = $_;
	chomp($thisline);
	#use regular expressions to pull the ip address from the line.
	($currhost,$currip) = split(/\,/,$thisline);

    #remove leading and trailing whitespace
    $hostlist[$i] = trim($currhost);
    $iplist[$i] = trim($currip);
	++$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 = <CPPTEMPLATE>;

    #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 =~ s/__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(@_);
    if(not defined $str){ return ''; }
    $str =~ s/^\s+//;
    $str =~ s/\s+$//;
    return $str;
}

  • 0

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


#53 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 01 August 2009 - 12:38 AM

Thanks dargueta, really appreciate it.

I'll test it on Monday.

Panarchy

PS: I didn't change the script at all?
  • 0

#54 dargueta

dargueta

    I chown trolls.

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

Posted 01 August 2009 - 07:10 AM

I think you changed it and then uploaded the unchanged version or something, I dunno.
  • 0

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


#55 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 01 August 2009 - 05:39 PM

Nope...

Well anyway, pretty sure this latest script of yours will work, so I'll just give that a go on Monday.
  • 0

#56 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 04 August 2009 - 04:50 PM

Hi

I've got 300+ .cpp files now, with the correct names. However, when opening the .cpp files, the IP Address didn't go through :(

I've also got about half that in .exe.manifest & .obj files.

Nothing's been completed compiled.

Please fix the script.

Thanks in advance,

Panarchy
  • 0

#57 dargueta

dargueta

    I chown trolls.

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

Posted 04 August 2009 - 08:30 PM

The IP addresses worked fine for me...are you sure you spelled the replacement token right?
  • 0

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


#58 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 04 August 2009 - 10:53 PM

I kept the everything identical to your script.

In fact, I changed my files [CSV & the entire C++ project] to reflect the names you used.

I ran the script from the correct Command-Prompt.
  • 0

#59 dargueta

dargueta

    I chown trolls.

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

Posted 05 August 2009 - 04:03 AM

Weird. It worked just fine for me. What I meant was, did you put the token __PUTIPHERE__ everywhere you needed the IP address in your template file, and leave __PUTIPHERE__ alone in the script? You shouldn't need to touch anything in the script except the file names (leave the < before the file name, it means "open for reading"), and the two compile/link commands. (Leave the backticks in, though.)


EDIT: Looking at your compile/link commands, I think you might have to change main.obj to $currhost.obj. I'm not sure, as I don't know how your compiler works.

Edited by dargueta, 05 August 2009 - 04:09 AM.
Forgot info

  • 0

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


#60 Panarchy

Panarchy

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 247 posts

Posted 05 August 2009 - 05:08 AM

^Hmm... I'll try with that tomorrow.

Thanks

Panarchy

BTW: I'm just using the 90-day trial of Visual Studio 2008 Professional. But there's also the express edition [free], which uses, what I believe to be, the same compiler. Perhaps if we still can't get it to work after this, you could install it?
  • 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