You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by "HAVENS,PETER (HP-Cupertino,ex3)" <pe...@hp.com> on 2002/07/28 00:35:23 UTC

tomcat 4.0 remote shutdown question ...

I have found that I cannot connect to the tomcat shutdown port remotely.
Using code highly leveraged from the tomcat source, I wrote a simple java
app to issue the shutdown command to a given server and port #.  It only
seems to work if I use "localhost" as the server name.  If I try to use the
hostname or the fully qualified host name (even of the local system) it will
fail.  I have tried using this to stop Tomcat on linux and Windows XP with
the same results.  I have included the output I get when I run it and the
source code.  Any help understanding this would be greatly appreciated.
 
-------------------------Sample output--------------------------------
Attempting to shut down the tomcat server on ovwpc574/15.27.245.212 using
port 8085
 
tomcat_stop: java.net.ConnectException: Connection refused
java.net.ConnectException: Connection refused
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:295)
            at
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:161)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
            at java.net.Socket.connect(Socket.java:425)
            at java.net.Socket.connect(Socket.java:375)
            at java.net.Socket.<init>(Socket.java:290)
            at java.net.Socket.<init>(Socket.java:146)
            at tomcat_stop.main(tomcat_stop.java:40)
-------------------------End of Sample
output--------------------------------
 
-------------------------Source Code--------------------------------
import java.io.*;
import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
 
 
class tomcat_stop
{
 
            public static void main( String [] args ) 
            {
                        int port_number;
                        InetAddress server_address=null;
                        Socket socket;
 
                        if (args.length != 2)
                                    usage();
                        
                        port_number = Integer.parseInt(args[1]);
 
                        try
                        {
                                    server_address =
InetAddress.getByName(args[0]);
                        }
                        catch (IOException e)
                        {
                                    System.out.println("");
                                    System.out.println("Unable to get
InetAddress using getByName on " + 
                                                            args[0]);
                                    System.out.println(":tomcat_stop " + e);
                                    e.printStackTrace(System.out);
                                    System.exit(1);  
                        }
 
                        // Stop the existing server
                        try {
                                    System.out.println("\n\nAttempting to
shut down the tomcat server on " + 
                                                server_address + " using
port " + port_number + "\n\n");    
 
                                    socket = new Socket(server_address,
port_number);
                                    OutputStream stream =
socket.getOutputStream();
                                    String shutdown = "SHUTDOWN";
                                    for (int i = 0; i < shutdown.length();
i++)
 
stream.write(shutdown.charAt(i));
                                    stream.flush();
                                    stream.close();
                                    socket.close();
                        }
                        catch (IOException e)
                        {
                                    System.out.println("tomcat_stop: " + e);
                                    e.printStackTrace(System.out);
                                    System.exit(1);
                        }
            }
 
            public static void usage()
            {
                        System.out.println("\nUsage:\n\ttomcat_stop <system
name> <port>\n\n");
                        System.exit(1);
            }
}
 
 

Re: tomcat 4.0 remote shutdown question ...

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 27 Jul 2002, HAVENS,PETER (HP-Cupertino,ex3) wrote:

> Date: Sat, 27 Jul 2002 18:35:23 -0400
> From: "HAVENS,PETER (HP-Cupertino,ex3)" <pe...@hp.com>
> Reply-To: Tomcat Developers List <to...@jakarta.apache.org>
> To: "'tomcat-dev@jakarta.apache.org'" <to...@jakarta.apache.org>
> Subject: tomcat 4.0 remote shutdown question ...
>
> I have found that I cannot connect to the tomcat shutdown port remotely.
> Using code highly leveraged from the tomcat source, I wrote a simple java
> app to issue the shutdown command to a given server and port #.  It only
> seems to work if I use "localhost" as the server name.  If I try to use the
> hostname or the fully qualified host name (even of the local system) it will
> fail.  I have tried using this to stop Tomcat on linux and Windows XP with
> the same results.  I have included the output I get when I run it and the
> source code.  Any help understanding this would be greatly appreciated.
>

Tomcat deliberately accepts connections to the shutdown port only from
localhost as a security precaution.  After all, you really don't want
anyone on the Internet to be able to shut down your Tomcat instance.

To change this behavior, you'd need to change the Tomcat source code for
the logic that opens the shutdown port's socket.  It's not configurable.

Craig


> -------------------------Sample output--------------------------------
> Attempting to shut down the tomcat server on ovwpc574/15.27.245.212 using
> port 8085
>
> tomcat_stop: java.net.ConnectException: Connection refused
> java.net.ConnectException: Connection refused
>             at java.net.PlainSocketImpl.socketConnect(Native Method)
>             at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:295)
>             at
> java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:161)
>             at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
>             at java.net.Socket.connect(Socket.java:425)
>             at java.net.Socket.connect(Socket.java:375)
>             at java.net.Socket.<init>(Socket.java:290)
>             at java.net.Socket.<init>(Socket.java:146)
>             at tomcat_stop.main(tomcat_stop.java:40)
> -------------------------End of Sample
> output--------------------------------
>
> -------------------------Source Code--------------------------------
> import java.io.*;
> import java.io.IOException;
> import java.net.Socket;
> import java.net.InetAddress;
>
>
> class tomcat_stop
> {
>
>             public static void main( String [] args )
>             {
>                         int port_number;
>                         InetAddress server_address=null;
>                         Socket socket;
>
>                         if (args.length != 2)
>                                     usage();
>
>                         port_number = Integer.parseInt(args[1]);
>
>                         try
>                         {
>                                     server_address =
> InetAddress.getByName(args[0]);
>                         }
>                         catch (IOException e)
>                         {
>                                     System.out.println("");
>                                     System.out.println("Unable to get
> InetAddress using getByName on " +
>                                                             args[0]);
>                                     System.out.println(":tomcat_stop " + e);
>                                     e.printStackTrace(System.out);
>                                     System.exit(1);
>                         }
>
>                         // Stop the existing server
>                         try {
>                                     System.out.println("\n\nAttempting to
> shut down the tomcat server on " +
>                                                 server_address + " using
> port " + port_number + "\n\n");
>
>                                     socket = new Socket(server_address,
> port_number);
>                                     OutputStream stream =
> socket.getOutputStream();
>                                     String shutdown = "SHUTDOWN";
>                                     for (int i = 0; i < shutdown.length();
> i++)
>
> stream.write(shutdown.charAt(i));
>                                     stream.flush();
>                                     stream.close();
>                                     socket.close();
>                         }
>                         catch (IOException e)
>                         {
>                                     System.out.println("tomcat_stop: " + e);
>                                     e.printStackTrace(System.out);
>                                     System.exit(1);
>                         }
>             }
>
>             public static void usage()
>             {
>                         System.out.println("\nUsage:\n\ttomcat_stop <system
> name> <port>\n\n");
>                         System.exit(1);
>             }
> }
>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>