import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SshLinux {
private static final Logger log = LoggerFactory.getLogger(SshLinux.class);
private static String DEFAULTCHART="UTF-8";
/**
* 登录主机
* @return
* 登录成功返回true,否则返回false
*/
public static Connection login(String ip,
String userName,
String userPwd){
boolean flg=false;
Connection conn = null;
try {
conn = new Connection(ip);
conn.connect();//连接
flg=conn.authenticateWithPassword(userName, userPwd);//认证
if(flg){
log.info("=========登录成功========="+conn);
return conn;
}
} catch (IOException e) {
log.error("=========登录失败========="+e.getMessage());
e.printStackTrace();
}
return conn;
}
/**
* 远程执行shll脚本或者命令
* @param cmd
* 即将执行的命令
* @return
* 命令执行完后返回的结果值
*/
public static String execute(Connection conn,String cmd){
String result="";
try {
if(conn !=null){
Session session= conn.openSession();//打开一个会话
session.execCommand(cmd);//执行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
//如果为得到标准输出为空,说明脚本执行出错了
if(StringUtils.isBlank(result)){
log.info("得到标准输出为空,链接conn:"+conn+",执行的命令:"+cmd);
result=processStdout(session.getStderr(),DEFAULTCHART);
}else{
log.info("执行命令成功,链接conn:"+conn+",执行的命令:"+cmd);
}
conn.close();
session.close();
}
} catch (IOException e) {
log.info("执行命令失败,链接conn:"+conn+",执行的命令:"+cmd+" "+e.getMessage());
e.printStackTrace();
}
return result;
}
/**
* 解析脚本执行返回的结果集
* @param in 输入流对象
* @param charset 编码
* @return
* 以纯文本的格式返回
*/
private static String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
String line=null;
while((line=br.readLine()) != null){
buffer.append(line+"\n");
}
} catch (UnsupportedEncodingException e) {
log.error("解析脚本出错:"+e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("解析脚本出错:"+e.getMessage());
e.printStackTrace();
}
return buffer.toString();
}
public static String photograph(String url){
SshLinux sshLinux = new SshLinux();
Connection connection = SshLinux.login("192.168.1.142", "root", "123456");
String imageName = "12.jpg";
// String res = sshLinux.execute(connection,"ffmpeg -i rtsp://admin:[email protected]:554/Streaming/Channels/101?transportmode=unicast -y -f image2 -ss 2 -vframes 1 " + imageName);
String res = sshLinux.execute(connection,String.format("ffmpeg -i %s -y -f image2 -ss 2 -vframes 1 %s",url,imageName));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
connection = SshLinux.login("192.168.1.142", "root", "123456");
String upload = sshLinux.execute(connection,"/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/" + imageName);
connection = SshLinux.login("192.168.1.142", "root", "123456");
sshLinux.execute(connection,"rm /root/" + imageName);
return upload;
}
// public static void main(String[] args) {
// SshLinux sshLinux = new SshLinux();
// Connection connection = SshLinux.login("192.168.1.142", "root", "123456");
// String imageName = "12.jpg";
// String res = sshLinux.execute(connection,"ffmpeg -i rtsp://admin:[email protected]:554/Streaming/Channels/101?transportmode=unicast -y -f image2 -ss 2 -vframes 1 " + imageName);
// try {
// Thread.sleep(20);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// connection = SshLinux.login("192.168.1.142", "root", "123456");
// String upload = sshLinux.execute(connection,"/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/" + imageName);
//
//
// connection = SshLinux.login("192.168.1.142", "root", "123456");
// sshLinux.execute(connection,"rm /root/" + imageName);
// return upload;
// }
}
SshLinux 工具类
猜你喜欢
转载自blog.csdn.net/bj_ameng/article/details/111662867
今日推荐
周排行