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 ~]#
相关推荐
3DVisionary15 分钟前
从手动到智能:XTOM-STATION自动化检测中心在复杂曲面零件全尺寸检测中的应用
运维·自动化·自动驾驶·工业自动化·质量控制·自动化3d测量·复杂零件检测
qiudaorendao15 分钟前
作业11.9
linux·服务器·apache
阿豪学编程16 分钟前
环境变量与程序地址空间
linux·运维·windows
秃秃秃秃哇19 分钟前
X5的相机同步方案
linux
岚天start1 小时前
解决方案—K8S集群的日志按天并按照命名空间分类定时同步到日志服务器
服务器·docker·kubernetes·shell·日志备份
佐杰1 小时前
什么是DevOps
运维·devops
CaracalTiger1 小时前
本地部署 Stable Diffusion3.5!cpolar让远程访问很简单!
java·linux·运维·开发语言·python·微信·stable diffusion
ai_xiaogui1 小时前
AIStarter跨平台完工:Win/Mac/Linux一键部署Stable Diffusion
linux·macos·stable diffusion·aistarter·kritaai·跨平台部署
顾安r2 小时前
11.10 脚本算法 五子棋 「重要」
服务器·前端·javascript·游戏·flask
梁萌2 小时前
linux中使用docker安装MySQL
linux·运维·docker·容器·mysql安装