java j2ssh_J2SSH - tudou@NorthWind - 博客园

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}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值