日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Jsch ssh登陆

發布時間:2025/3/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jsch ssh登陆 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Jsch ssh登陸

package com.xxxx;import com.jcraft.jsch.*;import java.io.BufferedReader; import java.io.InputStreamReader;/*** Created by xxx on 2017/4/5.*/ public class JshMain {public static void main(String args[]){JSch jsch = new JSch();try {// Create and connect session.Session session = jsch.getSession("root", "192.168.60.225", 22);session.setPassword("xxxx");session.setUserInfo(new MyUserInfo());session.connect();// Create and connect channel.Channel channel = session.openChannel("exec");((ChannelExec) channel).setCommand("ls");channel.setInputStream(null);BufferedReader input = new BufferedReader(new InputStreamReader(channel.getInputStream()));channel.connect();System.out.println("The remote command is: " + "ls");// Get the output of remote command.String line;while ((line = input.readLine()) != null) {System.out.println(line);}input.close();// Get the return code only after the channel is closed.//if (channel.isClosed()) {// returnCode = channel.getExitStatus();//}// Disconnect the channel and session.channel.disconnect();session.disconnect();} catch (JSchException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}class MyUserInfo implements UserInfo {private String password;private String passphrase;@Overridepublic String getPassphrase() {System.out.println("MyUserInfo.getPassphrase()");return null;}@Overridepublic String getPassword() {System.out.println("MyUserInfo.getPassword()");return null;}@Overridepublic boolean promptPassphrase(final String arg0) {System.out.println("MyUserInfo.promptPassphrase()");System.out.println(arg0);return false;}@Overridepublic boolean promptPassword(final String arg0) {System.out.println("MyUserInfo.promptPassword()");System.out.println(arg0);return false;}//本方法是一個關鍵的問題,返回true就可以@Overridepublic boolean promptYesNo(final String arg0) {System.out.println("MyUserInfo.promptYesNo()");System.out.println(arg0);if (arg0.contains("The authenticity of host")) {return true;}return false;}@Overridepublic void showMessage(final String arg0) {System.out.println("MyUserInfo.showMessage()");} }

無密碼登陸

jsch = new JSch();try {session = jsch.getSession("admin", host, 22);String privateKey = "~/.ssh/id_rsa";jsch.addIdentity(privateKey);java.util.Properties config = new java.util.Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);session.connect();} catch (Exception e){//log.error("",e);baseResult.setData(e);return baseResult;}

?

轉載于:https://my.oschina.net/QAAQ/blog/873459

總結

以上是生活随笔為你收集整理的Jsch ssh登陆的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。