当前位置: 首页 > news >正文

shell中的for循环和if判断

一.编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用户输入,例如: test1、test2、test3、.....、 test10

1.创建脚本for1.sh

[root@server ~]# vim for1.sh

2.编写脚本for1.sh

 3.执行脚本for1.sh

[root@server ~]# bash for1.sh
请输入用户账户名前缀 andy
请输入用户的密码 123456

4.测试是否创建成功

二.编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,网段前3段由用户输入。

如: 输入192.168.48 则ping 192.168.48.125 - 192.168.48.135,将可以ping通的主机IP地址写入到 /tmp/host_up.txt文件中,不能ping通的主机IP地址写入到: /tmp/host_down.txt文件中

 1.编写脚本for2.sh

 2.执行脚本for2.sh

 

 3.查看结果

root@server ~]# cat /tmp/host_up.txt 
192.168.111.128
[root@server ~]# cat /tmp/host_down.txt 
192.168.111.125
192.168.111.126
192.168.111.127
192.168.111.129
192.168.111.130
192.168.111.131
192.168.111.132
192.168.111.133
192.168.111.134
192.168.111.135

三.使用for循环实现批量主机root密码的修改
(1)打开多台主机
(2)使用ssh-keygen命令建立密钥对
(3)多台主机间通过ssh-copy-id进行免密登录
(4)编写脚本for3.sh,通过for循环登录主机修改对方root账户密码

1.首先建立密钥对

[root@server ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:9ndQyS81xfxmf1Pp5QBYbYnQtIB6lOpnFYn8zz9hbrs root@server
The key's randomart image is:
+---[RSA 3072]----+
|       . +oBoo +.|
|        * +.+o+.+|
|       + . ..o+.+|
|      o . o  ..+B|
|     . .S. o. o==|
|      ..o.  o.o++|
|       o  . .+..o|
|           . .=  |
|             .E+ |
+----[SHA256]-----+​

2.将产生的密钥发送给目标主机

[root@server ~]# ssh-copy-id root@192.168.111.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.111.130 (192.168.111.130)' can't be established.
ED25519 key fingerprint is SHA256:CqaJjGT+w+4B9rnNqQA2s8IzvInLUFRXO2+cGwsLUKU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.111.130's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'root@192.168.111.130'"
and check to make sure that only the key(s) you wanted were added.

3.创建一个目的主机IP所在文件

[root@server ~]# vim ip.txt

 4.编写脚本

 5.测试

 完成

http://www.lryc.cn/news/61028.html

相关文章:

  • 【Unity入门】16.脚本引用组件
  • 无线蓝牙耳机哪款音质好?目前音质最好的无线蓝牙耳机推荐
  • 【云原生进阶之容器】第六章容器网络6.6.1--Cilium网络方案概述
  • 集中式版本控制工具 —— SVN
  • 【Dom获取属性操作】JavaScript 全栈体系(十)
  • C# 中的多态和虚方法,如何实现多态和使用虚方法?
  • R软件使用一些常见的问题
  • 为什么需要uboot?
  • Qt布局实战:实现高效、美观的GUI应用程序
  • 推荐几款项目管理工具,提高你的团队协作效率
  • SQL101 检索每个顾客的名称和所有的订单号(一)
  • mac压缩文件多了__MACOSX目录问题
  • 1.17 从0开始学习Unity游戏开发--场景切换
  • 【golang学习笔记】——(五)Go格式化统一代码风格
  • CAD转SHP最好的方法 赶快收藏起来吧
  • PyQt PyQt5 Python VTK Gui Actor 选中 高亮显示 actor
  • TCP和UDP通信对比
  • SpringCloud:ElasticSearch之自动补全
  • TOOM解析如何搭建一套适合自己的舆情监测系统?完整的实战指南
  • 技术分享 | OceanBase 手滑误删了数据文件怎么办
  • windows上Git Bash支持常用命令gcc tree zip wget cmake ninja
  • 面试题30天打卡-day10
  • 【python】制作一个简单的界面,有手就行的界面~
  • 基于RK3568的Linux驱动开发—— GPIO知识点(二)
  • item_get-获得aliexpress商品详情API的调用参数说明
  • 【Python_Scrapy学习笔记(三)】Scrapy框架之全局配置文件settings.py详解
  • spark读写时序数据库 TDengine 错误总结
  • Web中间件常见漏洞
  • Python Web 深度学习实用指南:第三部分
  • C#基础学习--预处理指令