简体   繁体   English

预期产生:没有这样的文件或目录错误

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

I am writing an Expect script that remotes to a server "server2" and executes fileexists.sh that will find if file exists at a particular location. 我正在编写一个Expect脚本,该脚本可以远程访问服务器“ server2”并执行fileexists.sh,它将查找文件是否存在于特定位置。

Bash script - fileexists.sh is as follows: Bash脚本-fileexists.sh如下:

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

Expect script - expec is as follows: 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 

When i call the expect script with ./expec user pass server2 /apps/bin/file.p I get output as "File doesnot exist or is empty." 当我使用./expec用户通过server2 /apps/bin/file.p调用Expect脚本时,输出为“文件不存在或为空”。 although the file exists at its location on server2. 尽管该文件位于server2上的位置。

i checked using expect: if {[file exists $rfile]} { puts "file exists" } 我使用了期望值进行了检查:如果{[文件存在$ rfile]} {将“文件存在”放入}

And output i get is "file exists". 我得到的输出是“文件存在”。

Seems to be something with spawn that I am unable to figure out. 似乎是我无法弄清楚的东西。

To check on the remote host, do this (I assume you are logging in to a linux machine, or a machine with GNU stat ): untested 要检查远程主机,请执行以下操作(假设您正在登录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