ssh를 이용하여 웹서버에서 쉘 명령 실행결과를 가져오기

thepath의 이미지

안녕하세요. 저는 네트워크 상태를 모니터링하는 프로그램을 만들고 있습니다.

서버 여러 대의 네트워크 포트를 계속 기록하는 프로그램입니다.

"netstat" 실행 결과의 일부를 일정시간마다 저장하는 프로그램입니다.

서버가 외부(공개)망에서 접속할 수 없어서 웹서버를 통해

http://xxx.org/xxxx.cgi?xxx=yyy&... 로 접속하여

네트워크 상태를 가져오려고 합니다.

다른 컴퓨터에서 여러 대의 컴퓨터에 동일한 쉘 명령을 실행할 방법을 찾다가

네이버 개발자 센터에서 dist 라는 프로젝트를 보고

http://dev.naver.com/projects/dist/#s-1.2.1.1

웹서버를 마스터로 감시할 대상이 되는 여러 대의 서버를 타켓으로 하여

ssh 자동 로그인 설정을 하였습니다.

터미널로 "ssh ips ls 명령어"가 성공하여 아래와 같이 jsp 코드를 작성하였습니다.

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR" %>
<%@ page import="java.io.BufferedReader"
         import="java.io.InputStreamReader"
         import="java.io.IOException"
         import="java.io.PrintWriter"
         import="java.io.SequenceInputStream" %>
<%
try {
  PrintWriter wr = response.getWriter();
  String cmds[][] = {
    {"ssh", "ip", "netstat -na"},
     ....
    {....                      }};
  for (int i=0;i<n;i++) {
    String aLine = null;
    int cnt = 0;
    Process ps = Runtime.getRuntime().exec(cmds[i]);
    BufferedReader br = new BufferedReader(
        new InputStreamReader(
            new SequenceInputStream(ps.getInputStream(), ps.getErrorStream())
        ));
    while (null != (aLine = br.readLine())) wr.println(aLine);
    br.close();
    ps.waitFor();
  }
} catch (Exception e) {
  e.printStackTrace();
}
%>

웹브라우저의 주소창에 입력하여 테스트하였을 때 실행결과(stdout+stderr)를 텍스트로 받을

수 있었는데 모든 서버의 비밀번호를 바꾼 후로

"Host key verification failed" 오류가 발생합니다.

위의 내용으로 검색하여보니 known_host 파일 삭제가 해결방법같아서 삭제하였으나

터미널에서는 잘 실행되지만 jsp로 실행하면 실패합니다.

그래서 -v 옵션으로 실행하여보니 ssh 설정파일을 /etc/ssh/config 사용하였습니다.

웹서버 프로그램을 실행하는 계정(= bob)의 ssh 설정파일을 읽도록 -F 옵션을 사용하였지만 파일을 찾지 못하네요.

웹서버를 통하니 bob 계정으로 ssh 쉘명령을 실행할 것 같은데 bob의 ssh 설정파일을 읽지 못하는지 모르겠습니다.

익명 사용자의 이미지

장하다..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.