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 ~]#
相关推荐
tedcloud12323 分钟前
taste-skill部署教程:打造个性化AI推荐工作流
服务器·前端·人工智能·系统架构·edge
凡人叶枫1 小时前
Effective C++ 条款28:避免使用 handles 指向对象内部
linux·服务器·开发语言·c++·嵌入式开发
AI帮小忙2 小时前
Debian系linux操作系统里安装OpenClaw
linux·运维·debian
极创信息2 小时前
Linux挖矿病毒深度清理实战教程,从进程隐藏、Rootkit驻留到彻底根除
java·大数据·linux·运维·安全·tomcat·健康医疗
志栋智能3 小时前
超自动化巡检剧本(Playbook):运维经验的数字化封装
运维·自动化
风味蘑菇干3 小时前
WTomcat服务器
java·服务器
ElevenS_it1883 小时前
Nginx日志监控告警实战:access_log解析+5xx突增+慢请求+异常IP自动告警完整方案(Filebeat+Zabbix)
运维·网络·tcp/ip·nginx·zabbix
weixin_307779133 小时前
Python写入Shell文件使用Linux系统的换行符
linux·开发语言·python·自动化
liulilittle3 小时前
Linux Swap 文件配置与持久化(虚拟内存)
linux·运维·服务器
未若君雅裁4 小时前
日志采集与ELK:从本地日志到集中检索分析
运维·elk·jenkins