LINUX 820 shell:shift,expect

bash 复制代码
Session stopped
    - Press <Return> to exit tab
    - Press R to restart session
    - Press S to save terminal output to file
    ┌───────────────────────────────────────────────────────────────────
    │                 • MobaXterm Personal Edition v23.2 •
    │               (SSH client, X server and network tools)
    │
    │ ⮞ SSH session to code@192.168.235.10
    │   • Direct SSH      :  ✓
    │   • SSH compression :  ✓
    │   • SSH-browser     :  ✓
    │   • X11-forwarding  :  ✓  (remote display is forwarded through SSH
    │
    │ ⮞ For more info, ctrl+click on help or visit our website.
    └───────────────────────────────────────────────────────────────────

Last login: Sun Aug 17 15:34:31 2025 from 192.168.235.1
[code@samba ~]$ vim for3.sh
[code@samba ~]$ cat for3.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum
[code@samba ~]$ chmod +x for3.sh
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./
[code@samba ~]$ ./for3.sh 2
sum=2
[code@samba ~]$ ./for3.sh 9
sum=9
[code@samba ~]$
bash 复制代码
[code@samba ~]$ vim shift.sh
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$i
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 2
-bash: ./shift.sh: 权限不够
[code@samba ~]$ chmod +x shift.sh
[code@samba ~]$ ./shift.sh
sum=0
[code@samba ~]$ /shift.sh 4
-bash: /shift.sh: 没有那个文件或目录
[code@samba ~]$ ./shift.sh 4
./shift.sh:行4: let: sum=0+: 语法错误: 期待操作数 (错误符号是 "+")
sum=0
[code@samba ~]$
bash 复制代码
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$1
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 4
sum=4


bash 复制代码
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./for3.sh 4 5 3
sum=12
[code@samba ~]$ ./fot3.sh 5 34 54
-bash: ./fot3.sh: 没有那个文件或目录
[code@samba ~]$ ./for3.sh 231 2 32 3
sum=268
bash 复制代码
[code@samba ~]$ vim for4.sh
[code@samba ~]$ cat for4.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum


:
true
false
[code@samba ~]$ ./for4.sh 324 32 4
sum=360
[code@samba ~]$
bash 复制代码
[code@samba ~]$ spawn ssh root@192.168.235.100
bash: spawn: 未找到命令...
bash 复制代码
[root@web ~]# cat expect.sh
spawn ssh@192.168.235.200
expect {
        "(yes/no)?" { send "yes\r";exp_continue }
        "password:" { send "123456\r" }
}
interact
#./expect.sh
# /expect.sh
#expect -f expect.sh
set ip 192.168.235.9
set pass 123456
set timeout 5
spawn ssh root@$ip
except {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$pass\r"}

}
interact
[root@web ~]# ssh@192.168.235.200
bash: ssh@192.168.235.200: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.200
root@192.168.235.200's password:
Permission denied, please try again.
root@192.168.235.200's password:
Last failed login: Wed Aug 20 23:19:20 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Jul 16 21:25:52 2025 from 192.168.235.20
[root@client ~]# ^C
[root@client ~]# exit
登出
Connection to 192.168.235.200 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.9' (ECDSA) to the list of known hosts.
root@192.168.235.9's password:
Permission denied, please try again.
root@192.168.235.9's password:
Last failed login: Wed Aug 20 23:20:01 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Aug 20 23:11:58 2025 from 192.168.235.1
-bash-4.2# exit
登出
Connection to 192.168.235.9 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# chmod +x expect.sh
[root@web ~]# ./expect.sh
./expect.sh:行1: spawn: 未找到命令
couldn't read file "{": no such file or directory
./expect.sh:行3: (yes/no)?: 没有那个文件或目录
./expect.sh:行3: exp_continue: 未找到命令
./expect.sh:行4: password:: 未找到命令
./expect.sh:行5: 未预期的符号 `}' 附近有语法错误
./expect.sh:行5: `}'
您在 /var/spool/mail/root 中有邮件
[root@web ~]#

记录

bash 复制代码
Session stopped
    - Press <Return> to exit tab
    - Press R to restart session
    - Press S to save terminal output to file
    ┌───────────────────────────────────────────────────────────────────
    │                 • MobaXterm Personal Edition v23.2 •
    │               (SSH client, X server and network tools)
    │
    │ ⮞ SSH session to code@192.168.235.10
    │   • Direct SSH      :  ✓
    │   • SSH compression :  ✓
    │   • SSH-browser     :  ✓
    │   • X11-forwarding  :  ✓  (remote display is forwarded through SSH
    │
    │ ⮞ For more info, ctrl+click on help or visit our website.
    └───────────────────────────────────────────────────────────────────

Last login: Sun Aug 17 15:34:31 2025 from 192.168.235.1
[code@samba ~]$ vim for3.sh
[code@samba ~]$ cat for3.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum
[code@samba ~]$ chmod +x for3.sh
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./for3.sh 2
sum=2
[code@samba ~]$ ./for3.sh 9
sum=9
[code@samba ~]$ ./for3.sh 23
sum=23
[code@samba ~]$ vim shift.sh
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$i
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 2
-bash: ./shift.sh: 权限不够
[code@samba ~]$ chmod +x shift.sh
[code@samba ~]$ ./shift.sh
sum=0
[code@samba ~]$ /shift.sh 4
-bash: /shift.sh: 没有那个文件或目录
[code@samba ~]$ ./shift.sh 4
./shift.sh:行4: let: sum=0+: 语法错误: 期待操作数 (错误符号是 "+")
sum=0
[code@samba ~]$ vim shift.sh
[code@samba ~]$ cat shift.sh
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$1
shift
done
echo sum=$sum

[code@samba ~]$ ./shift.sh 4
sum=4
[code@samba ~]$ ./shift.sh 33 44 32
sum=109
[code@samba ~]$ ./shift.sh 21 3 2 1 32
sum=59
[code@samba ~]$ ./for3.sh
sum=0
[code@samba ~]$ ./for3.sh 4 5 3
sum=12
[code@samba ~]$ ./fot3.sh 5 34 54
-bash: ./fot3.sh: 没有那个文件或目录
[code@samba ~]$ ./for3.sh 231 2 32 3
sum=268
[code@samba ~]$ vim for4.sh
[code@samba ~]$ cat for4.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$si,


:
true
false
[code@samba ~]$ chmod +x for4.sh
[code@samba ~]$ ./for4.sh
sum=,
[code@samba ~]$ ./for4.sh 32 42
sum=,
[code@samba ~]$ vim for4.sh
[code@samba ~]$ cat for4.sh
sum=0
for i
do
let sum=$sum+$i
done
echo sum=$sum


:
true
false
[code@samba ~]$ ./for4.sh 324 32 4
sum=360
[code@samba ~]$ ssh root@192.168.235.100
The authenticity of host '192.168.235.100 (192.168.235.100)' can't be established.
ECDSA key fingerprint is SHA256:PbCAj5CjyP8yLyHMKdEPFFA4planWCr8FpxrrOp3d+I.
ECDSA key fingerprint is MD5:5b:0a:38:81:36:32:b6:38:d1:d8:3b:61:12:39:5a:fc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.100' (ECDSA) to the list of known hosts.
root@192.168.235.100's password:
Last login: Sun Aug 17 08:10:51 2025
[root@backup ~]# exit
登出
Connection to 192.168.235.100 closed.
[code@samba ~]$
bash 复制代码
root@192.168.235.20's password:
▒▒▒ʱ▒▒ܾ▒
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 failed login: Wed Aug 20 22:43:17 CST 2025 from 192.168.235.1 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue Aug 19 21:42:09 2025 from 192.168.235.1
[root@web ~]# spawn ssh root@192.168.235.200
bash: spawn: 未找到命令...
您在 /var/spool/mail/root 中有新邮件
[root@web ~]# systemctl status expect
Unit expect.service could not be found.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# yum install except
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
没有可用软件包 except。
错误:无须任何处理
[root@web ~]# yum install expect
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 expect.x86_64.0.5.45-14.el7_1 将被 安装
--> 正在处理依赖关系 libtcl8.5.so()(64bit),它被软件包 expect-5.45-14.el7_1.x86_64 需要
--> 正在检查事务
---> 软件包 tcl.x86_64.1.8.5.13-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=========================================================================================================================================================
 Package                           架构                              版本                                         源                                大小
=========================================================================================================================================================
正在安装:
 expect                            x86_64                            5.45-14.el7_1                                local                            262 k
为依赖而安装:
 tcl                               x86_64                            1:8.5.13-8.el7                               local                            1.9 M

事务概要
=========================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:2.1 M
安装大小:4.9 M
Is this ok [y/d/N]: y
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                     393 kB/s | 2.1 MB  00:00:05
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:tcl-8.5.13-8.el7.x86_64                                                                                                            1/2
  正在安装    : expect-5.45-14.el7_1.x86_64                                                                                                          2/2
  验证中      : 1:tcl-8.5.13-8.el7.x86_64                                                                                                            1/2
  验证中      : expect-5.45-14.el7_1.x86_64                                                                                                          2/2

已安装:
  expect.x86_64 0:5.45-14.el7_1

作为依赖被安装:
  tcl.x86_64 1:8.5.13-8.el7

完毕!
您在 /var/spool/mail/root 中有邮件
[root@web ~]# spawn ssh root@192.168.235.200
bash: spawn: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# expect {
couldn't read file "{": no such file or directory
[root@web ~]# vim expect.sh
您在 /var/spool/mail/root 中有邮件
[root@web ~]# cat expect.sh
spawn ssh@192.168.235.200
expect {
        "(yes/no)?" { send "yes\r";exp_continue }
        "password:" { send "123456\r" }
}
interact
#./expect.sh
# /expect.sh
#expect -f expect.sh
set ip 192.168.235.9
set pass 123456
set timeout 5
spawn ssh root@$ip
except {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$pass\r"}

}
interact
[root@web ~]# ssh@192.168.235.200
bash: ssh@192.168.235.200: 未找到命令...
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.200
root@192.168.235.200's password:
Permission denied, please try again.
root@192.168.235.200's password:
Last failed login: Wed Aug 20 23:19:20 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Jul 16 21:25:52 2025 from 192.168.235.20
[root@client ~]# ^C
[root@client ~]# exit
登出
Connection to 192.168.235.200 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)?
Host key verification failed.
[root@web ~]# ssh root@192.168.235.9
The authenticity of host '192.168.235.9 (192.168.235.9)' can't be established.
ECDSA key fingerprint is SHA256:l7oqyOLFBOYXfikXWZ6nAea7yct7UK3lp7zJ4/b5EDI.
ECDSA key fingerprint is MD5:7b:8c:da:2e:04:dc:6b:ec:12:6e:44:75:11:30:40:02.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.235.9' (ECDSA) to the list of known hosts.
root@192.168.235.9's password:
Permission denied, please try again.
root@192.168.235.9's password:
Last failed login: Wed Aug 20 23:20:01 CST 2025 from 192.168.235.20 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Aug 20 23:11:58 2025 from 192.168.235.1
-bash-4.2# exit
登出
Connection to 192.168.235.9 closed.
您在 /var/spool/mail/root 中有邮件
[root@web ~]# chmod +x expect.sh
[root@web ~]# ./expect.sh
./expect.sh:行1: spawn: 未找到命令
couldn't read file "{": no such file or directory
./expect.sh:行3: (yes/no)?: 没有那个文件或目录
./expect.sh:行3: exp_continue: 未找到命令
./expect.sh:行4: password:: 未找到命令
./expect.sh:行5: 未预期的符号 `}' 附近有语法错误
./expect.sh:行5: `}'
您在 /var/spool/mail/root 中有邮件
[root@web ~]#
相关推荐
LH_R4 小时前
OneTerm开源堡垒机实战(四):访问授权与安全管控
运维·后端·安全
用户31187945592184 小时前
Kylin Linux 10 安装 glib2-devel-2.62.5-7.ky10.x86_64.rpm 方法(附安装包)
linux
Raymond运维4 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
涛啊涛5 小时前
Centos7非LVM根分区容量不足后扩容,对调硬盘挂载/
linux·磁盘管理
JuiceFS21 小时前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
CYRUS_STUDIO1 天前
用 Frida 控制 Android 线程:kill 命令、挂起与恢复全解析
android·linux·逆向
熊猫李1 天前
rootfs-根文件系统详解
linux
葡萄城技术团队1 天前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
chen9451 天前
mysql 3节点mgr集群部署
运维·后端
LH_R1 天前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全