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 ~]#
相关推荐
脆皮炸鸡75510 分钟前
进程通信----命名管道
linux·经验分享·笔记·算法·学习方法
Mapleay26 分钟前
FE-BE 动态路机制之 DPCM 与 DAPM 协作
linux
wzl2026121328 分钟前
基于企销宝定时群发接口的私域自动化触达系统设计与实现
运维·自动化
闲适达人41 分钟前
nginx传递url的获取方案
java·服务器·前端
IT大白鼠44 分钟前
Linux故障分析与排查:系统日志、启动故障与文件系统修复
linux·运维·服务器
老詹图解IT1 小时前
统信 UOS 登录界面转圈闪退/卡登录等常见原因及处理
linux·服务器·网络
闫记康1 小时前
Linux学习笔记day1
linux·笔记·学习
轻颂呀1 小时前
进程间关系和守护进程
linux·网络
视***间1 小时前
算力为核,智驱无界——视程空间硬核赋能机器狗与机器人,解锁具身智能产业新未来
大数据·运维·人工智能·机器人·采集卡·机器狗·视程空间
Yang96111 小时前
交通一线运维优选:Smart-S3 多模光时域反射仪
运维