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.
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.
Here is the java code
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:
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();
}
}
}