root@ol8vm2 soft]# cat test2.sh
#!/usr/bin/expect
spawn ssh [email protected]
expect "password: "
send "Root@123\r"
expect "$ "
pwd
ls
expect "$ "
send "exit\r"
[root@ol8vm2 soft]#
[root@ol8vm2 soft]# sh test2.sh
test2.sh: line 3: spawn: command not found
couldn't read file "password: ": no such file or directory
test2.sh: line 5: send: command not found
couldn't read file "$ ": no such file or directory
/soft
script.exp test2.sh
couldn't read file "$ ": no such file or directory
test2.sh: line 10: send: command not found
[root@ol8vm2 soft]#
答案1
您使用了错误的外壳。
您正在运行该命令sh test2.sh
。当您执行此操作时,第一行 ( ) 中定义的 shell#!/bin/...
将被忽略,并使用调用 shell。
直接运行脚本或使用expect而不是sh调用它。