Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to switch root and use exec function #292

Open
lucky2046 opened this issue Jul 15, 2023 · 2 comments
Open

how to switch root and use exec function #292

lucky2046 opened this issue Jul 15, 2023 · 2 comments

Comments

@lucky2046
Copy link

The remote machine does not support root user SSH connection. Using test user SSH to connect to the remote machine to obtain session A, I can smoothly switch to root user through the session shell. Then, I execute whoami through the session exec command, prompting me that session A has been reused. How can I solve this problem?

@lucky2046
Copy link
Author

package com.mixislink.task.ssh;

import lombok.extern.slf4j.Slf4j;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.userauth.UserAuthException;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Slf4j
public class SSHTest {

    public static void main(String[] args) {
        String hostname = "192.168.100.185";
        int port = 22;
        String testUsername = "test";
        String testPassword = "123456";
        String rootUsername = "root";
        String rootPassword = "123456";

        try {
            SSHClient sshClient = new SSHClient();
            sshClient.addHostKeyVerifier(new PromiscuousVerifier()); // Use with caution, for testing purposes only

            // Connect with test user
            sshClient.connect(hostname, port);
            sshClient.authPassword(testUsername, testPassword);

            // Start an interactive session
            try (net.schmizz.sshj.connection.channel.direct.Session session = sshClient.startSession()) {
                // Start a shell
                session.allocateDefaultPTY();
                session.startShell();

                session.setAutoExpand(true);

                // Simulate the command to switch to root user
                session.getOutputStream().write(("su - root\n").getBytes());
                session.getOutputStream().flush();

                // Wait for a moment to allow the command to be executed and password to be prompted
                Thread.sleep(1000);

                // Provide the root password
                session.getOutputStream().write((rootPassword + "\n").getBytes());
                session.getOutputStream().flush();

                // Wait for a moment to allow the password to be validated
                Thread.sleep(1000);

                // Now, you are logged in as root. You can execute commands or perform any operations as root here.
                // For example, let's print the current user (should be root now)
                String command = "whoami";
                session.getOutputStream().write((command + "\n").getBytes());
                session.getOutputStream().flush();
                Thread.sleep(1000);

                // Read the command output
                byte[] buffer = new byte[1024];
                int read = session.getInputStream().read(buffer);
                String result = new String(buffer, 0, read);
                System.out.println("Current user: " + result.trim());

                Session.Command cmd = session.exec("whoami");
                System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
                cmd.join(5, TimeUnit.SECONDS);
                System.out.println("\n** exit status: " + cmd.getExitStatus());
                // Close the session when you're done.
                session.close();
            }

            // Don't forget to close the connection when you're done.
            sshClient.disconnect();

        } catch (UserAuthException e) {
            System.err.println("Authentication as test user failed. Make sure test user credentials are correct.");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@lucky2046
Copy link
Author

lucky2046 commented Jul 15, 2023

i got error:
This session channel is all used up

if i use sshClient start new session,i got test,not root.

How can I solve this problem?

Session.Command cmd = sshClient.startSession().exec("whoami");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
 cmd.join(5, TimeUnit.SECONDS);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant