private string user = "root", newpassword = "test123"; private int port = 22; public sshconnection(string host, string password) { try { jsch jsch = new jsch(); session session = jsch.getsession(user, host, port); session.setpassword(password); properties config = new properties(); config.put("stricthostkeychecking", "no"); session.setconfig(config); session.connect(); channelexec channel = (channelexec)session.openchannel("exec"); outputstream out = channel.getoutputstream(); ((channelexec)channel).seterrstream(system.err); channel.connect(); out.write(password.getbytes()); out.flush(); out.write(newpassword.getbytes()); out.flush(); out.write(newpassword.getbytes()); out.flush(); channel.disconnect(); session.disconnect(); } catch(exception e) { e.printstacktrace(); } }
i asked change password on first time log in on server. trying jsch, i'm not sure how can accomplish this. far understand can't use commands i'm forced change password before doing anything, can't use
(echo old_password; echo new_password; echo new_password) | passwd username
i solved issue calling channel.setpty(true);
private string user = "root", newpassword = "test123"; private int port = 22; public sshconnection(string host, string password) { try { jsch jsch = new jsch(); session session = jsch.getsession(user, host, port); session.setpassword(password); properties config = new properties(); config.put("stricthostkeychecking", "no"); session.setconfig(config); session.connect(); channelexec channel = (channelexec)session.openchannel("exec"); outputstream out = channel.getoutputstream(); ((channelexec)channel).seterrstream(system.err); channel.setpty(true); channel.connect(); out.write((password + "\n").getbytes()); out.flush(); thread.sleep(1000); out.write((newpassword + "\n").getbytes()); out.flush(); thread.sleep(1000); out.write((newpassword + "\n").getbytes()); out.flush(); thread.sleep(1000); channel.disconnect(); session.disconnect(); } catch(exception e) { e.printstacktrace(); } }
i added sleeps before each input consistency, want wait output before entering each password, uses do.
Comments
Post a Comment