实现自动SSH连接

#!/usr/bin/expect  
set timeout 10  
set username [lindex $argv 0]  
set password [lindex $argv 1]  
set hostname [lindex $argv 2]  
#set username ftpuser
#set password ftpuser
#spawn 模拟终端交互
#ssh-copy-id将本机当前用户的ssh登录公钥copy到目标机器上
# "*(yes/no)*" 返回内容包含yes/no,表示添加host到已知host
#password 表示要输入目标机器的密码
#"*please*","*Permission denied*" 错误,没法成功添加ssh公钥到目标机器
#"*All keys were skipped*","*you wanted were added*" 已添加过,则直接跳过。

spawn ssh-copy-id $username@$hostname
expect {
            "*(yes/no)*" {
                send "yes\r"; 
                exp_continue;
            }
            
            "password:" {
                send "$password\r";
                exp_continue;
            }
            "*please*" { exit 5 }
            "*All keys were skipped*" { exit }
            "*you wanted were added*" {exit }
            "*Permission denied*" { exit 6 }
        }
expect eof
#1.保存上面的代码为sh文件,并设置文件具有执行权限
#2.如下使用
./auto_ssh.sh "username" "passpord" "ip"
上一篇:Windows 技术篇-设置电脑启用或禁用开机按Ctrl+Alt+Del解除锁定


下一篇:可扩展的微服务演示 Kubernetes Istio Kafka