簡體   English   中英

預期產生:沒有這樣的文件或目錄錯誤

[英]Expect spawn: No such file or directory error

我正在編寫一個Expect腳本,該腳本可以遠程訪問服務器“ server2”並執行fileexists.sh,它將查找文件是否存在於特定位置。

Bash腳本-fileexists.sh如下:

 #!/bin/bash if [ -s $1 ]; then echo "non empty file exists." else echo "File doesnot exist or is empty." 

Expect腳本-expec如下:

 #!/usr/bin/expect set username [lindex $argv 0] set password [lindex $argv 1] set hostname [lindex $argv 2] set rfile [lindex $argv 3] set prompt "$username* ~]$ " spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname expect { timeout { puts "\\n\\nConnection timed out" exit 1 } "*assword:" { send "$password\\r" } } expect { "$prompt" { puts "Logged in successfully. } } spawn ./fileexists.sh $rfile set lineterminationChar "\\r" expect { $lineterminationChar { append output $expect_out(buffer);exp_continue} eof { append output $expect_out(buffer)} } puts $output 

當我使用./expec用戶通過server2 /apps/bin/file.p調用Expect腳本時,輸出為“文件不存在或為空”。 盡管該文件位於server2上的位置。

我使用了期望值進行了檢查:如果{[文件存在$ rfile]} {將“文件存在”放入}

我得到的輸出是“文件存在”。

似乎是我無法弄清楚的東西。

要檢查遠程主機,請執行以下操作(假設您正在登錄linux機器或具有GNU stat的機器):未經測試

send -- "stat -c '%s' '$rfile'\r"

expect {
    -re {\r\n(\d+)\r\n} {
        puts "$rfile exists with size $expect_out(1,string)"
    }
    -gl {*No such file or directory*} {
         puts "$rfile does not exist"
    }
}

send "exit\r"
expect eof

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM