I'm using SSHJ and ExpectIt to send multiple commands to a virtual machine hosted on Amazon's EC2. I get no errors, but ExpectIt executes only the first command and forgets the rest. Can you find out what am I doing wrong here?
{
SSHClient ssh=new SSHClient();
Session session;
Shell shell;
session=ssh.startSession();
session.allocateDefaultPTY();
shell=session.startShell();
Expect expect = new ExpectBuilder()
.withOutput(shell.getOutputStream())
.withInputs(shell.getInputStream(), shell.getErrorStream())
.build();
expect.sendLine("sudo useradd "+uname+" -g sftponly -m -d /home/"+uname); //Only this command is getting executed
expect.sendLine("sudo passwd "+uname);
expect.sendLine(pwd);
statusbar.setText("Assigning access key to user...");
expect.sendLine("sudo mkdir /home/"+uname+"/.ssh");
expect.sendLine("sudo touch /home/"+uname+"/.ssh/authorized_keys");
expect.sendLine("sudo echo "+pemfile+">/home/"+uname+"/.ssh/authorized_keys");
statusbar.setText("Providing permissions to user...");
expect.sendLine("sudo chown root /home/"+uname);
expect.sendLine("sudo chmod go-w /home/"+uname);
expect.sendLine("sudo mkdir /home/"+uname+"/"+uname);
expect.sendLine("sudo chmod ug+rwX /home/"+uname);
expect.sendLine("sudo chmod 700 /home/"+uname+"/.ssh");
expect.sendLine("sudo chmod 600 /home/"+uname+"/.ssh/authorized_keys");
expect.sendLine("sudo chmod 755 /home/"+uname);
statusicon.setForeground(Color.green);
statusbar.setText("User created!");
expect.close();
}
NOTE: Some sensitive (but irrelevant to this question) code has been omitted from this code.