• Nick D (unregistered)

    Dear God! There is so much wrong with that, I don't know where to start!

    Maybe he actually gave his development pc an IP address of '127.0.0.1' and the production machine was '127.0.0.2'?

    Fairly standard convention, isnt it?






    MWUAHAHAHAHAHHAAA

  • josh (unregistered)

    Well, you certainly won't be getting '127.0.0.1' on a production machine. Slightly better than a stopped clock...

  • Jeff Darcy (unregistered)

    It doesn't look like a particularly bad way to scan through a machine's addresses, but what kind of idiot sets a real interface's address to 127.0.0.1?

  • Ben Hutchings (unregistered)

    If I understand the code correctly, the LocalAddress getter returns the last listed address that isn't 127.0.0.1. So I don't think there's anyhing wrong with the logic; it's just rather inefficient. It looks like it was written by someone used to writing Unix shell scripts, where grepping through the output of ifconfig would be the right solution; in fact I do exactly that in my home firewall configuration script.

  • Derick Bailey (unregistered)

    OH LOOK! a moron developing in C#! 8)

    and on the Moron scale of 1 to 10 (10 being the biggest moron you can find), this guy gets a 10.5 for three reasons: 1) the stupid idea that only a development machine can have 2 ip addresses. 2) the stupid idea that only a development machine can have the IP of 127.0.0.1, and 3)being too stupid to know that ALL machines have a 127.0.0.1 IP address because it's the LOOPBACK address that ALWAYS points to the same machine...

  • Manni (unregistered)

    Derick: The flaw in your graduating scale of moron-ness is that if this guy truly was the biggest moron you could find, that would put him at a 10. It is impossible to score higher than that, because you defined it such that the biggest moron receives the 10, and all other morons presumably have their scores adjusted based on his moron rating.

    It's like turning the knobs to 11 when you need that extra push to get you over the cliff. Why not just make 10 the loudest, and make that a little louder?

  • Ryan (unregistered)

    And you should always use a firewall to block 127.0.0.1 anyway. The loopback interface is such a security hole, sitting wide-open on just about every computer.



    :P

  • Greg Hurlman (unregistered)

    No, but see... this moron goes to 11.

  • skicow (unregistered)

    Originally posted by Manni :

    "It's like turning the knobs to 11 when you need that extra push to get you over the cliff. Why not just make 10 the loudest, and make that a little louder?"

    These go to 11.

    Nice one Manni, just had to finish that for you....love that movie!

  • Manni (unregistered)

    @Greg- Point well taken, I have to concur

    @skicow- My bad, thanks for filling that in ;)

  • Ray S (unregistered)

    Actually, and I hate to nitpick, the whole 127.x.x.x range is loopback, not just 127.0.0.1.
    You shouldn't be able to set the IP address on any sensible OS to 127.0.0.2, let alone .1

    Anyway...

  • Bu (unregistered)

    Ummmm.... (ok keep in mind I haven't done C# yet, but am very fluent in Java)

    In Java there's an API in java.net.InetAddress called "getAllByName()" that returns a list of all IP's for the specified machine name. Surely C# has something similar? I can't believe you have to run an external process to get a friggin IP address (ok I believe HE had to do it, but not your ordinary PROGRAMMER).

  • Bu (unregistered)

    Yeah, the more I look at this, the more it looks like this guy had a shell script that did this, then needed to do it in C# but didn't take the time to READ THE FREAKIN MANUAL!

  • [email protected] (unregistered)

    There is no patch for stupidity

  • KoFFiE (unregistered)

    I think everybody should:
    1) Read the code, and try to understand it
    2) Read Ben Hutchings comment, he's absolutely right :)

    The code will indeed return the last non-127.0.0.1 ip-address the ipconfig command will show, or a null when there is no IP :)

    This surely looks like a shell-script to me :) Also have smth like that in my firewall script, but that's actually a bit simpler/shorter, as in:

    EXTIP="/sbin/ifconfig $EXTIF | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'"

    where $EXTIF is eth0, the external interface :)

    Sure, it still is a stupid way to check wether it's a production machine or not, simply by checking the ip-address, but it could work :)

  • Matthew W. Jackson (unregistered)

    IsDevelopmentMachine()

    He calls writing this code "development?"

    I would refactor this method and rename it:

    IsMachineWhereIHastilyThrowTogetherApplicationsWhileNeverLookingAtTheDocumentationAndCallItDevelopment()

  • asdfs (unregistered)

    There is nothing wrong with the code itself, anyway. It does what is expected - returns the last IP address that is not the loopback address. The digit "2" points not to two IP addresses but to two groups in a regex match, where the first group contains all the input substring and the second group is the actual match.
    So, if the guy used Dns.GetHostByName(Dns.GetHostName()).AddressList, the function would be what it is expected to be.
    Now he gets a two-star "WTF" for executing shell scripts (which won't work on MONO framework, for example.)

  • josh (unregistered)

    The biggest problem is that the unseen "IsDevelopmentMachine" is presumably checking for a condition that this code will never report. Or am I just being crazy for reading the opening blurb that way?

  • Peter (unregistered)

    >The flaw ... It is impossible to score higher than that, because you defined it such that the biggest moron receives the 10, and all other morons presumably have their scores adjusted based on his moron rating.

    Fine example of an "off by one" error.

  • Jason (unregistered)

    BU:
    There is a method that does what your looking for in C# (it is referenced at the very top, by Alex I'm assuming).

    Problem with doing that is that what you are doing is asking the DNS server what your IP is. Is it just me, or does asking another server what your IP is, as stored in its cache sound like a bad idea?

    It is highly recommended by Microsoft (and everyone else i've ever talked to) to not do that. So the way this guy is doing it (although he could have found about 10 better ways to do it), is technically more accurate than getting his IP from the DNS server.

    The proper way to do it would be with PInvoke, or System.Management.

  • Ben Hutchings (unregistered)

    KoFFiE: Why use three commands to select the address when sed can do all you need? This is what I did:

    INT_ADDR=$($IFCONFIG $INT_IF | sed -e 's/ inet addr:([0-9.]).*/\1/; t; d')

  • Ben Hutchings (unregistered)

    Jason: Dns.GetHostByName is presumably a wrapper for gethostbyname, which isn't actually restricted to using DNS. It is required to Do The Right Thing with whatever gethostname returns.

  • PhantomCoder (unregistered)

    ugh...always store environment specific references (like URLS, or physical file paths, or dB connection strings) as encrypted strings in web.config? DPAPI works just fine.


  • Daniel Moth (unregistered)
  • fogelman (unregistered)

    whould'nt it be easier to have a special file on a dev box and absent on prod? then the check will simply be file.exists() kinda thing. Spawning an shell commad to capture printout on STDIO? yuck! I mean, that's if the "developer" counldn't even figure out the app.config availability and function. What's next? maybe that IP list he discovers should be sent over soap to a mainframe via SNA server to check against COBOL inventory system and tell you back that it's a dev machin?

    Hmm..
    I think I can make some money with this product.

  • Dave Alvarado (unregistered)

    I have a strong suspicion that the original poster didn't really know what was going on. LocalAddress is just a roundabout way of obtaining a machine's local IP address, but tossing out the result if it's the loopback address. You could check that against a known IP address (like the IP of your production server) to know if you're on the production machine or not. I'd even give points for using something that's likely to survive the production server being rebuilt, since the Ops guys are just about 100% likely to give the same server the same IP if it's rebuilt, although they might miss the step in building it where you copy the "magic" file or set the "I'm Production" environment variable. The only WTF here is that you've got code that was probably written by a sysadmin or someone much more familiar with the script world than with C# and .NET, leading to an inelegant but effective solution.

  • Ben (unregistered)

    um... wouldn't it be much simpler to have a key in the appSettings of the web.config file? since you're expected to maintain live settings on one and development settings on the other (i.e. not overwrite the web.config file), such as DB settings... if you were actually going to set a case in the code, and not in the web.config (for some reason, who knows?), why not check to see if System.Configuration.ConfigurationSettings.AppSettings("Setting") == "live" or "dev" or whatever?

    -- IFF that's the desired effect, considering the name of the function?

  • Brian (unregistered)

    In non-english windows installations the output of ipconfig is in the specific language and therefore "IP Address" won't neccesarily find a match...

  • tinoh (unregistered)

    I have to say that (regardless of all other possible mistakes in the actual implementation of this) to me the actual WTF is letting program code dynamically decide whether it's running on a "development machine" or not.

    The possible unwanted effects of such a thing are.. well. That one goes to 12.

  • Bart Lateur (unregistered)

    It looks just like a way to get your own IP address (but not 127.0.0.1) to me, under the assumption there's only one other IP address. All the rest seems to be made up by you guys.

  • Michael (unregistered)

    Re: 127.0.0.2

    Actually, all IP address in 127...* point to loopback; you couldn't assign another computer the address of 127.0.0.2 because it will always point to your computer.

Leave a comment on “IsDevelopmentMachine()”

Log In or post as a guest

Replying to comment #:

« Return to Article