spawn ssh root@10.18.18.36 set timeout -1 expect "*password:" send "11111\n" expect "#*" { send "cd /data/cpdn\r" send 'sed -i "s/1111/cpdn/g" index.html\r' } expect eof 这个脚本里 执行报错 usage: send [args] string while executing sed命令没执行
时间: 2023-07-05 10:35:44 浏览: 52
这个脚本的问题在于 `send` 命令后面需要跟字符串参数,但是在第6行中,`send` 命令后面的字符串参数 `'sed -i "s/1111/cpdn/g" index.html\r'` 中包含了单引号,导致 `expect` 无法正确解析这个字符串参数,从而导致 `send` 命令执行失败。
为了解决这个问题,你可以将字符串参数改为双引号,或者使用转义字符 `\` 来转义单引号,例如:
```
send "sed -i 's/1111/cpdn/g' index.html\r"
```
或:
```
send 'sed -i '\''s/1111/cpdn/g'\'' index.html\r'
```
这样就可以正确执行 `sed` 命令了。
相关问题
使用这个脚本 #!/usr/bin/expect -f cd /home/111 spawn scp -r * root@10.18.18.1:/data/111 set timeout -1 expect "*password:" send "1111\n" 在执行的时候报错 *: No such file or directory
这个错误是因为在你执行的脚本中,使用了`cd`命令,但是`cd`命令是shell命令,而不是expect命令。因此,你需要将`cd`命令去掉,并使用`set`命令设置当前工作目录。
下面是修改后的脚本:
```bash
#!/usr/bin/expect -f
# 设置工作目录
set workDir /home/111
cd $workDir
# 定义变量
set host "10.18.18.1"
set user "root"
set password "1111"
set localPath "."
set remotePath "/data/111"
# 执行scp
spawn scp -r $localPath $user@$host:$remotePath
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
}
expect "100%"
expect eof
```
在这个脚本中,我们使用`set`命令设置了当前工作目录,然后执行了scp命令,将当前目录下的所有文件和子目录上传到远程主机。我们使用`expect`命令匹配密码输入提示,并发送密码。最后,我们等待文件传输完成,并使用`expect eof`命令关闭连接。
注意,如果你想要上传的是当前目录下的所有文件和子目录,应该将`localPath`设置为`.`,而不是`*`。因为`*`会展开为当前目录下的所有文件和子目录,而不是一个整体的路径。
expect {"*yes/no" { send "yes\r"; exp_continue }"*password:" { send "$passwd\r" }}
这是一个使用Expect脚本自动化交互式命令行操作的例子[^1]。它的作用是在命令行中执行一个需要输入密码的操作,自动填入密码并回车以完成操作。
这个Expect脚本包含两个关键字expect和send。expect是关键字后面的花括号中的参数,用于匹配命令行输出中的特定字符串,send则是在匹配到对应字符串后需要发送的命令。
在这个例子中,expect包含两个参数。第一个是"*yes/no",表示如果命令行输出中包含"yes/no",则执行后面的send命令。第二个是"*password:",表示如果命令行输出中包含"password:",则执行后面的send命令。
在send中,"$passwd\r"表示变量passwd的值加上回车符"\r"。这里的变量passwd需要在脚本中定义并初始化。执行此脚本后,Expect会自动将$passwd的值发送到命令行,完成密码输入。
范例代码如下所示:
```shell
#!/usr/bin/expect
set passwd "my_password"
spawn ssh user@host
expect {
"*yes/no" {
send "yes\r"
exp_continue
}
"*password:" {
send "$passwd\r"
}
}
interact
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)