千家信息网

linux expect

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,expect -c " set timeout -1; spawn ssh root@192.168.6.132; expect { *(yes/no)* {send yes\r
千家信息网最后更新 2025年02月03日linux expect
expect -c "   set timeout -1;   spawn ssh root@192.168.6.132;   expect {    *(yes/no)* {send  yes\r ;exp_continue;}    *password* {send  123456\r ;exp_continue;}eof {exit 0 }}";通过expect工具,shell在while循环中完成服务期间的免密登录一键话脚本1.前提要安装expect包2.代码如下:#!/bin/sh#主服务器密码password=123456while read hostname ; do    expect -c "         set timeout -1;         spawn ssh-copy-id ${hostname} ;         expect {         "*password:*" {send --  ${password}\r ;exp_continue;}         "*\(yes/no\)*"  {send -- yes\r ;exp_continue;}         eof {exit 0;}          }"echo "hosts:${hostname} done."#ipconf.cfg 中存放要与主服务器要免密登录的用户的ip 例如: root@1921.68.245.134 每一行的格式与该例相同done < ipconf.cfgifconf.cfg示例:   root@192.168.6.132   root@192.168.6.133



0