linux shell 脚本 和 tcn expec 混合的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux shell 脚本 和 tcn expec 混合的问题相关的知识,希望对你有一定的参考价值。

我想写一个脚本,首先处理一些数据, 然后登录 一台服务器,用expect 输入交互密码,再输入执行 一些操作

用到shell 脚本 和 tcl expect ,我想写成一些以下这样
问题有两个 一、 行 1 和 行 2 怎么共同起作用? 实际效果是只有第一行起作用
二、 行 7 如何 改成 if 条件判断 ?因为有时候登录只在第一次时出现特殊问题(如协议)确认
/---------------------
1 #! /bin/bash
2 #! /usr/bin/expect -f
3 aa=`whoami`
4 echo $aa
5 spawn ssh duqiang.desktop.amazon.com
6 sleep 1
7 expect "(yes/no)?"
8 send "yes\r"
9 expect "*asswor*"
10 send "AAAAAAAA\r"
11 sleep 1
12 send "pwd\r"

一、 行 1 和 行 2 怎么共同起作用? 实际效果是只有第一行起作用
不可能同时起作用,总是第一行的起作用,一个脚本只能是一种语言,要么是 shell, 要么是 expect,要么是其他某种。 你可以把 expect 脚本另外写入一个文件中,然后在 shell 脚本中调用 expect 脚本。

二、 行 7 如何 改成 if 条件判断 ?因为有时候登录只在第一次时出现特殊问题(如协议)确认

改成
expect
"(yes/no)?" send "yes\r"; continue
"password" send "AAAA\r"

.....
即可。 建议你先去学一些 shell 脚本的基础知识,tcl/expect的基础知识再说。追问

能介绍些好书吗? 电子书也行

追答

shell的书、教材很多,中文的资料也很多,自己搜关键字 bash

tcl/expect 可以找一本叫 Exploring Expect 的书,一起就把 tcl 和 expect 学了。不过我只知道英文的,tcl 语言的中文材料可能比较少。自己搜吧

参考技术A 1. 行二没有用也没必要, 当然写着这也不影响, 只是作注释用。
还是建议做成 #! /usr/bin/expect -f, 就是去掉行一。 然后研究一下怎样在expect 中实现你要的bash功能吧。

Linux第六周

1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。

#expect
[root@centos8-2 ~]# vim shh_auto
#!/usr/bin/expect
spawn ssh 10.0.0.150
expect
"yes/no" send "yes\\n";exp_continue
"password" send "abcd123!\\n"

interact
[root@centos8-2 ~]# expect shh_auto
spawn ssh 10.0.0.150
The authenticity of host 10.0.0.150 (10.0.0.150) cant be established.
ECDSA key fingerprint is SHA256:yIWzFvnJmN6nJ/GeG6n0FVUDljrnP8AhepKL8/boZIg.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 10.0.0.150 (ECDSA) to the list of known hosts.
root@10.0.0.150s password:
welcome
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sun Dec 26 12:59:23 2021 from 10.0.0.1
[root@centos8 ~]#
#shell
[root@centos8-2 ~]# vim ssh_auto.sh
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect
"yes/no" send "yes\\n";exp_continue
"password" send "$password\\n"

expect eof
EOF
[root@centos8-2 ~]# sh ssh_auto.sh 10.0.0.150 root abcd123!
spawn ssh root@10.0.0.150
root@10.0.0.150s password:
welcome
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sun Dec 26 13:16:56 2021 from 10.0.0.151
[root@centos8 ~]#

2、生成10个随机数保存于数组中,并找出其最大值和最小值

[root@localhost ~]# vim min_max.sh
#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=$nums[0] && max=$nums[0] && continue
[ $nums[$i] -gt $max ] && max=$nums[$i] && continue
[ $nums[$i] -lt $min ] && min=$nums[$i]
done
echo "all nums are $nums[*]"
echo MAX is $max
echo MIN is $min
[root@localhost ~]# chmod +x min_max.sh
[root@localhost ~]# sh min_max.sh
all nums are 27262 31952 25499 25003 14360 24023 27305 11460 124 22417
MAX is 31952
MIN is 124

3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

#!/bin/bash
# 冒泡排序
declare -a score
for ((k=0;k<10;k++))
do
score[$k]=$RANDOM
done
for ((i=1;i<$#score[*];i++))
do
for ((j=0;j<$#score[*]-$i;j++))
do
if [ $score[j] -gt $score[$(($j+1))] ]
then temp=$score[j]
score[j]=$score[$(($j+1))]
score[$(($j+1))]=$temp
fi
done
done
echo $score[*]
[root@localhost ~]# sh maopao.sh
1467 3789 4877 14246 17184 19287 22824 24106 30496 31815

4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)

[root@centos8-2 ~]# uptime
23:46:02 up 1:17, 1 user, load average: 0.00, 0.00, 0.00
[root@centos8-2 ~]# w
23:50:13 up 1:21, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 22:29 0.00s 0.02s 0.00s w
[root@centos8-2 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1368348 5308 259208 0 0 18 4 51 75 0 0 100 0 0

top命令

第一行解释:

top - 23:36:58 up  1:07,  1 user,  load average: 0.00, 0.00, 0.00

23:36:58:系统当前时间

1:07 :系统开机到现在经过1小时07分了

1 users:当前1用户在线

load average: 0.00, 0.00, 0.00:系统1分钟、5分钟、15分钟的CPU负载信息.

备注:load average后面三个数值的含义是最近1分钟、最近5分钟、最近15分钟系统的负载值。这个值的意义是,单位时间段内CPU活动进程数。如果你的机器为单核,那么只要这几个值均<1,代表系统就没有负载压力,如果你的机器为N核,那么必须是这几个值均<N才可认为系统没有负载压力。

第二行解释:

Tasks: 150 total,   1 running, 149 sleeping,   0 stopped,   0 zombie

150 total:当前有150个任务

1 running:1个任务正在运行

149 sleeping:149个进程处于睡眠状态

0 stopped:停止的进程数

0 zombie:僵死的进程数

第三行解释:

%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.5 id,  0.0 wa,  0.2 hi,  0.2 si,  0.0 st

0.0 us:用户态进程占用CPU时间百分比

0.2 sy:内核占用CPU时间百分比

0.0 ni:renice值为负的任务的用户态进程的CPU时间百分比。nice是优先级的意思

99.5 id:空闲CPU时间百分比

0.0 wa:等待I/O的CPU时间百分比

0.2 hi:CPU硬中断时间百分比

0.0 si:CPU软中断时间百分比

0.0 st:CPU被盗取时间

第四行解释:

MiB Mem :   1790.0 total,   1338.9 free,    194.3 used,    256.9 buff/cache

1790.0 total:物理内存总数

194.3 used: 使用的物理内存

1338.9 free:空闲的物理内存

256.9 buff/cache:用作缓存的内存

第五行解释:

MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   1442.4 avail Mem  

2048.0 total:交换空间的总量

0k used: 使用的交换空间

0k free:空闲的交换空间

1442.4 avail Mem:可用的内存

最后一行:

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND

PID:进程ID

USER:进程的所有者

PR:进程的优先级

NI:nice值

VIRT:占用的虚拟内存

RES:占用的物理内存

SHR:使用的共享内存

S:进行状态 S:休眠 R运行 Z僵尸进程 N nice值为负

%CPU:占用的CPU

%MEM:占用内存

TIME+: 占用CPU的时间的累加值

COMMAND:启动命令

5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

#for循环
net=10.0.0
for i in 1..254;do

ping -c1 -W1 $net.$i &> /dev/null && echo $net.$i is success! || echo $net.$i is fail!
&
done
wait
[root@centos8-2 ~]# sh scan_host.sh
10.0.0.2 is success
10.0.0.1 is success
10.0.0.151 is success
10.0.0.7 is fail
10.0.0.3 is fail
10.0.0.6 is fail
...........
#while循环
net=10.0.0
declare -i i=1
while [ $i -lt 254 ];do
ping -c1 -W1 $net.$i &>/dev/null;
if [ $? -eq 0 ];then
echo $net.$i success!
else
Linux下自动还原MySQL数据库的Shell脚本

linux spawn命令

44shell脚本编程-循环语句

shell脚本编程学习笔记-for循环

备忘录AIX主机下用SHELL脚本编写FTP传某个目录下的文件到LINUX主机

Shell脚本之for语句