/**
* 由SshConfig配置获取一个Session
* @param conf
* @return
*/
public static Session createSession(SshConfig conf){
LOG.info("try connect to " + conf.getHostIp() + ",username: " + conf.getUsername() + ",password: " + conf.getPassword() + ",port: " + conf.getPort());
JSch jSch=new JSch(); //创建JSch对象
Session session= null;//根据用户名,主机ip和端口获取一个Session对象
try {
session = jSch.getSession(conf.getUsername(), conf.getHostIp(), conf.getPort());
session.setPassword(conf.getPassword()); //设置密码
Properties config=new Properties();
//StrictHostKeyChecking no
//"StrictHostKeyChecking"如果设为"yes",ssh将不会自动把计算机的密匙加入"$HOME/.ssh/known_hosts"文件,且一旦计算机的密匙发生了变化,就拒绝连接
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);//为Session对象设置properties
session.setTimeout(conf.getTimeOut());//设置超时
session.connect();//通过Session建立连接
LOG.info("connect to {} success",conf.getHostIp());
} catch (JSchException e) {
LOG.error("connect to {} fail, connect infos:{}",conf.getHostIp(),conf.toString());
e.printStackTrace();
}
return session;
} /**
* 关闭session
* @param session
*/
public static void closeSession(Session session){
session.disconnect();
}
/**
* 从src文件拿到本地dst文件
* @param session
* @param src
* @param dst
* @return
*/
public static Boolean downLoad(Session session,String src,String dst){
Boolean success=true;
//src linux服务器文件地址,dst 本地存放地址
ChannelSftp channelSftp= null;
try {
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(src, dst);
channelSftp.quit();
} catch (JSchException e) {
success=false;
e.printStackTrace();
LOG.error(e.getMessage());
} catch (SftpException e) {
success=false;
e.printStackTrace();
LOG.error(e.getMessage());
}
return success;
}
/**
* 由ip 用户名 密码 获取该ip对应的hostname
* @param hostIp
* @param username
* @param password
* @return
*/
public static String getHostName(String hostIp, String username, String password){
Session session=createSession(new SshConfig(hostIp,username,password));
String hostname = exeCommand(session,"hostname");
return hostname.trim();
}
/**
* 执行command命令,把执行结果返回
* @param session
* @param command
* @return
*/
public static String exeCommand(Session session,String command){
ChannelExec channelExec = null;
String out=null;
try {
channelExec = (ChannelExec) session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand(command);
channelExec.setErrStream(System.err);
channelExec.connect();
out = IOUtils.toString(in, "UTF-8"); channelExec.disconnect();
//session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(null==out){
LOG.error("execute {} error",command);
out="";
}
return out;
}
public class SshConfig {
private String hostIp;
private String username;
private String password;
//默认端口22
private int port=;
//10个小时超时
private int timeOut=***; public SshConfig(String hostIp, String username, String password) {
this.hostIp = hostIp;
this.password = password;
this.username = username;
} public String getHostIp() {
return hostIp;
} public void setHostIp(String hostIp) {
this.hostIp = hostIp;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public int getPort() {
return port;
} public void setPort(int port) {
this.port = port;
} public int getTimeOut() {
return timeOut;
} public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
} @Override
public String toString() {
String builder="\nhostIp"+" : " + hostIp
+"\nusername"+" : " + username
+"\npassword"+" : " + password
+"\nport"+" : " + port
+"\ntimeOut"+" : " + timeOut;
return builder;
} }