Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: String output for system()

  1. #1
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts

    String output for system()

    Hi, i've changed the system() function to return a string of executed command instead of an builtin true/false. This allows to do some interesting stuff like executing external scripts in Perl/PHP etc. and get an output from them.
    Grab it: https://github.com/voron00/libcod/co...c667fb9868f82a
    Here is a little example:
    A geolocation script written in Perl
    PHP Code:
    # List of modules
    use strict;                     # strict keeps us from making stupid typos
    use warnings;                   # helps us find problems in code
    use diagnostics;                # good for detailed explanations about any problems in code
    use Geo::IP;                    # GeoIP is used for locating IP addresses

    my $ip $ARGV[0];

    if (!
    $ip) { die("No IP-Address has been defined"); }

    if (
    $ip =~ /^192\.168\.|^10\.|^169\.254\./) { die("Local Network"); }

    if (
    $ip !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
        die(
    "Invalid IP-Address: $ip");
    }
        
    my $gi Geo::IP->open("Geo/GeoLiteCity.dat"GEOIP_STANDARD);
    my $record $gi->record_by_addr($ip);
    my $geo_ip_info;
    if (!
    $record) { die("No location found for this IP-Address"); }

        if (
    $record->city) {

            
    # we know the city
            
    if ($record->region_name) {

                
    # and we know the region name
                
    if ($record->city ne $record->region_name) {

                    
    # the city and region name are different, all three are relevant.
                    
    $geo_ip_info $record->city '^7,^2 ' $record->region_name ' ^7-^2 ' $record->country_name;
                }
                else {
                    
    # the city and region name are the same.  Use city and country.
                    
    $geo_ip_info $record->city '^7,^2 ' $record->country_name;
                }
            }
            else {
                
    # Only two pieces we have are city and country.
                
    $geo_ip_info $record->city '^7,^2 ' $record->country_name;
            }
        }
        
    elsif ($record->region_name) {

            
    # don't know the city, but we know the region name and country.  close enough.
            
    $geo_ip_info $record->region_name '^7,^2 ' $record->country_name;
        }
        
    elsif ($record->country_name) {

            
    # We may not know much, but we know the country.
            
    $geo_ip_info $record->country_name;
        }
        
    elsif ($record->country_code3) {

            
    # How about a 3 letter country code?
            
    $geo_ip_info $record->country_code3;
        }
        
    elsif ($record->country_code) {

            
    # How about a 2 letter country code at least?
            
    $geo_ip_info $record->country_code;
        }
        else {
            
    # I give up.
            
    $geo_ip_info "Unknown location";
        }

    print 
    "$geo_ip_info\n"
    Which i cruly copypasted from Nanny, this would return city (in some case not), region name, and country.
    So in gsc i would run it like
    PHP Code:
    location system("perl locate.pl " self getIP()); 
    and then iprintln the result:
    Click image for larger version. 

Name:	shot0001.jpg 
Views:	131 
Size:	29.5 KB 
ID:	1008
    Ye that's right.
    Last edited by voron00; 30th December 2015 at 16:50.

  2. The Following 2 Users Say Thank You to voron00 For This Useful Post:

    kung foo man (30th December 2015),Mitch (30th December 2015)

  3. #2
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    Well shit, When running a more complicated code which output would be delayed, this would lag the server since it waits for that output, would need a thread or something...

  4. #3
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,011
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    inb4 async nodejs for libcod, or like izno did with async mysql

    nice job and nice too see some ppl come up with something
    timescale 0.01

  5. #4
    Sergeant maxdamage99's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    458
    Thanks
    79
    Thanked 122 Times in 101 Posts
    V KAKUYU DIRECTORIYU KIDAT' PERL SCRIPT??
    p.s: ili perl iz lyboy "hvataet"??!

  6. #5
    Corporal voron00's Avatar
    Join Date
    Nov 2014
    Posts
    248
    Thanks
    64
    Thanked 216 Times in 116 Posts
    Quote Originally Posted by maxdamage99 View Post
    V KAKUYU DIRECTORIYU KIDAT' PERL SCRIPT??
    p.s: ili perl iz lyboy "hvataet"??!
    You can run scripts from any directory by cd'ing && executing, but GeoIP database lookup takes too much time, it would lag the server as i said before.

    Anyway, i tried to mess around the pthreads but there seems to be no way to push the value from a thread function into the gsc. Well, i reverted the original system() but added mine as execute():
    https://github.com/voron00/libcod/co...659bd93d081db8
    Proably gonna leave it like that, seems fine by me.

  7. #6
    Sergeant maxdamage99's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    458
    Thanks
    79
    Thanked 122 Times in 101 Posts
    вери бед инглиш ворон...
    p.s:
    if you got error:
    Code:
    Can't locate Geo/IP.pm in @INC (you may need to install the Geo::IP module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at locate.pl line 5.
    BEGIN failed--compilation aborted at locate.pl line 5 (#1)
        (F) You said to do (or require, or use) a file that couldn't be found.
        Perl looks for the file in all the locations mentioned in @INC, unless
        the file name included the full path to the file.  Perhaps you need
        to set the PERL5LIB or PERL5OPT environment variable to say where the
        extra library is, or maybe the script needs to add the library name
        to @INC.  Or maybe you just misspelled the name of the file.  See
        "require" in perlfunc and lib.
    
    Uncaught exception from user code:
            Can't locate Geo/IP.pm in @INC (you may need to install the Geo::IP module) (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at locate.pl line 5.
            BEGIN failed--compilation aborted at locate.pl line 5.
    put in terminal:
    Code:
    perl -MCPAN -e "install Geo::IP::PurePerl" 
    perl -MCPAN -e "install Geo::IP"
    or look: http://kb.odin.com/en/120247
    http://dev.maxmind.com/geoip/legacy/install/city/
    Last edited by maxdamage99; 2nd January 2016 at 11:31.

  8. #7
    Sergeant maxdamage99's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    458
    Thanks
    79
    Thanked 122 Times in 101 Posts
    Last edited by maxdamage99; 2nd January 2016 at 12:41.

  9. #8
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    You forgot to free() the result you get from your function. Might cause out of memory errors if you use the function a lot.
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  10. The Following User Says Thank You to IzNoGoD For This Useful Post:

    voron00 (2nd January 2016)

  11. #9
    Sergeant maxdamage99's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    458
    Thanks
    79
    Thanked 122 Times in 101 Posts
    How izno??

  12. #10
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Quote Originally Posted by maxdamage99 View Post
    How izno??
    this is for voron
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •