当前位置: 首页 > news >正文

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]#
http://www.dtcms.com/a/348052.html

相关文章:

  • 基于5G NR NTN与DVB-S2X/RCS2的机载卫星通信终端性能分析
  • 低功耗模式DMA数据搬运问题解析
  • 在测试接入抖音小游戏订阅消息推送时遇到的问题
  • bun + vite7 的结合,孕育的 Robot Admin 【靓仔出道】(十八)
  • K8s部署MySQL8.0数据库
  • Transformer实战(13)——从零开始训练GPT-2语言模型
  • 【go语言】字符串函数
  • imx6ull-驱动开发篇39——Linux INPUT 子系统实验
  • 05-ArkUI界面开发
  • Solidity学习笔记
  • ZKmall模块商城的推荐数据体系:从多维度采集到高效存储的实践
  • 用 Bright Data MCP Server 构建实时数据驱动的 AI 情报系统:从市场调研到技术追踪的自动化实战
  • 青少年软件编程(python五级)等级考试试卷-客观题(2024年6月)
  • 数据库原理及应用_数据库基础_第2章关系数据库标准语言SQL_数据的维护
  • Adobe CS6所有系列绿色免安装版,Photoshop 6 Adobe Illustrator CS6 等绿色版
  • Spring创建的方式
  • 使用 Frida 运行时检测 Android 应用的真实权限状态 (App Ops)
  • 第4章栈和队列:顺序队——基本结构
  • Java 基础学习总结(211)—— Apache Commons ValidationUtils:让参数校验从 “体力活“ 变 “优雅事“
  • Vue状态管理工具pinia的使用以及Vue组件通讯
  • 一个byte表示多个bool属性的功能
  • 高并发AI服务部署方案:vLLM、TGI、FastChat性能压测报告
  • CSS 进阶用法
  • Read View是实现MVCC的三大前提之一,那么它是在什么时候建立的
  • frida安装配置及其使用方法
  • 一个奇怪的问题-Python会替代Java吗?技术语言之争的真相-优雅草卓伊凡
  • Java 大视界 -- Java 大数据在智能交通智能公交系统中的乘客流量预测与车辆调度优化
  • 程序里的依赖和中间件的依赖冲突,怎么解决
  • Docling:一个基于AI驱动的免费文档解析工具
  • Python性能优化实战(二):让循环跑得比博尔特还快