Is there any way to send if else statement to remote server while using expect command in shell script?

huangapple go评论60阅读模式
英文:

Is there any way to send if else statement to remote server while using expect command in shell script?

问题

我想使用expect连接到远程服务器,通过ssh检查特定目录是否存在,如果不存在,则创建文件,如果存在,则创建后续目录。我可以连接到远程服务器并发送命令,但当涉及到if else语句时,它不接受。

以下是我的代码片段。它只连接远程服务器然后退出。

#!/usr/bin/expect -f

set timeout 120
spawn ssh user@remoteIP
expect "assword:"
send "远程密码"
expect "$ "
set name "b"
send "if [ -d /path/to/remote/directory/$name ]; then
    set i 1
  while [ -d /path/to/remote/directory/$name$i ]; do
      let i++
    done
   set name $name$i
  fi"
send -- "mkdir -p /path/to/remote/directory/$name\r"
expect "$ "
send -- "exit\r"
expect eof
英文:

I want to connect a remote server via ssh using expect and check specific directory exists if not create file if exists create subsequent directory. I can connect remote server and send line commands but when it comes to statement like if else it doesn't accept.

Here is my code snippet. It only connects remote and exits.

#!/usr/bin/expect -f

set timeout 120
spawn ssh user@remoteIP
expect "*?assword:"
send "remote password"
expect "$ "
set name "b"
send "if [ -d /path/to/remote/directory/$name ]; then
    set i 1
  while [ -d /path/to/remote/directory/$name$i ]; do
      let i++
    done
   set name $name$i
  fi"
send -- "mkdir -p /path/to/remote/directory/$name\r"
expect "$ "
send -- "exit\r"
expect eof

答案1

得分: 1

你发送到 shell 的代码需要是有效的 shell 脚本。你不能期望 shell 更新你的 Expect 变量,反之亦然。

尝试以下:

```shell
send "i=''"
while [ -d /path/to/remote/directory/$name$i ]; do
   let i++
done
mkdir -p /path/to/remote/directory/$name$i\r"

变量 $name 是你的 Expect 变量,而 \$i 应该原样传递给 shell,就像 $i 一样。类似地,[ 需要转义以避免将其作为 Expect 结构执行。

为了完整起见,以下是具有这些更改的完整脚本:

#!/usr/bin/expect -f

set timeout 120
spawn ssh user@remoteIP
expect "*?assword:"
send "remote password"
expect "$ "
set name "b"
send "i=''"
while [ -d /path/to/remote/directory/$name$i ]; do
   let i++
done
mkdir -p /path/to/remote/directory/$name$i\r"
expect "$ "
send -- "exit\r"
expect eof

<details>
<summary>英文:</summary>

The code you send to the shell needs to be valid shell script. You can&#39;t expect the shell to update your Expect variables, or vice versa.

Try the following:

send "i=''
while [ -d /path/to/remote/directory/$name$i ]; do
let i++
done
mkdir -p /path/to/remote/directory/$name$i\r"


The variable `$name` is your Expect variable, while `$i` should be passed through to the shell as literally `$i`. Similarly, `[` needs to be escaped to avoid executing it as an Expect construct.

For completeness, here is the full script with this change.

#!/usr/bin/expect -f

set timeout 120
spawn ssh user@remoteIP
expect "*?assword:"
send "remote password"
expect "$ "
set name "b"
send "i=''
while [ -d /path/to/remote/directory/$name$i ]; do
let i++
done
mkdir -p /path/to/remote/directory/$name$i\r"
expect "$ "
send -- "exit\r"
expect eof


</details>



huangapple
  • 本文由 发表于 2023年3月1日 16:08:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定