#!/usr/bin/expect
set ipaddr "192.168.40.132"
set name "root"
set passwd "1234"
set timeout 30
spawn ssh-keygen   
expect {
"continue connecting (yes/no)" { send "yes\r";exp_continue }
"Enter file in which to save the key" { send "\r";exp_continue }
"Overwrite (y/n)" { send "y\r";exp_continue }
"Enter passphrase" { send "\r";exp_continue }
"Enter same passphrase" { send "\r";exp_continue }
}


#expect "#" 
set timeout 30
#send "ls /root/.ssh/\r"
set timeout 30
spawn ssh-copy-id $ipaddr
expect {
"continue connecting (yes/no)" { send "yes\r";exp_continue }
"password" { send "$passwd\r" }
}
set timeout 30

expect "#"

spawn  ssh $ipaddr
send "hostname \r"

send "exit\r"
expect eof
#!/usr/bin/expect
set ipaddr "192.168.40.132"
set name "root"
set passwd "1234"
set timeout 30
spawn ssh-keygen   
expect {
"continue connecting (yes/no)" { send "yes\r";exp_continue }
"Enter file in which to save the key" { send "\r";exp_continue }
"Overwrite (y/n)" { send "y\r";exp_continue }
"Enter passphrase" { send "\r";exp_continue }
"Enter same passphrase" { send "\r";exp_continue }
}


#expect "#" 
set timeout 30
#send "ls /root/.ssh/\r"
set timeout 30
spawn ssh-copy-id $ipaddr
expect {
"continue connecting (yes/no)" { send "yes\r";exp_continue }
"password" { send "$passwd\r" }
}
set timeout 30

expect "#"

spawn  ssh $ipaddr
send "hostname \r"

send "exit\r"
expect eof

 

 
[root@host2 ~]# rm -rf .ssh/* 
[root@host2 ~]# expect ssh2.exp 
spawn ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VUjMMH3KhRp7gR49M7WgaWCTVZmOe+kBO2f3OcTpUy8 root@host2
The key's randomart image is:
+---[RSA 2048]----+
|      +o+X+*o    |
|     ..o++#oo.   |
|       .+O.O.    |
|       .*.=      |
|        S= . . . |
|        + * . + .|
|         * o + o.|
|          .   E .|
|               + |
+----[SHA256]-----+
spawn ssh-copy-id 192.168.40.132
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.40.132 (192.168.40.132)' can't be established.
ECDSA key fingerprint is SHA256:j7LBaeHpeaVltw8x0fdaZ8OCvjDXww5cFKofUiy5D6Y.
ECDSA key fingerprint is MD5:c5:55:4e:29:4a:6c:cc:d3:2c:a7:15:34:1f:43:26:f1.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.40.132's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.40.132'"
and check to make sure that only the key(s) you wanted were added.

spawn ssh 192.168.40.132
hostname 
exit
Last login: Sun May 19 08:36:01 2024 from 192.168.40.131
hostname 
exit
[root@host3 ~]# hostname 
host3
[root@host3 ~]# exit
登出