43. Monitoring and management using a remote shell

Spring Boot supports an integrated Java shell called ‘CRaSH’. You can use CRaSH to ssh or telnet into your running application. To enable remote shell support add a dependency to spring-boot-starter-remote-shell:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-remote-shell</artifactId>
 </dependency>
[Tip]Tip

If you want to also enable telnet access your will additionally need a dependency on org.crsh:crsh.shell.telnet.

43.1 Connecting to the remote shell

By default the remote shell will listen for connections on port 2000. The default user is user and the default password will be randomly generated and displayed in the log output. If your application is using Spring Security, the shell will use the same configuration by default. If not, a simple authentication will be applied and you should see a message like this:

Using default password for shell access: ec03e16c-4cf4-49ee-b745-7c8255c1dd7e

Linux and OSX users can use ssh to connect to the remote shell, Windows users can download and install PuTTY.

$ ssh -p 2000 user@localhost

user@localhost's password:
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.2.0.RELEASE) on myhost

Type help for a list of commands. Spring boot provides metrics, beans, autoconfig and endpoint commands.

43.1.1 Remote shell credentials

You can use the shell.auth.simple.user.name and shell.auth.simple.user.password properties to configure custom connection credentials. It is also possible to use a ‘Spring Security’ AuthenticationManager to handle login duties. See the CrshAutoConfiguration and ShellProperties Javadoc for full details.

43.2 Extending the remote shell

The remote shell can be extended in a number of interesting ways.

43.2.1 Remote shell commands

You can write additional shell commands using Groovy or Java (see the CRaSH documentation for details). By default Spring Boot will search for commands in the following locations:

  • classpath*:/commands/**
  • classpath*:/crash/commands/**
[Tip]Tip

You can change the search path by settings a shell.commandPathPatterns property.

Here is a simple ‘hello world’ command that could be loaded from src/main/resources/commands/hello.groovy

package commands

import org.crsh.cli.Usage
import org.crsh.cli.Command

class hello {

    @Usage("Say Hello")
    @Command
    def main(InvocationContext context) {
        return "Hello"
    }

}

Spring Boot adds some additional attributes to InvocationContext that you can access from your command:

Attribute NameDescription

spring.boot.version

The version of Spring Boot

spring.version

The version of the core Spring Framework

spring.beanfactory

Access to the Spring BeanFactory

spring.environment

Access to the Spring Environment

43.2.2 Remote shell plugins

In addition to new commands, it is also possible to extend other CRaSH shell features. All Spring Beans that extend org.crsh.plugin.CRaSHPlugin will be automatically registered with the shell.

For more information please refer to the CRaSH reference documentation.