rah*_*huL 3 linux bash perl centos expect
我试图expect在 Perl 脚本中使用系统调用在远程服务器上递归创建目录。相关调用如下:
system("expect -c 'spawn ssh $username\@$ip; expect '*?assword:*' {send \"$password\r\"}; expect '*?*' {send \"mkdir -p ~/$remote_start_folder/$remote_folder_name/$remote_username/$remote_date/\r\"}; expect '*?*' {send \"exit\r\"}; interact;'");
Run Code Online (Sandbox Code Playgroud)
这很好用。但是,如果这是第一次使用 访问远程机器ssh,则会要求(yes/no)确认。我不知道在上面的声明中该在哪里添加。有没有办法将其合并到上面的语句中(使用某种or-ing)?
将匹配添加到与密码匹配yes/no相同的调用中:expect
expect '*yes/no*' {send "yes\r"; exp_continue;} '*?assword:*' {send \"$password\r\"};
Run Code Online (Sandbox Code Playgroud)
这将查找两个匹配项,如果yes/no遇到exp_continue则告诉期望继续查找密码提示。
完整示例:
system( qq{expect -c 'spawn ssh $username\@$ip; expect '*yes/no*' {send "yes\r"; exp_continue;} '*?assword:*' {send "$password\r"}; expect '*?*' {send "mkdir -p ~/$remote_start_folder/$remote_folder_name/$remote_username/$remote_date/\r"}; expect '*?*' {send "exit\r"}; interact;'} );
Run Code Online (Sandbox Code Playgroud)
我也曾经qq避免必须转义所有引用。从带有标志的 shell 运行此命令-d显示期望寻找任一匹配:
Password:
expect: does "...\r\n\r\nPassword: " (spawn_id exp4) match glob pattern
"*yes/no*"? no
"*?assword:*"? yes
Run Code Online (Sandbox Code Playgroud)
有yes/no提示:
expect: does "...continue connecting (yes/no)? " (spawn_id exp4) match glob pattern
"*yes/no*"? yes
...
send: sending "yes\r" to { exp4 }
expect: continuing expect
...
expect: does "...\r\nPassword: " (spawn_id exp4) match glob pattern
"*yes/no*"? no
"*?assword:*"? yes
...
send: sending "password\r" to { exp4 }
Run Code Online (Sandbox Code Playgroud)