java runtime 객체 와 ssh를 활용한 파일 배포
Intro
java단에서 sftp를 활용하여 java 소스를 배포하는 업무 개선 개발을 하게되었고 runtime 객체를 이용하여 java단에서 linux 명령어를 직접 호출하는 방법을 고려하게 되었습니다.
but ssh, 및 scp 명령어를 호출할 경우 패스워드를 묻게 되는데 이를 처리 하는 방법은 ssh 설정을 하면 됩니다.
조치 방법은 리눅스 SSH를 이용한 암호없이 로그인 설정 포스트 참고 바랍니다.
소스 내용
private void deploySCP(String host, String id, List fileList) throws Exception {
String qaPath = null;
String prodPath = null;
String backupPath = null;
String cmd = null;
for (int i = 0; i < fileList.getDataCount(); i++) {
qaPath = fileList.getString("from", i);
prodPath = fileList.getString("to", i);
backupPath = fileList.getString("backup", i);
if (i == 0) {
cmd = "ssh " + id + "@" + host + " mkdir -p " + backupPath.substring(0, backupPath.lastIndexOf("/"));
} else {
cmd = cmd + " ; ssh " + id + "@" + host + " mkdir -p "
+ backupPath.substring(0, backupPath.lastIndexOf("/"));
}
cmd = cmd + " ; ssh " + id + "@" + host + " cp " + prodPath + " " + backupPath;
cmd = cmd + " ; ssh " + id + "@" + host + " mkdir -p " + prodPath.substring(0, prodPath.lastIndexOf("/"));
cmd = cmd + " ; scp " + qaPath + " " + id + "@" + host + ":" + prodPath;
}
String[] cmdArray = { "/bin/sh", "-c", cmd };
Runtime.getRuntime().exec(cmdArray); //명령어를 실행
}
댓글남기기