How to log in as root user with password in a single command

Hi,

I need to automate a software installation on one of the FreeBSD servers, and I was able to connect to the server using the Apache SSHD java library. However, after logging in as a normal user, I need to switch to the root user, who is asking for a password similar to the one below.
Code:
~ $ su root
Password:
However, even if I submit the password to the Apache script, it does not work.
Could you please tell me if it is possible to log in as a root user with a password in a single command or without being prompted for it?

When the command in the following line is executed, all usual successive commands operate correctly except for inputting the root password.
Code:
     for (Object o : command) {
                            pipedIn.write(o.toString().getBytes());
                    }


Here is the java code

Code:
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ClientChannel;
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.channel.Channel;
import org.apache.sshd.common.session.Session;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;

public class SSHD {

    public static void main(String[] args) throws IOException {
        Session session = null;
        String host="host";
        String user="user";
        String password="pass";
        ArrayList<String> strings = new ArrayList<>();
        strings.add("pwd \n");
        strings.add("ls -lrt\n");
        strings.add("su root\n");
        strings.add("password\n");

        listFolderStructure(user, password, host, 22, 10, strings);
    }

    public static void listFolderStructure(String username, String password,
                                           String host, int port, long defaultTimeoutSeconds, ArrayList<String> command) throws IOException {
        SshClient client = SshClient.setUpDefaultClient();
        client.start();

        try (ClientSession session = client.connect(username, host, port)
                .verify(defaultTimeoutSeconds, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(password);
            session.auth().verify(defaultTimeoutSeconds, TimeUnit.SECONDS);

            try (ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
                 ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) {
                channel.setOut(responseStream);

                try {
                    channel.open().verify(defaultTimeoutSeconds, TimeUnit.SECONDS);

                    try (OutputStream pipedIn = channel.getInvertedIn()) {
                        for (Object o : command) {
                            pipedIn.write(o.toString().getBytes());
                    }

                    }
                    //pipedIn.flush();
                    channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED),
                            TimeUnit.SECONDS.toMillis(defaultTimeoutSeconds));
                    String responseString = responseStream.toString();
                    System.out.println(responseString);
                }
                finally {
                    channel.close(false);
                }


            }
        } finally {
            client.stop();
        }
    }
}
 
Configure sudo (or doas) to allow your user passwordless access. Only members of the wheel group are able to su(1) and it will always require root's password.
 
Configure sudo (or doas) to allow your user passwordless access. Only members of the wheel group are able to su(1) and it will always require root's password.
Thanks for the quick response. I appreciate it so if it has been configured with sudo, assume root user switch without password.
 
Thanks for the quick response. I appreciate it so if it has been configured with sudo, assume root user switch without password.
If set in the sudoers file to use NOPASSWD.

Otherwise you could just wrap it in a shell script to check the user.

Code:
#!/bin/sh

if [ "$(whoami)" != "root" ]; then
  sudo <someprog> "$@"
fi

Though you probably want to stick with standard OpenSSH (rather than the Java SSHD) and set up paasswordless logins instead?
 
sudo will work with a password read from a file/pipe
I know all this because I locked myself out on 12.3 to 12.4 upgrade :\ (did not restart sshd after last freebsd-update install )
and I researched a way to log back in
 
If you have no experience with sudo, and are just starting to use it, may I suggest that you try doas instead? It's easier to configure.
 
Back
Top