我是AWS的新手,任何人都可以帮助我如何使用java代码在虚拟机上创建用户。我已经创建了 windows ec2 实例,并且能够使用 sshClient 进行连接,但无法创建用户。我不知道如何做到这一点。
[我是堆栈溢出的新手,如果格式错误,请原谅我]
回答我自己的问题...它可能会帮助某人。您可以使用执行命令在远程窗口上创建用户...例:
public boolean executeCommands(String commands, SSHRequestParam param)
throws Exception,IOException {
boolean response = false;
try {
Connection conn = getConnection(param);
Session sess = conn.openSession();
sess.execCommand(commands);
sess.waitForCondition(ChannelCondition.EXIT_SIGNAL, 0);
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(
new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null)
break;
response=true;
System.out.println(line);
}
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader br1 = new BufferedReader(new InputStreamReader(
stderr));
while (true) {
String line = br1.readLine();
if (line == null)
break;
response=false;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
System.out.println("closing session");
sess.close();
closeConnection(conn);
} catch (IOException e) {
System.out.println("Exception..while executing command.: " + commands);
e.printStackTrace();
}
System.out.println("Command executed : "+response);
return response;
}
public void createUser(String userName, String password,SSHRequestParam param)
throws Exception,IOException {
String command = "net user " +userName+" "+password+" /add" ;
if(executeCommands(command,param))
System.out.println("User created successfully");
else
System.out.println("User didn't create....");
}
在上面的例子中,param 变量存储连接到服务器的参数,即主机名、用户名、密码。