java 나 C/C++ 등으로 expect 와 같은 대화형 프로그램을 쉽게

vudghkzm의 이미지

간단하게 ssh 접속을 자동화 할 수 있는 프로그램을 짤려고 하고 있습니다. 인증키를 통한게 아니라 비밀번호를 입력하는 방식으로 말이죠..

그래서 expect 를 사용해보았는데..expect 는 스크립트라 소스코드가 그대로 노출될 수 있다는 위험이 있더군요. 그래서 이걸 바이너리화 할 수 있는 방법을 찾다가 libexpect 라는 것이 있다고 하길래 사용해 볼려고 했습니다. 하지만 문서라고는 man 페이지 뿐이더군요.. 맨 페이지를 읽고 삽질을 해보았으나 도저히 잘 안되더라구요..

C/C++ 혹은 자바등으로 expect 를 쉽게 흉내낼 수 있는 방법이 없을런지요?

익명 사용자의 이미지

초간단 예제되겠습니다.
gdb를 실행시키고, 프롬프트 (gdb)가 나타나면 help보내고,
다시, 프롬프트 (gdb)가 나타나면, quit를 보낸다는....
\n을 보내야 실행될테고,....

* 고려사항 :
- timeout등을 검사해야 할것이다.(연결이 안되면 어쩌지?등)
- 에러메시지에 대한 검토도 해야할 것이다.(예. 암호가 틀렸다는..등의...)

// gcc -o test test.c -lexpect
#include <stdio.h>
#include <expect.h>

int main(){

   FILE *fp1;
   int ec;
   char buff[2][128]= {"help", "quit"};
   exp_loguser = 1;
   exp_timeout = 3600;

   if (0 == (fp1 = exp_popen("gdb"))) {

      printf("exp_popen failed\n");
      exit(-1);
   }
   if (0 > exp_fexpectl(fp1,exp_glob,"(gdb)",0,exp_end)) exit(-1);
   fprintf(fp1,"%s\n", buff[0]);
   if (0 > exp_fexpectl(fp1,exp_glob,"(gdb)",0,exp_end)) exit(-1);
   fprintf(fp1,"%s\n", buff[1]);

   return(0);
}
vudghkzm의 이미지

답변 감사드립니다. 근데 interactive 모드로의 전환은 어떻게 하면 되는지요?

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.