bashスクリプトループでExpectスクリプトを呼び出す

bashスクリプトループでExpectスクリプトを呼び出す

予想されるスクリプトを使用してサイトのリストを繰り返そうとします。このループは私のリストを取得し、1行ずつ読み込み、ディレクトリをサイト名のフォルダに変更し、サイト名を予想されるスクリプトに渡します。

クンクンスクリプト:

#!/bin/bash
sites="/home/user/sites.cfg"

while read site; do
  cd /home/user/$site
  pwd
  echo $site
  ./base.sh
done < $sites

期待されるスクリプト:

#!/usr/bin/expect

set site [lindex $argv 0]
set timeout 3

logfile services
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -oUserKnownHostsFile=/dev/null user@$site
expect "password" { send mypassword\r }
expect "#"
send environment\ no\ more\n
expect "#"
send show\ service\ service-using\n
expect "#"
send logout\n

結果:

user@myserver:~$ sh show-database.sh
/home/user/site-01
site-01
show-database.sh: 11: show-database.sh: ./base.sh: not found
/home/user/site-02
site-02
show-database.sh: 11: show-database.sh: ./base.sh: not found

すべてのサイトのすべてのフォルダでservicesというファイルを見たいです。次のコマンドを実行でき、CLIで動作しますが、ディレクトリは変更されません。これは、ループでより多くのタスクを実行するより大きなスクリプトの始まりにすぎません。今これが私がテストしたいものです。

./base.sh site-01

ありがとうございます!

ベストアンサー1

bashラッパースクリプトはまったく必要ありません。すべてを処理できると期待します。

#!/usr/bin/expect
set timeout 3

set rootdir /home/user
set sites [file join $rootdir sites.cfg]
set fh [open $sites r]

while {[gets $fh site] != -1} {
    set dir [file join $rootdir $site]
    if { ! [file isdirectory $dir]} {
        puts "*** error: $dir does not exist"
        continue
    } 

    cd $dir
    puts [pwd]
    puts $site

    log_file services

    spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -oUserKnownHostsFile=/dev/null user@$site
    expect "password" 
    send "mypassword\r 
    expect "#"
    send "environment no more\r"
    expect "#"
    send "show service service-using\r"
    expect "#"
    send "logout\r"
    expect eof

    log_file
}

おすすめ記事