์๋ํ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๋ ๊ฑด ์ค์ํ ์ผ์ด๋ค. ์ด๊ฒ ์์ผ๋ฉด DB ์คํ/๋ฐฑ์ ๊ณผ ๊ฐ์ด ์ผ์์ ์ด๋ฉด์ ๋ฐ๋ณต๋๋ ์์ ๋ค์ ์ผ์ผ์ด ํด์ค์ผํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฆฌ๋ ์ค ์๋ฒ์ ์ ์ํ๊ณ ... ๋ก๊ทธ์ธํ๊ณ ... ๋ช ๋ น์ด ํ๋ํ๋๋ฅผ ํ ๋ ํ ๋ ๋ฃ์ด์ฃผ๋ ์ผ์ ๊ฝค๋ ๊ณ ๋ ์ผ์ด๋ค. ์ด๋ฐ ์ผ๋ค์ ์๋ํํ๊ธฐ ์ํด ์ผ๋ฐ์ ์ผ๋ก ์ ์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ง๋ง, ํน์ ์๋ฐ ๊ฐ๋ฐ์๋ค์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์์๊น ์ฐพ์๋ณด๋ JSch๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค๊ณ ํ๋ค.
JSch ๋?
๋ฆฌ๋ ์ค ์๋ฒ๋ฅผ ์๊ฒฉ์ผ๋ก ๊ด๋ฆฌํ ๋, SSH (Secure Shell)๋ฅผ ํตํด ๋ช ๋ น์ด๋ฅผ ์ ์กํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ค. ์ด๋ฅผ ์ํด ์๋์ฐ์์๋ ํ์ด์ฌ์ paramiko ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๋๋ฐ, ์๋ฐ์๋ JSch๋ผ๋ ๋น์ทํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ค. JSch๋ ์๋ฐ๋ก ๊ตฌํ๋ SSH2 ํ๋กํ ์ฝ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ฐ ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ฆฌ๋ ์ค ์๋ฒ๋ก ๋ช ๋ น์ด๋ฅผ ์ ์กํ๊ณ ์คํํ ์ ์๋ค.
๊ธฐ๋ณธ ์ฝ๋ ์์
์๋๋ JSch๋ฅผ ์ฌ์ฉํ์ฌ ๋ฆฌ๋ ์ค ์๋ฒ์ ์ ์ํ๊ณ ๊ฐ๋จํ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ ์์ ์ด๋ค. ๋ช ๋ น์ด๋ฅผ ์คํํ ํ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ๋ถ๋ถ์์ ํด๋ผ์ด์ธํธ ์ชฝ stream๊ณผ ์๋ฒ ์ชฝ stream ๋ ๋ค ๋๊ธฐ์ํ๋ก ๋ค์ด๊ฐ ๋ฐ๋๋ฝ์ ๊ฑธ๋ฆด ์ ์์ผ๋ ๊ตฌํํ ๋ ์ฃผ์ํด์ผํ๋ค.
import java.io.*;
import com.jcraft.jsch.*;
public class JschExample {
public static void main(String[] args) {
final String host = "xxx.xxx.xxx.xxx";
final String username = "yourUsername";
final String password = "yourPassword";
final int port = 22;
Session session = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no"); // ํธ์คํธ ํค ๊ฒ์ฆ ๋นํ์ฑํ (๋ณด์์ ๊ถ์ฅ๋์ง ์์, ํ
์คํธ์ฉ์ผ๋ก๋ง)
session.connect();
executeCommand(session, "ls -l");
} catch (Exception e) {
// error handling
} finally {
if (session != null) {
session.disconnect();
}
}
}
private static void executeCommand(Session session, String command) throws IOException, JSchException {
ChannelExec channel = (ChannelExec) session.openChannel("exec"); // sftp ์ฑ๋ ์์ฑ
channel.setCommand(command);
ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
ByteArrayOutputStream errorBuffer = new ByteArrayOutputStream();
InputStream in = channel.getInputStream();
InputStream err = channel.getExtInputStream();
channel.connect();
byte[] temp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(temp, 0, 1024);
if (i < 0) {
break;
}
outputBuffer.write(temp, 0, i);
}
while (err.available() > 0) {
int i = err.read(temp, 0, 1024);
if (i < 0) {
break;
}
errorBuffer.write(temp, 0, i);
}
if (channel.isClosed()) {
if ((in.available() > 0) || err.available() > 0) {
continue;
}
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
String output = outputBuffer.toString("UTF-8");
String error = errorBuffer.toString("UTF-8");
if (output != null && output.length() > 0)
System.out.println("output: " + output);
if (error != null && error.length() > 0)
System.out.println("error: " + error);
channel.disconnect();
}
}
๊ณต์ ์์ ๋ http://www.jcraft.com/jsch/examples/Exec.java.html ← ์ฌ๊ธฐ์ ๋ ์ฐพ์๋ณผ ์ ์๋ค.
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ถ๋์์์ ์ค์ฐจ ๊ด๋ฆฌํ๊ธฐ (0) | 2024.05.16 |
---|---|
[Java] BitSet (0) | 2024.04.19 |