expect로 만든 스크립트에서 변수 값을 읽어 들이려면?

익명 사용자의 이미지

저는 아래와 같은 스크립트를 만들었습니다.

--------------zz-------------------------
#!/usr/bin/expect
spawn bash -c "su - aaa"
expect -re "Password"
sleep 0.2
send "bbb\n"
interact
---------------zz-------------------------

자동으로 ID와 PASSWD를 입력해주는 아주 간단한 스크립트입니다.
그런데 여기에다가 입력값을 받아서 실행시키고 싶습니다.
예를 들면, 스크립트(파일명zz)를

$zz aaa bbb

이런식으로 명령을 내려 주면 처음에 입력된(aaa)는 ID로,
나중에 입력한 (bbb)는 PASSWD로 읽어 들여서 실행시키고 싶습니다.

그래서 저 나름대로 아래와 같이 프로그램을 만들어 봤는데, 에러가 나네
요 ^^;

#!/usr/bin/expect
#!/bin/bash
a=$1
b=$2
spawn bash -c "su - $a"
expect -re "Password"
sleep 0.2
send "$b\n"
interact

아래와 같이 에러가 납니다..

$zz aaa bbb

can`t read "1" no such variable
while executing
"a=$1"
(file "/usr/local/bin/zz" line 3)

제 생각에는 /bin/bash 이 아닌 expect 프로그램 때문인듯 합니다.(베
시 쉘에서는
되는데 말이죠...)

혹시 아시는 분 계시면 도움 주시면 좋겠습니다..

익명 사용자의 이미지


#!/usr/bin/expect
#!/bin/bash
a=$1
b=$2

에서 #!/bin/bash 는 expect에는 그냥 주석문으로 인식되기 때문에
아무런 효과가 없습니다.

expect에서는 다른 프로그램들과 비슷하게 argv와 argc에
파라메터를 넣습니다. (사실 이건 TCL 이 이렇고, expect는 TCL의
확장이니 당연히 동일하게 쓰겠죠?)
따라서 argv와 argc 를 조작하시면 됩니다.
참고로, 프로그램명은 argv0 에 들어갑니다.

아래 예를 참고 하세여..

#!/usr/local/bin/expect

if {$argc < 2} {
puts "usage progname id pwd"
exit
}

set id [lindex $argv 0]
set pwd [lindex $argv 1]

puts "program name $argv0"
puts "id $id"
puts "pwd $pwd"

익명 사용자의 이미지

답변 감사합니다. 덕분에 해결했습니다.

한가지만 더 물어봐도 될까요?

로그인 한뒤에 외부 프로그램을 실행시키고 싶은데요..

#!/usr/bin/expect

set id [lindex $argv 0]
set pwd [lindex $argv 1]

spawn bash -c "su - $id"
expect -re "Password"
sleep 0.2
send "$pwd\n"
ls <----- 추가
interact

제가 의도하는 것은 로그인 한뒤에 ls 명령어를 자동으로 실행시키고 싶습니다.
역시 일반 bash쉘에서 같이 그냥 'ls' 라고 추가시켜주니 에러가 나네요~

익명 사용자의 이미지

일반적으로는 expect 를 별도로 만들어서 쉘스크립트에서 호출
하는 방법을 많이 쓰는데, 쉘 스크립트 안에서 command line 과
같이 -c 옵션을 줘서 사용할 수도 있습니다. 다음 소스를 참조
하시면 될 겁니다.

-- cut here --
#1/bin/sh

# 변경할 계정 이름
changeuser=oops
# 변경할 패스워드를 랜덤하게 구함.
# $$ 는 이 스크립트가 실행하는 프로세스 ID 임.
# 이 부분은 알아서 알고리즘을 만드시기를.
changepass=$$

expect -c "spawn passwd $changeuser" \
-c "expect -re \"New password\"" \
-c "send \"$changepass\r\"" \
-c "expect -re \"Retype new password\"" \
-c "send \"$changepass\r\"" \
-c "interact" > /dev/null 2>&1

-- cut here --

댓글 달기

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