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

Linux 96 shell:expect { }

问题

[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" "exp;continue /r"
"password:" "$pass;/r"
}
interact
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" "send exp;continue/r"
"password:" "send "$pass"/r"
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
[root@web ~]#
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send exp;continue/r }
"password:" { send "$pass"/r }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: extra characters after close-quotewhile executing
"send "$pass"/r "invoked from within
"expect {
"yes/no" { send exp;continue/r }
"password:" { send "$pass"/r }
}"(file "./pubok.sh" line 5)
[root@web ~]#
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes";continue/r }
"password:" { send "$pass"/r }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: extra characters after close-quotewhile executing
"send "$pass"/r "invoked from within
"expect {
"yes/no" { send "yes";continue/r }
"password:" { send "$pass"/r }
}"(file "./pubok.sh" line 5)
[root@web ~]#

在这里插入图片描述
在这里插入图片描述

[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
send "#"
send "find ~/.ssh/id_isa.pub"
send "#"
send "$?"
send "#"
send "exit   "
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: [root@web ~]#

[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_isa.pub"
expect "#"
send "$?"
expect "#"
send "exit"
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:16:10 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_isa.pub
$?
^C您在 /var/spool/mail/root 中有邮件
[root@web ~]# ^C
[root@web ~]# e

在这里插入图片描述

[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo "$?"\r"
expect "#"
send "exit\r"
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:52:48 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# extra characters after close-quotewhile executing
"send "echo "$?"\r"
expect "#"
send "exit\r"
"(file "./pubok.sh" line 13)
[root@web ~]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts  knows_hosts
[root@web ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
您在 /var/spool/mail/root 中有邮件
[root@web ~]#

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
公钥
获取
错误1:
#!/usr/bin/expect
2 :
spawn ssh root@$ip {
" "
}
3:
spawn ssh root@ $ip{
“yes/no” [ ]
}
4:spawn ssh root@ $ip{
“yes/no” { “send yes”;continue}
}
spawn ssh root@ KaTeX parse error: Expected '}', got 'EOF' at end of input: …word:" { send "pass\r"}
}

shell

expect

#!/usr/bin/expect
set ip 112.
set pass 12
spawn ssh root@$ip
expect
{
“yes/no” { “send yes\r” send “yes\r” ;exp_continue }
“password:” { “send $pass\r” send " $pass\r" }
}
ecpect "#"
send ls\r”
expect eof

记录

root@192.168.235.20's password:
Remote side unexpectedly closed network connection──────────────────────────────────────────────────────────────────────────────────────────────────────Session stopped- Press <return> to exit tab- Press R to restart session- Press S to save terminal output to file
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 login: Fri Sep  5 21:50:38 2025 from 192.168.235.1
[root@web ~]# ls
1828.txt                head6                      pas4.txt           tail.txt
1837.txt                head7                      pas5.txt           ta.txt
1.txt                   he.txt                     pas6.txt           test1.sh
2-1.java                httpd-2.4.37               pas7.txt           testcfor.sh
2.java                  httpd-2.4.37.tar.bz2       passwd1.txt        testff1.sh
=6                      id.sh                      paste1.txt         testff.sh
7.txt                   initial-setup-ks.cfg       pa.txt             testfor2.sh
alll1                   inotify-tools-3.13.tar.gz  ph1.txt            testfor3.sh
anaconda-ks.cfg         ip.txt                     phonenum.txt       testfor4.sh
app                     jn.sh                      phon.txt           testforf.sh
apr-1.5.2               judgefn.sh                 pho.sh             testfor.sh
apr-1.5.2.tar.bz2       judgepath.sh               php-7.2.17         tmp
apr-util-1.5.4          judgeprimeok.sh            php-7.2.17.tar.xz  u1.txt
apr-util-1.5.4.tar.bz2  judgeprime.sh              ping2.sh           uc.sh
axel-2.4                kj                         ping.sh            uname2.sh
axel-2.4.tar.gz         kk                         primeok.sh         unameok.sh
bb.conf                 lc3.txt                    process1.sh        uname.sh
cc.conf                 lc4.txt                    process.sh         up1.sh
classfi.sh              lc5.txt                    psok.sh            up2.sh
code                    lc6.txt                    pub                upt.sh
cuser1.sh               lc7.txt                    r1oooot.txt        url.sh
dir1                    lc.sh                      readme7            ur.sh
dn.sh                   lc.txt                     readme.txt         us2.sh
expect1.sh              lk.txt                     roooot.txt         us4.sh
expect4.sh              ll                         rooot.txt          us4.txt
expect5c.sh             logs                       root1.txt          usc.sh
expect5.sh              luad.txt                   rsa.sh             user1c.sh
expect6.sh              luck.sh                    s1yyyys.txt        user1.sh
expect.sh               lucky1.txt                 serverOk.sh        user2.txt
fi1.txt                 lucky.txt                  service-s.sh       useradd1.sh
file{11.23}.txt         luvk.txt                   se.sh              useradd.sh
file20.java             mysql-5.6.31               shift.sh           userad.sh
file23.java             mysql-5.6.31.tar.gz        Steam.dll          user.sh
fn.sh                   n1ormallll.txt             stuc.sh            use.txt
fn.txt                  normal12.txt               sum.sh             vsftpd1.sh
for51.sh                normallll.txt              suuu.txt           vsftpd2.sh
for5.sh                 normalll.txt               system12.txt       vsftpdins.sh
ftpok.sh                no.txt                     syyyys.txt         vsftpdok.sh
ftp.sh                  null                       tail               vsftpd.sh
head                    num.sh                     tail4              vstest.sh
head2.txt               ok                         tail5              wordpress
head4                   okl                        tail6              wordpress-4.7.3-zh_CN.tar.gz
head5                   pas3.txt                   tail7              yearok.sh
您在 /var/spool/mail/root 中有新邮件
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" exp;continue /r
"password:" $pass/r
}[root@web ~]# chmod +x pubok.sh
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: invalid command name "123456/r"while executing
"123456/r"invoked from within
"expect {
"yes/no" exp;continue /r
"password:" $pass/r
}"(file "./pubok.sh" line 5)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" exp;continue /r
"password:" $pass;/r
}[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: invalid command name "123456"while executing
"123456"invoked from within
"expect {
"yes/no" exp;continue /r
"password:" $pass;/r
}"(file "./pubok.sh" line 5)
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" "exp;continue /r"
"password:" "$pass;/r"
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Permission denied, please try again.
root@192.168.235.100's password:
Last failed login: Sat Sep  6 20:02:21 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Sat Sep  6 19:29:58 2025 from 192.168.235.1
[root@dns ~]#      vim pubok.sh
[root@dns ~]# vim pubok.sh
[root@dns ~]# cat pubok.sh
cat: pubok.sh: 没有那个文件或目录
[root@dns ~]# exit
登出
Connection to 192.168.235.100 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" "exp;continue /r"
"password:" "$pass;/r"
}
interact
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" "send exp;continue/r"
"password:" "send "$pass"/r"
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send exp;continue/r }
"password:" { send "$pass"/r }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: extra characters after close-quotewhile executing
"send "$pass"/r "invoked from within
"expect {
"yes/no" { send exp;continue/r }
"password:" { send "$pass"/r }
}"(file "./pubok.sh" line 5)
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes";continue/r }
"password:" { send "$pass"/r }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: extra characters after close-quotewhile executing
"send "$pass"/r "invoked from within
"expect {
"yes/no" { send "yes";continue/r }
"password:" { send "$pass"/r }
}"(file "./pubok.sh" line 5)
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass"/r }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: extra characters after close-quotewhile executing
"send "$pass"/r "invoked from within
"expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass"/r }
}"(file "./pubok.sh" line 5)
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 20:02:24 2025 from 192.168.235.20
[root@dns ~]# ^C
[root@dns ~]# ^C
[root@dns ~]# exit
登出
Connection to 192.168.235.100 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat ./ssh
cat: ./ssh: 没有那个文件或目录
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cd ./ssh
-bash: cd: ./ssh: 没有那个文件或目录
[root@web ~]# cd ~/.ssh
[root@web .ssh]# ls
id_rsa  id_rsa.pub  known_hosts  knows_hosts
[root@web .ssh]# find ~/.ssh -n id_rsa.pub
find: 未知的断言“-n”
您在 /var/spool/mail/root 中有邮件
[root@web .ssh]# find ~/.ssh id_rsa.pub
/root/.ssh
/root/.ssh/known_hosts
/root/.ssh/id_rsa
/root/.ssh/id_rsa.pub
/root/.ssh/knows_hosts
id_rsa.pub
[root@web .ssh]# echo $?
0
您在 /var/spool/mail/root 中有邮件
[root@web .ssh]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web .ssh]# exot
bash: exot: 未找到命令...
[root@web .ssh]# exit
登出──────────────────────────────────────────────────────────────────────────────────────────────────────Session stopped- Press <return> to exit tab- Press R to restart session- Press S to save terminal output to file
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 login: Sat Sep  6 19:29:48 2025 from 192.168.235.1
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有新邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 20:57:41 2025 from 192.168.235.20
[root@dns ~]# exit
登出
Connection to 192.168.235.100 closed.
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
send "#"
send "find ~/.ssh/id_isa.pub"
send "#"
send "$?"
interact
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:08:24 2025 from 192.168.235.20
[root@dns ~]# ^C
[root@dns ~]# exit
登出
Connection to 192.168.235.100 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
send "#"
send "find ~/.ssh/id_isa.pub"
send "#"
send "$?"
send "#"
send "exit   "
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password: [root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_isa.pub"
expect "#"
send "$?"
expect "#"
send "exit"
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:16:10 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_isa.pub
$?
^C您在 /var/spool/mail/root 中有邮件
[root@web ~]# ^C
[root@web ~]# vim pub
pub/      pubok.sh
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_isa.pub\r"
expect "#"
send "$?\r"
expect "#"
send "exit\r"
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:34:37 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_isa.pub
find: ‘/root/.ssh/id_isa.pub’: 没有那个文件或目录
[root@dns ~]# $?
bash: 1: 未找到命令...
[root@dns ~]# 您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_isa.pub\r"
expect "#"
send "echo "$?"\r"
expect "#"
send "exit\r"
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:42:54 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_isa.pub
find: ‘/root/.ssh/id_isa.pub’: 没有那个文件或目录
[root@dns ~]# extra characters after close-quotewhile executing
"send "echo "$?"\r"
expect "#"
send "exit\r"
"(file "./pubok.sh" line 13)
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo "$?"\r"
expect "#"
send "exit\r"
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:52:48 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# extra characters after close-quotewhile executing
"send "echo "$?"\r"
expect "#"
send "exit\r"
"(file "./pubok.sh" line 13)
[root@web ~]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts  knows_hosts
[root@web ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ./pub
pub/      pubok.sh
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 21:55:34 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# extra characters after close-quotewhile executing
"send "echo "\$?"\r"
expect "#"
send "exit\r"
"(file "./pubok.sh" line 13)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
send "exit\r"
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 22:09:10 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# echo $?
0
[root@dns ~]# 您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
end "exit\r"
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
end "exit\r"
spawn scp root@$ip:~/.ssh/id_rsa.pub ~/pub
[root@web ~]# mkdir pub1
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls pub1
[root@web ~]# scp root@192.168.235.100:~/.ssh/id_rsa.pub ~/pub
root@192.168.235.100's password:
id_rsa.pub                                                          100%  393   173.9KB/s   00:00
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ls pub
id_rsa.pub
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
end "exit\r"
spawn scp root@$ip:~/.ssh/id_rsa.pub ~/pub1
expect {
"password:" { send "$pass\r" }
}
expect eof
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 22:11:13 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# echo $?
0
[root@dns ~]# invalid command name "end"while executing
"end "exit\r""(file "./pubok.sh" line 15)
您在 /var/spool/mail/root 中有邮件
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
send "exit\r"
spawn scp root@$ip:~/.ssh/id_rsa.pub ~/pub1
expect {
"password:" { send "$pass\r" }
}
expect eof
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 23:12:50 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# echo $?
0
[root@dns ~]# spawn scp root@192.168.235.100:~/.ssh/id_rsa.pub ~/pub1
root@192.168.235.100's password:
~/pub1: No such file or directory
[root@web ~]# ls pub1
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
send "exit\r"
spawn scp root@$ip:~/.ssh/id_rsa.pub ~/pub1
expect {
"password:" { send "$pass\r" }
}
expect eof
[root@web ~]# vim pubok.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat pubok.sh
#!/usr/bin/expect
set ip 192.168.235.100
set pass 123456
set timeout 30
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";continue }
"password:" { send "$pass\r" }
}
expect "#"
send "find ~/.ssh/id_rsa.pub\r"
expect "#"
send "echo \$?\r"
expect "#"
send "exit\r"
spawn scp root@$ip:~/.ssh/id_rsa.pub /root/pub1
expect {
"password:" { send "$pass\r" }
}
expect eof
[root@web ~]# ./pubok.sh
spawn ssh root@192.168.235.100
root@192.168.235.100's password:
Last login: Sat Sep  6 23:16:01 2025 from 192.168.235.20
[root@dns ~]# find ~/.ssh/id_rsa.pub
/root/.ssh/id_rsa.pub
[root@dns ~]# echo $?
0
[root@dns ~]# spawn scp root@192.168.235.100:~/.ssh/id_rsa.pub /root/pub1
root@192.168.235.100's password:
id_rsa.pub                                                          100%  393   400.5KB/s   00:00
您在 /var/spool/mail/root 中有邮件
[root@web ~]#
http://www.xdnf.cn/news/1478953.html

相关文章:

  • 车载通信架构 --- DoIP企业规范中细节有哪些?
  • Huawei C 安全函数库
  • LabVIEW无线预警喷淋系统
  • 问题:指令译码前控制信号还没有产生,那么如何控制译码前指令的动作呢?
  • NV308NV309美光固态闪存NW388NW504
  • Docker部署搜索引擎SearXNG
  • (算法 哈希表)【LeetCode 349】两个数组的交集 思路笔记自留
  • 《云原生故障诊疗指南:从假活到配置漂移的根治方案》
  • Spark 中spark.implicits._ 中的 toDF和DataFrame 类本身的 toDF 方法
  • 【51单片机】【protues仿真】基于51单片机PM2.5空气质量检测系统
  • 云手机在企业办公中的作用
  • [论文阅读] 软件工程 - 需求工程 | 2012-2019年移动应用需求工程研究趋势:需求分析成焦点,数据源却藏着大问题?
  • Linux内核网络子系统框架介绍
  • STM32----W25QXX
  • Long-VLA:释放机器人长范围操作视觉-语言-动作模型的能力
  • 【HEMCO Reference Guide 参考指南第二期】配置文件的结构和语法
  • 贪心算法应用:3D打印支撑结构问题详解
  • 大语言模型预训练数据采集与清洗技术实践:从语料到知识库的全流程优化
  • Qt对话框与文件操作学习
  • Transformer 架构的演进与未来方向(RNN → Self-Attention → Mamba)——李宏毅大模型2025第四讲笔记
  • 如何快速屏蔽红黄区偷偷上互联网呢
  • 为什么服务器有主备BMC?
  • Maven的介绍及基本使用
  • Springboot集成minio实现文件上传与下载
  • Go基础(②Viper)
  • 安装MATLAB205软件记录
  • 【Linux】Linux环境基础开发工具使用
  • clang(clangd)与arm-linux-gcc、ARMGCC、ICCARM(IAR)、C51编译器的兼容性
  • Nginx停止脚本命令
  • 性能优化的边界-不该优化什么