Linux 824 shell:expect
问题
[root@web ~]# cat rsa.sh
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# chmod +x rsa.sh
[root@web ~]# ./rsa.sh
./rsa.sh:行4: spawn: 未找到命令
couldn't read file "{": no such file or directory
./rsa.sh:行6: yes/no: 没有那个文件或目录
./rsa.sh:行6: exp_continue: 未找到命令
./rsa.sh:行7: password:: 未找到命令
./rsa.sh:行8: 未预期的符号 `}' 附近有语法错误
./rsa.sh:行8: `}'
您在 /var/spool/mail/root 中有邮件
[root@web ~]#
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "$?"while executing
"$? -eq 0 "invoked from within
"if [ $? -eq 0 ]"(file "./rsa.sh" line 10)
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "timeout"while executing
"timeout {
echo “timeout”
exit 1
}"(file "./rsa.sh" line 10)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: spawn scp root@192.168.235.200:/root/.ssh/id_rsa.pub /pub/
root@192.168.235.200's password: spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sat Aug 23 22:11:39 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pub
cat: pub: 是一个目录
[root@web ~]# cd pub
[root@web ~]# ls pub
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cd pub
[root@web pub]# ls
[root@web pub]# cat rsa.sh
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
expect {-re “ssh-rsa.*” {spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }}expect eof
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.pub
-bash: ./rsa.pub: 没有那个文件或目录
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: missing close-bracewhile executing
"expect {-re “ssh-rsa.*” {spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "..."(file "./rsa.sh" line 15)
您在 /var/spool/mail/root 中有邮件
[root@web ~]#
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
expect {-re "ssh-rsa.*" {spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }}expect eof
}
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 21:46:51 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls /pub
ls: 无法访问/pub: 没有那个文件或目录
[root@web ~]# ls pub
[root@web ~]# cd pub
[root@web pub]# ls
[root@web pub]# cd /pub
-bash: cd: /pub: 没有那个文件或目录
您在 /var/spool/mail/root 中有邮件
[root@web pub]#
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 22:24:01 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): [root@web ~]# cat pub
cat: pub: 是一个目录
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls pub
[root@web ~]# ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 22:29:44 2025 from 192.168.235.20
[root@client ~]# cat ~/.ssh/id_rsa.pub
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
[root@client ~]# cd ~/.ssh/
[root@client .ssh]# ls
authorized_keys known_hosts
[root@client .ssh]#
shell
获取指定ip用户的公钥
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
expect {-re "ssh-rsa.*" {spawn scp root@$ip:/root/.ssh/id_rsa.pub ~/pub/expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }}expect eof
}
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 22:24:01 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): [root@web ~]# cat pub
cat: pub: 是一个目录
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls pub
记录
root@192.168.235.20's password:
▒▒▒ʱ▒▒ܾ▒
root@192.168.235.20's password:┌────────────────────────────────────────────────────────────────────┐│ • MobaXterm 20.0 • ││ (SSH client, X-server and networking tools) ││ ││ ➤ SSH session to root@192.168.235.20 ││ • SSH compression : ✘ ││ • SSH-browser : ✔ ││ • X11-forwarding : ✔ (remote display is forwarded through SSH) ││ • DISPLAY : ✔ (automatically set on remote server) ││ ││ ➤ For more info, ctrl+click on help or visit our website │└────────────────────────────────────────────────────────────────────┘Last failed login: Sun Aug 24 20:15:24 CST 2025 from 192.168.235.1 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Sat Aug 23 17:59:26 2025 from 192.168.235.1
[root@web ~]# ls
1828.txt inotify-tools-3.13.tar.gz readme.txt
1837.txt ip.txt rsa.sh
1.txt judgeprimeok.sh serverOk.sh
2-1.java judgeprime.sh service-s.sh
2.java lc3.txt shift.sh
=6 lc4.txt Steam.dll
alll1 lc5.txt stuc.sh
anaconda-ks.cfg lc6.txt sum.sh
app lc7.txt tail
apr-1.5.2 lc.sh tail4
apr-1.5.2.tar.bz2 lc.txt tail5
apr-util-1.5.4 lk.txt tail6
apr-util-1.5.4.tar.bz2 logs tail7
axel-2.4 luad.txt tail.txt
axel-2.4.tar.gz luck.sh ta.txt
bb.conf lucky1.txt test1.sh
cc.conf lucky.txt testcfor.sh
classfi.sh luvk.txt testff1.sh
code mysql-5.6.31 testff.sh
cuser1.sh mysql-5.6.31.tar.gz testfor2.sh
dir1 no.txt testfor3.sh
dn.sh null testfor4.sh
expect1.sh num.sh testforf.sh
expect4.sh ok testfor.sh
expect5c.sh okl uc.sh
expect5.sh pas3.txt uname2.sh
expect6.sh pas4.txt unameok.sh
expect.sh pas5.txt uname.sh
fi1.txt pas6.txt url.sh
file20.java pas7.txt ur.sh
file23.java passwd1.txt user1c.sh
for51.sh paste1.txt useradd1.sh
for5.sh pa.txt useradd.sh
ftpok.sh ph1.txt userad.sh
ftp.sh phonenum.txt user.sh
head phon.txt vsftpd1.sh
head2.txt pho.sh vsftpd2.sh
head4 php-7.2.17 vsftpdins.sh
head5 php-7.2.17.tar.xz vsftpdok.sh
head6 ping2.sh vsftpd.sh
head7 ping.sh vstest.sh
he.txt primeok.sh wordpress
httpd-2.4.37 process1.sh wordpress-4.7.3-zh_CN.tar.gz
httpd-2.4.37.tar.bz2 process.sh yearok.sh
id.sh psok.sh
initial-setup-ks.cfg readme7
您在 /var/spool/mail/root 中有新邮件
[root@web ~]# cat rsa.sh
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect{"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect{"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
while read ip pass
do{/usr/bin/expect <<-Cspawn ssh root@$ipexpect{"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r"}}}expect "#"send
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls
1828.txt inotify-tools-3.13.tar.gz readme.txt
1837.txt ip.txt rsa.sh
1.txt judgeprimeok.sh serverOk.sh
2-1.java judgeprime.sh service-s.sh
2.java lc3.txt shift.sh
=6 lc4.txt Steam.dll
alll1 lc5.txt stuc.sh
anaconda-ks.cfg lc6.txt sum.sh
app lc7.txt tail
apr-1.5.2 lc.sh tail4
apr-1.5.2.tar.bz2 lc.txt tail5
apr-util-1.5.4 lk.txt tail6
apr-util-1.5.4.tar.bz2 logs tail7
axel-2.4 luad.txt tail.txt
axel-2.4.tar.gz luck.sh ta.txt
bb.conf lucky1.txt test1.sh
cc.conf lucky.txt testcfor.sh
classfi.sh luvk.txt testff1.sh
code mysql-5.6.31 testff.sh
cuser1.sh mysql-5.6.31.tar.gz testfor2.sh
dir1 no.txt testfor3.sh
dn.sh null testfor4.sh
expect1.sh num.sh testforf.sh
expect4.sh ok testfor.sh
expect5c.sh okl uc.sh
expect5.sh pas3.txt uname2.sh
expect6.sh pas4.txt unameok.sh
expect.sh pas5.txt uname.sh
fi1.txt pas6.txt url.sh
file20.java pas7.txt ur.sh
file23.java passwd1.txt user1c.sh
for51.sh paste1.txt useradd1.sh
for5.sh pa.txt useradd.sh
ftpok.sh ph1.txt userad.sh
ftp.sh phonenum.txt user.sh
head phon.txt vsftpd1.sh
head2.txt pho.sh vsftpd2.sh
head4 php-7.2.17 vsftpdins.sh
head5 php-7.2.17.tar.xz vsftpdok.sh
head6 ping2.sh vsftpd.sh
head7 ping.sh vstest.sh
he.txt primeok.sh wordpress
httpd-2.4.37 process1.sh wordpress-4.7.3-zh_CN.tar.gz
httpd-2.4.37.tar.bz2 process.sh yearok.sh
id.sh psok.sh
initial-setup-ks.cfg readme7
您在 /var/spool/mail/root 中有邮件
[root@web ~]# pwd
/root
[root@web ~]# mkdir pub
[root@web ~]# cat rsa.sh
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect{"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect{"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
while read ip pass
do{/usr/bin/expect <<-Cspawn ssh root@$ipexpect{"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r"}}}expect "#"send
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# chmod +x rsa.sh
[root@web ~]# ./rsa.sh
./rsa.sh:行4: spawn: 未找到命令
couldn't read file "{": no such file or directory
./rsa.sh:行6: yes/no: 没有那个文件或目录
./rsa.sh:行6: exp_continue: 未找到命令
./rsa.sh:行7: password:: 未找到命令
./rsa.sh:行8: 未预期的符号 `}' 附近有语法错误
./rsa.sh:行8: `}'
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "$?"while executing
"$? -eq 0 "invoked from within
"if [ $? -eq 0 ]"(file "./rsa.sh" line 10)
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
timeout {
echo “timeout”
exit 1
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "timeout"while executing
"timeout {
echo “timeout”
exit 1
}"(file "./rsa.sh" line 10)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vin rsa.sh
bash: vin: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
timeout {
put “timeout”
exit 1
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "timeout"while executing
"timeout {
put “timeout”
exit 1
}"(file "./rsa.sh" line 10)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}
timeout {put “timeout”exit 1
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "timeout"while executing
"timeout {put “timeout”exit 1
}"(file "./rsa.sh" line 10)
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
if [ $? -eq 0 ];then
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
else
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
fi
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: invalid command name "$?"while executing
"$? -eq 0 "invoked from within
"if [ $? -eq 0 ]"(file "./rsa.sh" line 15)
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: spawn scp root@192.168.235.200:/root/.ssh/id_rsa.pub /pub/
root@192.168.235.200's password: spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sat Aug 23 22:11:39 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pub
cat: pub: 是一个目录
[root@web ~]# cd pub
[root@web pub]# cd ~
[root@web ~]# ls
1828.txt inotify-tools-3.13.tar.gz readme7
1837.txt ip.txt readme.txt
1.txt judgeprimeok.sh rsa.sh
2-1.java judgeprime.sh serverOk.sh
2.java lc3.txt service-s.sh
=6 lc4.txt shift.sh
alll1 lc5.txt Steam.dll
anaconda-ks.cfg lc6.txt stuc.sh
app lc7.txt sum.sh
apr-1.5.2 lc.sh tail
apr-1.5.2.tar.bz2 lc.txt tail4
apr-util-1.5.4 lk.txt tail5
apr-util-1.5.4.tar.bz2 logs tail6
axel-2.4 luad.txt tail7
axel-2.4.tar.gz luck.sh tail.txt
bb.conf lucky1.txt ta.txt
cc.conf lucky.txt test1.sh
classfi.sh luvk.txt testcfor.sh
code mysql-5.6.31 testff1.sh
cuser1.sh mysql-5.6.31.tar.gz testff.sh
dir1 no.txt testfor2.sh
dn.sh null testfor3.sh
expect1.sh num.sh testfor4.sh
expect4.sh ok testforf.sh
expect5c.sh okl testfor.sh
expect5.sh pas3.txt uc.sh
expect6.sh pas4.txt uname2.sh
expect.sh pas5.txt unameok.sh
fi1.txt pas6.txt uname.sh
file20.java pas7.txt url.sh
file23.java passwd1.txt ur.sh
for51.sh paste1.txt user1c.sh
for5.sh pa.txt useradd1.sh
ftpok.sh ph1.txt useradd.sh
ftp.sh phonenum.txt userad.sh
head phon.txt user.sh
head2.txt pho.sh vsftpd1.sh
head4 php-7.2.17 vsftpd2.sh
head5 php-7.2.17.tar.xz vsftpdins.sh
head6 ping2.sh vsftpdok.sh
head7 ping.sh vsftpd.sh
he.txt primeok.sh vstest.sh
httpd-2.4.37 process1.sh wordpress
httpd-2.4.37.tar.bz2 process.sh wordpress-4.7.3-zh_CN.tar.gz
id.sh psok.sh yearok.sh
initial-setup-ks.cfg pub
[root@web ~]# ls pub
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cd pub
[root@web pub]# ls
[root@web pub]# cat rsa.sh
cat: rsa.sh: 没有那个文件或目录
[root@web pub]# cd ~
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/
expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
expect {-re “ssh-rsa.*” {spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "$pass\r" }}expect eof
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.pub
-bash: ./rsa.pub: 没有那个文件或目录
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password: missing close-bracewhile executing
"expect {-re “ssh-rsa.*” {spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/expect {"yes/no" { send "yes\r";exp_continue }"password:"{ send "..."(file "./rsa.sh" line 15)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
expect {-re "ssh-rsa.*" {spawn scp root@$ip:/root/.ssh/id_rsa.pub /pub/expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }}expect eof
}
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 21:46:51 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls /pub
ls: 无法访问/pub: 没有那个文件或目录
[root@web ~]# ls pub
[root@web ~]# cd pub
[root@web pub]# ls
[root@web pub]# cd /pub
-bash: cd: /pub: 没有那个文件或目录
您在 /var/spool/mail/root 中有邮件
[root@web pub]# cd ./pub
-bash: cd: ./pub: 没有那个文件或目录
您在 /var/spool/mail/root 中有邮件
[root@web pub]# cd ~/pub
[root@web pub]# vim rsa.sh
[root@web pub]# cd /
[root@web /]# ls
1.java app data id_rsa.pub oo .pcapng samba tmp u3 var
2041 app1 date java opt sbin u01 u7 vsftpd2.sh
2.java bin dev lib proc shell01 u03 uc1 www
35.txt boot e1 lib64 rhe16.iso shop u06 ue6
3.java boot.bak etc media rhome srv u07 uname.sh
4.java cc.conf file3.java mmt root sys u1 usr
5.java code home mnt run test3.sh u2 ut1
[root@web /]# cd ~
[root@web ~]# ls
1828.txt inotify-tools-3.13.tar.gz readme7
1837.txt ip.txt readme.txt
1.txt judgeprimeok.sh rsa.sh
2-1.java judgeprime.sh serverOk.sh
2.java lc3.txt service-s.sh
=6 lc4.txt shift.sh
alll1 lc5.txt Steam.dll
anaconda-ks.cfg lc6.txt stuc.sh
app lc7.txt sum.sh
apr-1.5.2 lc.sh tail
apr-1.5.2.tar.bz2 lc.txt tail4
apr-util-1.5.4 lk.txt tail5
apr-util-1.5.4.tar.bz2 logs tail6
axel-2.4 luad.txt tail7
axel-2.4.tar.gz luck.sh tail.txt
bb.conf lucky1.txt ta.txt
cc.conf lucky.txt test1.sh
classfi.sh luvk.txt testcfor.sh
code mysql-5.6.31 testff1.sh
cuser1.sh mysql-5.6.31.tar.gz testff.sh
dir1 no.txt testfor2.sh
dn.sh null testfor3.sh
expect1.sh num.sh testfor4.sh
expect4.sh ok testforf.sh
expect5c.sh okl testfor.sh
expect5.sh pas3.txt uc.sh
expect6.sh pas4.txt uname2.sh
expect.sh pas5.txt unameok.sh
fi1.txt pas6.txt uname.sh
file20.java pas7.txt url.sh
file23.java passwd1.txt ur.sh
for51.sh paste1.txt user1c.sh
for5.sh pa.txt useradd1.sh
ftpok.sh ph1.txt useradd.sh
ftp.sh phonenum.txt userad.sh
head phon.txt user.sh
head2.txt pho.sh vsftpd1.sh
head4 php-7.2.17 vsftpd2.sh
head5 php-7.2.17.tar.xz vsftpdins.sh
head6 ping2.sh vsftpdok.sh
head7 ping.sh vsftpd.sh
he.txt primeok.sh vstest.sh
httpd-2.4.37 process1.sh wordpress
httpd-2.4.37.tar.bz2 process.sh wordpress-4.7.3-zh_CN.tar.gz
id.sh psok.sh yearok.sh
initial-setup-ks.cfg pub
[root@web ~]# vim rsa.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat rsa.sh
#!/usr/bin/expect
set ip 192.168.235.200
set pass 123456
set timeout 5
spawn ssh root@$ip "cat ~/.ssh/id_rsa.pub"
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }timeout {put “timeout”exit 1
}
}
expect {-re "ssh-rsa.*" {spawn scp root@$ip:/root/.ssh/id_rsa.pub ~/pub/expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }}expect eof
}
}
spawn ssh root@$ip
expect {"yes/no" { send "yes\r";exp_continue }"password:" { send "$pass\r" }
}expect "#"
send "mkdir .ssh\r"
send "chmod 700 .ssh\r"
send "cd .ssh\r"
send "ssh-keygen\r"
send "exit\r"
expect eof
[root@web ~]# ./rsa.sh
spawn ssh root@192.168.235.200 cat ~/.ssh/id_rsa.pub
root@192.168.235.200's password:
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
spawn ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 22:24:01 2025 from 192.168.235.20
[root@client ~]# mkdir .ssh
mkdir: 无法创建目录".ssh": 文件已存在
[root@client ~]# chmod 700 .ssh
[root@client ~]# cd .ssh
[root@client .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): [root@web ~]# cat pub
cat: pub: 是一个目录
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls pub
[root@web ~]# ssh root@192.168.235.200
root@192.168.235.200's password:
Last login: Sun Aug 24 22:29:44 2025 from 192.168.235.20
[root@client ~]# cat ~/.ssh/id_rsa.pub
cat: /root/.ssh/id_rsa.pub: 没有那个文件或目录
[root@client ~]# cd ~/.ssh/
[root@client .ssh]# ls
authorized_keys known_hosts
[root@client .ssh]#