The actual background

In my daily work, I have to log on to A Linux server A and then log on to another Linux server B.

This is usually done for security reasons, to isolate servers that can be accessed offline from servers that provide services, and to protect against attacks. However, this design will also bring some trouble for normal use, so it is expected that some design can help us more convenient to use.

SSH Command line login

Assuming that machine A can be accessed by the command terminal, which means that machine A can be accessed by the Intranet of the company, and our target server is B, we can provide A Web service on server A and directly access the content of server B through the interface. Specific ideas are as follows:

Core ideas:

You can use SSH to log in to server B from server A, so you can run commands on server B directly.

Run the following command on server A:

SSH server name "specific command"Copy the code

Note: The specific command is enclosed in double quotation marks. If the command contains special characters such as single and double quotation marks or /, escape them

The Java code is as follows:

            String [] cmdArray = new String[] {"/bin/sh", "-c", finalCmd};
            process = Runtime.getRuntime().exec(cmdArray);
Copy the code

A normal return can be obtained via process.getinputStream ()

The return of the exception message can be obtained via process.geterrorStream ()

advantages

  1. You can use Java code in this way to implement the commands normally executed after SSH login on server A and server B. It is very convenient to verify

  2. Simple to develop, input and return can be controlled and handled to a certain degree by your service

  3. Environment variables on the target server can be automatically loaded.

disadvantages

  1. Some commands may need to interact with the terminal (not for input parameters, but for output to a specific location such as /dev/tty), which may not return properly and requires special handling

  2. Each request represents a command and an SSH connection is created. After the request is completed, the SSH connection cannot be maintained. Therefore, it is not applicable to the situation where multiple commands are executed at a time or a high number of concurrent requests are made.

Build links using the JSCH tool

If it is expected to establish a link and execute multiple commands, it is necessary to use some third-party dependent packages to achieve this. I choose com.jcraft. JSCH package here. There are several packages with similar functions, which can be selected according to needs

The core idea remains the same, except that the JSCH package provides functionality for establishing connections and executing commands

Maven rely on

<dependency> <groupId>com.jcraft</groupId> <artifactId> JSCH </artifactId> <version>0.1.53</version> </dependency>Copy the code

The Java code implementation is as follows

public void jsch() throws JSchException, IOException { List<String> stdout = new ArrayList<>(); JSch jsch = new JSch(); Session session = jsch.getSession("user", "host", 222); session.setPassword("password"); session.connect(5000); Channel Channel = session.openChannel("exec"); ChannelExec channelExec = (ChannelExec)channel; channelExec.setCommand("ls"); channelExec.setInputStream(null); BufferedReader input = new BufferedReader(new InputStreamReader(channelExec.getInputStream())); channelExec.connect(); // Receive the result of the command executed by the remote server. while ((line = input.readLine()) ! = null) { stdout.add(line); } input.close(); // Close channel channelexec.disconnect (); // Close session session.disconnect(); Logger.info (" + stout.toString ()); }Copy the code

advantages

  1. SSH connections can be maintained using tools, which is suitable for frequent command execution and reduces unnecessary SSH connection establishment and closure consumption

  2. Various API interfaces have complete functions and are not complex

  3. Support for pulling server files

disadvantages

  1. There are some holes in certification, you need to fill in according to the situation you encounter

  2. The environment variables of the target server cannot be obtained during the command execution. Therefore, you need to use the full path of the command. For example, XXXX/XXX/XXX /bin/ java-jar cannot be directly used. You can run the type command to obtain the command path