importjava.io.BufferedReader;2importjava.io.IOException;3importjava.io.InputStreamReader;4importjava.io.OutputStream;56importcom.sshtools.j2ssh.SshClient;7importcom.sshtools.j2ssh.authentication.AuthenticationProtocolState;8importcom.sshtools.j2ssh.authentication.PasswordAuthenticationClient;9importcom.sshtools.j2ssh.session.SessionChannelClient;1011publicclassMain {1213publicstaticvoidmain(String[] args) {14SshClient ssh=newSshClient();15PasswordAuthenticationClient authentication=newPasswordAuthenticationClient();16authentication.setUsername("root");17authentication.setPassword("123");18try{19ssh.connect("192.168.94.254",22,newHostsKeyVerification());20if(ssh.authenticate(authentication)==AuthenticationProtocolState.COMPLETE) {21SessionChannelClient session=ssh.openSessionChannel();22//session.setEnvironmentVariable("TERM", "linux");23//if (client.requestPseudoTerminal("vt100", 120, 400, 0, 0,24//"")) {25if(session.startShell()) {26OutputStream writer=session.getOutputStream();27writer.write("echo $?\n".getBytes());28writer.flush();29writer.write("exit\n".getBytes());30writer.flush();31BufferedReader in=newBufferedReader(32newInputStreamReader(session.getInputStream()));33BufferedReader err=newBufferedReader(34newInputStreamReader(session35.getStderrInputStream()));36String line;37while((line=in.readLine())!=null) {38System.out.println(line);39}40System.out.println("------------------------");41while((line=err.readLine())!=null) {42System.out.println(line);43}44if(session!=null) {45session.close();46}47}48//}49}50}catch(IOException e) {51e.printStackTrace();52}finally{53}5455}5657}
java j2ssh_J2SSH - tudou@NorthWind - 博客园
最新推荐文章于 2021-08-06 02:56:18 发布