问题
获取公钥
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect
}
}
}
[code@samba ~]$ vim
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ ./pub.sh
can't read "1": no such variable
while executing
"set ip "$1""
(file "./pub.sh" line 2)
[code@samba ~]$ ./pub.sh 192.168.235.12
can't read "1": no such variable
while executing
"set ip "$1""
(file "./pub.sh" line 2)
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "lindex args0"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
ssh: Could not resolve hostname 192.168.235.100:"cat: Name or service not known
[code@samba ~]$
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
send_user "id_rsa.pub has been created"
}
send_user "id_rsa.pub is existed"
}
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$
记录
┌─────────────────────────────────────────────────────────────────┐
│ • MobaXterm Personal Edition v23.2 • │
│ (SSH client, X server and network tools) │
│ │
│ ⮞ SSH session to code@192.168.235.10 │
│ • Direct SSH : ✓ │
│ • SSH compression : ✓ │
│ • SSH-browser : ✓ │
│ • X11-forwarding : ✓ (remote display is forwarded th SSH) │
│ │
│ ⮞ For more info, ctrl+click on help or visit our website. │
└─────────────────────────────────────────────────────────────────┘
Last login: Sun Aug 31 14:59:20 2025 from 192.168.235.1
[code@samba ~]$ vim pub.sh
[code@samba ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/code/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/code/.ssh/id_rsa.
Your public key has been saved in /home/code/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZdIv0j5weH79BKpjSw8Xt8kU/KyWP/CRP+vo8NKhly4 code@samba.w
The key's randomart image is:
+---[RSA 2048]----+
| |
| . . |
| . + o |
| * . + |
| S = o + +|
| B . X O |
| *.* & +|
| .oEo=.B.|
| .ooB=.o*|
+----[SHA256]-----+
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect
}
}
}
[code@samba ~]$ vim pub.sh
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"phase):" { send "/r" }
"):" { send "/r" }
}
}
}
[code@samba ~]$ vim pub.sh
[code@samba ~]$ vim pub.sh
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/usr/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ chmod +x pub.sh
[code@samba ~]$ ./pub.sh
-bash: ./pub.sh: /bin/usr/expect: 坏的解释器: 没有那个文件或目录
[code@samba ~]$ vim pub.sh
[code@samba ~]$ which ecpect
/usr/bin/which: no ecpect in (/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/home/code/.local/bin:/home/code/bin)
[code@samba ~]$ which expect
/bin/expect
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "$1"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ ./pub.sh
can't read "1": no such variable
while executing
"set ip "$1""
(file "./pub.sh" line 2)
[code@samba ~]$ ./pub.sh 192.168.235.12
can't read "1": no such variable
while executing
"set ip "$1""
(file "./pub.sh" line 2)
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "lindex args0"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100:cat ~/.ssh/id_rsa.pub
ssh: Could not resolve hostname 192.168.235.100:cat: Name or service not known
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip "lindex args0"
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
ssh: Could not resolve hostname 192.168.235.100:"cat: Name or service not known
[code@samba ~]$ ./pub.sh 192.168.235.45
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
ssh: Could not resolve hostname 192.168.235.100:"cat: Name or service not known
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip lindex $argv 0
set password 123456
set timeout 20
spawn ssh root@192.168.235.100:"cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes/r";exp_continue}
"password:" { send "$pass/r" }
timeout {
echo "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "/r"}
"passphrase):" { send "/r" }
"again:" { send "/r" }
}
}
}
[code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
}
}
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
send_user "id_rsa.pub has been created"
}
send_user "id_rsa.pub is existed"
}
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password: [code@samba ~]$ vim pub.sh
[code@samba ~]$ cat pub.sh
#!/bin/expect
set ip [lindex $argv 0]
set pass 123456
set timeout 20
spawn ssh root@192.168.235.100 "cat ~/.ssh/id_rsa.pub"
expect {
"yes/no" {send "yes\r";exp_continue}
"password:" { send "$pass\r" }
timeout {
send_user "id _rsa.pub not exsits"
expect "#"
send "ssh-keygen"
expect {
"rsa_pub):" { send "\r"}
"passphrase):" { send "\r" }
"again:" { send "\r" }
}
send_user "id_rsa.pub has been created"
}
send_user "id_rsa.pub is existed"
}
expect eof
[code@samba ~]$ ./pub.sh
spawn ssh root@192.168.235.100 cat ~/.ssh/id_rsa.pub
root@192.168.235.100's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
[code@samba ~]$