首页 > 系统相关 >Shell阶段07 退出循环指令(示例:分发主机公钥), 函数应用(参数传参)

Shell阶段07 退出循环指令(示例:分发主机公钥), 函数应用(参数传参)

时间:2024-05-31 17:37:06浏览次数:15  
标签:传参 Shell 示例 echo shell01 123 scripts fun root

退出循环的语句

#1.exit    退出循环,退出脚本

#2.break 结束当前循环,或者跳出本地循环,继续执行循环外面的命令

#3.continue 忽略本次循环剩余的代码,直接执行下一次循环

#4.案例
先扫描内网网段的所有主机,存活的主机进行发放本机的公钥
1.本机是否要有公钥,创建密钥对    rm
2.本机是否要有公钥,创建秘钥对    ssh-keygen  交互式进行创建,免交互
-f filename    #指定是要文件保存的路径
-N new_passphrase #指定一个新的密码

#无交互创建密钥对
ssh-keygen -t rsa -f /root/.ssh/id_rsa -N ""

3.批量监测内网主机是否存活        ping
4.如果存活,则发送公钥        确认信息,输入密码    如何进行免交互
-o StrictHostKeyChecking=no #忽略回复yes的交互 (避免第一次交互 公钥检查)
sshpass -p123456    #指定密码为123456,忽略交互

#如果没有sshpass命令,就先安装
yum install sshpass -y
#免交互发送公钥
sshpass -padmin123 ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.51
#可以设定端口号(默认22端口) -p22
sshpass -padmin123 ssh-copy-id -p22 -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.51

[root@shell01 scripts]# vim key.sh
#!/bin/bash
#1.调用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions || echo "函数库文件不存在!"
#2.定义变量
Ip_log=/tmp/ip.log
Port_log=/tmp/port.log
>$Ip_log
>$Port_log
#3.删除旧的密钥对
rm -rf /root/.ssh/
#4.创建新的密钥对
ssh-keygen -t rsa -f /root/.ssh/id_rsa -N "" &>/dev/null
if [ $? -eq 0];then
    echo "密钥对创建成功"
else
    echo "密钥对创建是吧"
    exit
fi
#5.批量探测内网主机
i=1
while [ $i -le 254]
do
    {
        #6.测试主机网络是否可达
        Ip=10.0.0.$i
        ping -c1 -W1 $Ip &>/dev/null
        if [ $? -eq 0 ];then
            action "$Ip地址通畅! " /bin/true
            echo "$Ip地址通畅" >>$Ip_log
        fi
    } &
    let i++
    sleep 0.1
done
wait
echo "IP地址扫描完成..............."
echo "端口扫描开始.........."
while read line
do
    State=$(nmap -p22 $i |grep 22/ | awk '{print $2}')
    if [ $State == 'open' ];then
        action "${line} 的22端口是开放的" /bin/true
        echo "${line}" >> $Port_log
    else
        action "${line} 的22端口没有开放的" /bin/false
    fi
done <$Ip_log
#7.发送公钥
while read line
do
    sshpass -padmin123 ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $line &>/dev/null
    if [ $State == 'open' ];then
        action "${line}的主机公钥发送成功" /bin/true
    else
        action "${line}的主机公钥发送失败" /bin/false
    fi
done <$Port_log
-------------------------------------
#测试,直接连接登录就说明成功了
[root@shell01 scripts]# ssh 10.0.0.6

 

Shell函数应用

什么是函数
函数其实就是一堆命令的集合,用来完成一些特定的代码块

作用:
便于代码的复用

函数的基本概述
#定义函数
#第一种
函数名() {
    命令集合
}

#第二种
function 函数名 {
    命令集合
}

#示例
#定义函数,函数的大括号与命令之间要空格作为分隔符
[root@shell01 scripts]# fun1() { echo "123"; }
#调用函数
[root@shell01 scripts]# fun1
123

[root@shell01 scripts]# function fun2 { echo "456"; }
[root@shell01 scripts]# fun2
456

#函数的内部位置变量
[root@shell01 scripts]# fun2 () { echo "$1"; }
[root@shell01 scripts]# fun2 123
123
[root@shell01 scripts]# fun3() { echo "$1 $2"; }
[root@shell01 scripts]# fun3 123 456
123 456

#定义任意数量位置变量
[root@shell01 scripts]# fun4() { echo "$*"; }
[root@shell01 scripts]# fun4 123
123
[root@shell01 scripts]# fun4 123 456 789 
123 456 789

[root@shell01 scripts]# fun5() { echo "$@"; }    #$@等于$*
[root@shell01 scripts]# fun5 123 456 789 
123 456 789


#如何向函数传递参数,都是使用$1..$9
[root@shell01 scripts]# vim fun-1.sh
#!/bin/bash
fun_1() {
    echo "$1"
}
fun_1 123
[root@shell01 scripts]# sh fun-1.sh 
123
#通过固定变量取值
[root@shell01 scripts]# cat fun-1.sh 
#!/bin/bash
fun_1() {
    echo "$Num"
}
Num=123
fun_1

#注意:脚本的$1不是函数的$1
[root@shell01 scripts]# cat fun-1.sh 
#!/bin/bash
fun_1() {
    echo "$1"
}
Num=$1
fun_1
[root@shell01 scripts]# sh fun-1.sh        #这里显示为空

#注意:脚本中的位置变量不是函数中的位置变量

#编写多个位置变量
[root@shell01 scripts]# vim fun-2.sh
#!/bin/bash
fun_2() {
    echo "$1"    #接受函数传递第一个位置变量
}
fun_2 $1    #接受脚本传递的第一个位置变量,传入函数中第一个参数
fun_2 $2    #接受脚本传递的第二个位置变量,传入函数中第一个参数
fun_2 $3    #接受脚本传递的第三个位置变量,传入函数中第一个参数
[root@shell01 scripts]# sh fun-2.sh 123 456 789
123
456
789

函数场景示例

写个脚本,实现简单的计算器功能,能够实现加减乘除四种计算
[root@shell01 scripts]# vim cal.sh
#!/bin/bash
cal() {
    echo $(( $1 $2 $3 ))
}
if [ $# -ne 3 ];then
    echo "你的传参不是3个!你可以这么写: 1 + 1"
    exit
fi
echo "$1 $2 $3 = $(cal $1 "$2" $3)"
[root@shell01 scripts]# sh cal.sh 1 - 1
1 - 1 = 0
[root@shell01 scripts]# sh cal.sh 1 \* 1
1 * 1 = 1

 

标签:传参,Shell,示例,echo,shell01,123,scripts,fun,root
From: https://www.cnblogs.com/ludingchao/p/18224941

相关文章

  • 构建LangChain应用程序的示例代码:1、AutoGPT
    AutoGPT实现https://github.com/Significant-Gravitas/Auto-GPT,但是使用了LangChain的基础组件(大型语言模型(LLMs)、提示模板(PromptTemplates)、向量存储(VectorStores)、嵌入(Embeddings)、工具(Tools))。设置工具我们将设置一个带有搜索工具、写文件工具和读文件工具的......
  • 20个PowerShell命令
    1.get-command,查找都有哪些指令,get-cmmand简写gcm例如gcm>aaa.txt结果输入到aaa.txt文件中cataaa.txt查看文本文件内容moreaaa.txt2、get-help后面跟命令get-helpcat3、clear-host,简称cls,相当于Linux里面的清屏命令clear,这里也可以用clear4、get-location,简称gl,相当......
  • xshell 自动断开连接的解决方法
    1.问题分析本文Xshell连接自动断开的原因是SSH配置文件的ClientAliveInterval字段设置的超时断开时间小于Xshell的检查断开连接时间。2.SSH配置文件中的字段详解在SSH配置文件/etc/ssh/sshd_config中加入以下配置ClientAliveInterval60#设置超时时间为60秒=>表......
  • 通用程序部署shell脚本
    简介通常一个独立的服务,程序,中间件成后,为便于测试,上线部署都会编写一一个shell用于对程序的启动,重启,查看状态,停止,卸载等动作,以下是一个使用shell脚本来实现的简单demo,直接见代码。功能启动:start重启:restart停止:stop卸载:uninstall查看运行状态:status获取程序......
  • Solidity学习-投票合约示例
    以下的合约有一些复杂,但展示了很多Solidity的语言特性。它实现了一个投票合约。当然,电子投票的主要问题是如何将投票权分配给正确的人员以及如何防止被操纵。我们不会在这里解决所有的问题,但至少我们会展示如何进行委托投票,同时,计票又是自动和完全透明的。我们的想法是......
  • React后台管理(十四)-- 完整示例页面构建教学
    文章目录前言一、组件源码+详细注释说明+技术分析二、效果展示总结前言经过了前面文章的学习,终于到最后一步了,那就是一个管理页面的构建,包括处理列表请求,搜索、重置和展开/收起等功能。结合之前封装的布局、功能相关组件,在本文只需要按需引入,统一了代码标准,减少重......
  • MySQL Shell 调用外部脚本
    使用CLI工具时,打错命令很容易纠正。不过,在MySQlShell中工作时,命令可能会更长、更复杂。以下是运行MySQLDocumentStore的一个例子。db.scores.find("year(date)=2023").fields(['lastName','firstName','round(avg(score),2)asavg','count(score)asnum......
  • Windows PowerShell
    WindowsPowerShell是微软发布的一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用.NETFramework的强大功能。引入了许多非常有用的新概念,从而进一步扩展了在Windows命令提示符和WindowsScriptHost环境中获得的知识和创建的脚本。=========================......
  • 什么是状态机,用简单的java示例说明状态机的概念
    1.什么是状态机状态机(StateMachine)是一种抽象的计算模型,用于描述一个系统在不同状态之间的转换以及触发这些转换的事件。它由状态、事件、动作和转换规则组成。状态代表系统在某个时刻的行为模式;事件是引起状态转换的外部或内部信号;动作是在状态转换时执行的操作;转换规则定义......
  • Shell脚本---条件判断
    1.条件判断语法结构思考:何为真(True)?何为(False)?格式1:test条件表达式格式2:[条件表达式]格式3:[[条件表达式]]支持正则=~特别说明:1)[中括号两边都有空格]2)[[中括号两边都有空格]]3)更多判断,mantest去查看,很多的参数都用来进行条件判断2.条件判断相关参......