Linux 96 shell:expect { }

问题

bash 复制代码
[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 ~]#
bash 复制代码
您在 /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-quote
    while 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 ~]#
bash 复制代码
[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-quote
    while 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 ~]#


bash 复制代码
[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 ~]#
bash 复制代码
[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
bash 复制代码
[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-quote
    while 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

记录

bash 复制代码
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-quote
    while 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-quote
    while 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-quote
    while 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-quote
    while 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-quote
    while 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-quote
    while 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 ~]#
相关推荐
路弥行至11 小时前
C语言入门教程 | 第四讲:深入理解数制与码制,掌握基本数据类型的奥秘
服务器·c语言·开发语言·经验分享·笔记·其他·入门教程
艾莉丝努力练剑11 小时前
【Linux指令 (一)】Linux 命令行入门:从零开始理解Linux系统理论核心概念与基础指令
linux·c++·经验分享·ubuntu·centos
hour_go12 小时前
BPEL:企业流程自动化的幕后指挥家
java·运维·自动化
^Lim12 小时前
Docker搭建ESPIDF环境,程序下载
运维·docker·容器
xqlily12 小时前
自动化专业核心课《计算机控制技术》导览---数字时代的控制中枢
运维·自动化
linweidong12 小时前
云服务器磁盘空间管理:binlog导致磁盘快速增长的应对策略与自动化实践
运维·服务器·自动化·binlog·容器化·磁盘管理·运维面经
代码797212 小时前
【无标题】使用 Playwright 实现跨 Chromium、Firefox、WebKit 浏览器自动化操作
运维·前端·深度学习·华为·自动化
失因12 小时前
Nginx 核心功能配置:访问控制、用户认证、HTTPS 与 URL 重写等
运维·nginx·https
COWORKSHOP12 小时前
华为芯片泄密案警示:用Curtain e-locker阻断内部数据泄露
运维·服务器·前端·数据库·安全·华为
DeeplyMind12 小时前
rocr专栏介绍
linux·ai·amdgpu·rocm·rocr·libhsakmt·thunk