views:

36

answers:

2

I have a form configured to change the IP address on a network appliance and it is all working well. The problem I am having is how to handle something like an IP address change on the client side. I whipped up some jQuery to essential sit and wait to see if the new IP is reachable, if it is the browser forwards and if it isn't the browser displays the new IP info and lets the user know that they may need to change their network config to get back to the box.

The problem with this is that occasionally this script hits the error conditions of the .ajax calls unexpectedly and then the browser is unable to load any page on that box anymore. If you use any other browser the web server is still reachable so its not a problem with the box itself. If you close the browser and reload the page it works. This leads me to believe that there is a loop somewhere, but I cant seem to find it. The following is what I am using on the client side.

$(function(){
    var newIp;
    var newSnm;
    var newGw; 
    var ns1;
    var ns2; 

    $("#updateip").click(function(){
        newIp = $("#ipaddress").val();
        newSnm = $("#subnetmask").val();
        newGw  = $("#gateway").val();
        ns1 = $("#ns1").val();
        ns2 = $("#ns2").val();

        $status.dialog('open');
        $.ajax({
            type: "POST",
            url: "setuphandler.php",
            data: { updateip : "false", ipaddress : newIp , subnetmask : newSnm, gateway : newGw, ns1 : ns1, ns2 : ns2},
            timeout: 10000, //If the post doesnt finish in under 10 seconds this will force an error event
            success: //This will fire if the IP was not changed by the post
                function(xhr, data){
                    $status.dialog('close');
                },
            error: //This will fire if the IP address has been changed by the post
                function(){
                    checkNewURL();
                }
            });
    });

    //Function to forward the browser if the new IP is reachable, if not alert the user and display the new settings to them 
    function checkNewURL(){
        $.ajax({
            type: "GET",
            timeout: 700,
            cache: false,
            url: 'http://' + newIp,
            error: //if the new ip is not reachable, display new connection info to the user 
                function(){
                    $status.dialog( "option", "title", "Error" );
                    $status.dialog( "option", "height", 300 );
                    $status.html(" \
                                <small> \
                                    <br />\
                                    <b>Unable to contact new IP address.</b> \
                                    <br />\
                                    You may need to change settings on your local machine in order to communicate with this address. \
                                    <br />\
                                    IP Address - " + newIp + "\
                                    Subnet Mask - " + newSnm + "\
                                    Gateway - " + newGw + "\
                                </small> \
                                ");
                },
            success: //If the new ip is reachable, auto-forward the user
                function(){
                    $status.html("Redirecting...");
                    redirectUrl = 'http://' + newIp + '/setup.php'
                    setTimeout("window.location=redirectUrl", 2000);
                }
        });
    }

    $status.dialog({
        modal: true,
        height: 225,
        width: 350,
        title: 'Applying Changes',
        autoOpen: false,
        draggable: false,
        resizable: false,
        closeOnEscape: false,
        open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
    });

});
+3  A: 

This isn't an answer, but you should change

                setTimeout("window.location=redirectUrl", 2000);

to

                setTimeout(function() { window.location=redirectUrl}, 2000);

Very bad practice sending a literal string to that function since it's slower and isn't as nice as a function literal.

meder
+1 for best practices advice :)
HurnsMobile
Have you tried `console.log` ing inside of the `error:` functions and maybe setting a longer delay for the `setTimeout` to debug?
meder
A: 

This turned out to be a very unrelated issue/answer. In testing I was entering in random ip's for nameservers. This would not have been an issue except for the fact that there was an initialization script on the box that was changing the hostname to but NOT adding it to /etc/hosts therefore the machine was unable to do a proper lookup on its own localhost address. As it turns out, this breaks sudo! Anyway, thanks for looking everyone!

HurnsMobile