The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Perl advice needed (http://hintsforums.macworld.com/showthread.php?t=25344)

ibroughton 07-03-2004 07:12 AM

Yay!
 
After all that hard work, it finally does what it is supposed to do. The final code was altered to:-
---

#!/usr/bin/perl

use strict;

sleep int(rand 60);

# Change this to match your server name:
my $webserver = "http://www.iainbroughton.com";

# Don't change anything below this unless you know what
# you're doing. :)
my $command = "ifconfig -a inet 2>/dev/null | ";
$command .= "sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' ";
$command .= "-e '/tunnel/d' -e '/inet/p' | wc -l";

my $network = `$command`;

$command = "system_profiler -detailLevel -2 2>/dev/null | ";
$command .= "grep -i \"serial number\"";

my @serial_number = split ' ', `$command`;
if ($network == 1) {
$command = "curl -A \"Thief Killer (Mac OS X)\" ";
$command .= "$webserver/$serial_number[2].html";
system $command;
}

---

and now produces a very pretty entry on my server log file (Obviously I've altered the IP and serial for this posting!)
--
212.159.xxx.xxx - - [03/Jul/2004:11:41:13 +0100] "GET /W902103XPJH.html HTTP/1.1" 200 0 "-" "Thief Killer (Mac OS X)"
--

Thankyou so much! :)

ibroughton 07-03-2004 07:30 AM

The only thing I don't quite understand is the

$command = "system_profiler -detailLevel -2 2>/dev/null | ";
$command .= "grep -i \"serial number\""

as it doesn't show up anywhere, and the W902103XPJH.html was created manually by me. Should the serial number (as got automatically by the script) show up anyhere? At least it works though! :)

hayne 07-03-2004 07:39 AM

If you want to make this less noticeable, you should avoid running the system_profiler when not connected to a network.
To do this, just add the following line right after the line that sets the $network variable.

exit unless $network;

nkuvu 07-03-2004 07:44 AM

Doesn't show up anywhere?

The script won't create the HTML file, if that's what you're thinking. And really, the HTML file doesn't need to exist in order for this to work. If it doesn't, the log will show a 404 response (instead of the 200 that you quoted) but you'll still have all of the info you need.

If you're thinking that the curl command should save the HTML file somewhere, you'd be right. If the command had the proper options. What's happening as it stands is that the curl has a -A option, which sets the user agent. Then it downloads the file and sends it to standard output. If you run this script by itself you'll see the downloaded file displayed on the screen. If you wanted to save the file you'd need to add a -o or -O option to the curl command (read man curl for explanations of the options).

ibroughton 07-03-2004 08:39 AM

No, I didn't think it would create the HTML file for me, and no, the system serial number doesn't appear at all. I don't want the file saved locally. Sorry for sounding thick, but I don't really understand what this is doing other than making a stamp (either a 200 or a 404) in my servers log, displaying the correct IP address. Is it actually supposed to show anything up in the HTML file, or just show in the server log? :confused:

nkuvu 07-03-2004 08:59 AM

The point is just to put a stamp in the server log. The HTML file is irrelevant.

ibroughton 07-03-2004 09:01 AM

In which case we have succeeded! :)

hayne 07-03-2004 09:23 AM

Quote:

Originally Posted by ibroughton
In which case we have succeeded!

At the risk of saying the obvious, I'd like to point out that you haven't really tested this until you have taken your Mac to various other network locations (other ISPs, dialup, cable, DSL, with & without intervening router) and tried it there.

ibroughton 07-03-2004 10:41 AM

Well yes, but I don't have the luxury of a dial up account. I'll just have to take this as a suitable measure untill I can verify it further. I'm quite content with the way that this is working at the moment, I presume the way the script is written, it doesn't matter how the machine is connected to the internet, as long as there is a connection present?

hayne 07-04-2004 01:49 PM

Quote:

Originally Posted by ibroughton
I presume the way the script is written, it doesn't matter how the machine is connected to the internet

I was merely pointing out that I wouldn't be confident that this script would fulfill its purpose until I had tested it on someone else's Internet connection (and preferably several other connections of varying types). It may seem to work in theory, but that's the nature of software bugs - you don't realize the problem until after it fails and then it often seems obvious what the problem is. Right now you only know that it works on your system. You don't know if there is some dependency on the current configuration.


All times are GMT -5. The time now is 05:45 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.