说明:exec用于执行命令;sftp用于文件处理
1 package com.wyg.simple;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.util.Properties;
11 import java.util.Vector;
12
13 import com.jcraft.jsch.Channel;
14 import com.jcraft.jsch.ChannelExec;
15 import com.jcraft.jsch.ChannelSftp;
16 import com.jcraft.jsch.JSch;
17 import com.jcraft.jsch.JSchException;
18 import com.jcraft.jsch.Session;
19 import com.jcraft.jsch.SftpException;
20
21 public class JSchUtils {
22 private static JSch jsch;
23 private static Session session;
24
25 /**
26 * 连接到指定的IP
27 *
28 * @throws JSchException
29 */
30 public static void connect(String user, String passwd, String host, int port) throws JSchException {
31 jsch = new JSch();// 创建JSch对象
32 session = jsch.getSession(user, host, port);// 根据用户名、主机ip、端口号获取一个Session对象
33 session.setPassword(passwd);// 设置密码
34
35 Properties config = new Properties();
36 config.put("StrictHostKeyChecking", "no");
37 session.setConfig(config);// 为Session对象设置properties
38 session.setTimeout(1500);// 设置超时
39 session.connect();// 通过Session建立连接
40 }
41
42 /**
43 * 关闭连接
44 */
45 public static void close() {
46 session.disconnect();
47 }
48
49 /**
50 * 执行相关的命令
51 *
52 * @throws JSchException
53 */
54 public static void execCmd(String command) throws JSchException {
55 BufferedReader reader = null;
56 Channel channel = null;
57 try {
58 if (command != null) {
59 channel = session.openChannel("exec");
60 ((ChannelExec) channel).setCommand(command);
61 // ((ChannelExec) channel).setErrStream(System.err);
62 channel.connect();
63
64 InputStream in = channel.getInputStream();
65 reader = new BufferedReader(new InputStreamReader(in));
66 String buf = null;
67 while ((buf = reader.readLine()) != null) {
68 System.out.println(buf);
69 }
70 }
71 } catch (IOException e) {
72 e.printStackTrace();
73 } catch (JSchException e) {
74 e.printStackTrace();
75 } finally {
76 try {
77 reader.close();
78 } catch (IOException e) {
79 e.printStackTrace();
80 }
81 channel.disconnect();
82 }
83 }
84
85 /**
86 * 上传文件
87 *
88 * @param directory
89 * 上传的目录
90 * @param uploadFile
91 * 要上传的文件
92 * @param sftp
93 * @throws JSchException
94 * @throws SftpException
95 * @throws FileNotFoundException
96 */
97 public void upload(String directory, String uploadFile) throws JSchException, FileNotFoundException, SftpException {
98 ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
99 channelSftp.cd(directory);
100 File file = new File(uploadFile);
101 channelSftp.put(new FileInputStream(file), file.getName());
102 System.out.println("Upload Success!");
103 }
104
105 /**
106 * 下载文件
107 *
108 * @param src
109 * @param dst
110 * @throws JSchException
111 * @throws SftpException
112 */
113 public static void download(String src, String dst) throws JSchException, SftpException {
114 // src linux服务器文件地址,dst 本地存放地址
115 ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
116 channelSftp.connect();
117 channelSftp.get(src, dst);
118 channelSftp.quit();
119 }
120
121 /**
122 * 删除文件
123 *
124 * @param directory
125 * 要删除文件所在目录
126 * @param deleteFile
127 * 要删除的文件
128 * @param sftp
129 * @throws SftpException
130 * @throws JSchException
131 */
132 public void delete(String directory, String deleteFile) throws SftpException, JSchException {
133 ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
134 channelSftp.cd(directory);
135 channelSftp.rm(deleteFile);
136 }
137
138 /**
139 * 列出目录下的文件
140 *
141 * @param directory
142 * 要列出的目录
143 * @param sftp
144 * @return
145 * @throws SftpException
146 * @throws JSchException
147 */
148 @SuppressWarnings("rawtypes")
149 public Vector listFiles(String directory) throws JSchException, SftpException {
150 ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
151 return channelSftp.ls(directory);
152 }
153
154 public static void main(String[] args) {
155 try {
156 // 1.连接到指定的服务器
157 connect("root", "sankairoot", "10.0.0.251", 22);
158
159 // 2.执行相关的命令
160 execCmd("grep '160622150549943666' /data/apps/nginx/logs/access_2016-07-07.log >> /data/nginx_log.20160707.txt");
161
162 // 3.下载文件
163 download("/data/nginx_log.20160707.txt", "D:\temp");
164
165 // 4.关闭连接
166 close();
167 } catch (JSchException e) {
168 // TODO Auto-generated catch block
169 e.printStackTrace();
170 } catch (SftpException e) {
171 // TODO Auto-generated catch block
172 e.printStackTrace();
173 }
174 }
175 }