==============问题:远程管理获得hosts ip以及推送公钥到hosts========================================

[root@Manager scripts]# cat getip_push_pkey.sh

#!/bin/bash

#get hosts ip push public key to hosts

>ip.txt

ip_prefix='10.0.0.'

#这里可以用read -p读入变量,保证安全性

#password="123456"

read -p  "Enter remote host password: " -s password

ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa

which expect &>/dev/null

if [ $? -ne 0 ];then

yum install expect -y &>/dev/null

fi

for i in {40..60}

do

{ ip=$ip_prefix$i

ping -c1 -w1 $ip &>/dev/null

if [ $? -eq 0 ];then

echo $ip>>ip.txt

/usr/bin/expect <<-EOF

spawn ssh-copy-id $ip

expect {

"yes/no" { send "yes\r"; exp_continue }

"password:" { send "$password\r" }

}

expect eof

EOF

fi }&

done

wait

echo "finish..."

======执行效果==========================================

[root@Manager scripts]# sh getip_push_pkey.sh

Generating public/private rsa key pair.

Created directory '/root/.ssh'.

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:PrGrvdltlSxYzIEY5hFFZskKD/SyXETFup0Xd6S3pOY root@Manager

The key's randomart image is:

+---[RSA 2048]----+

|     ...BX*o     |

|      o=.+= .   .|

|      .++o o . o |

|     . ++   = o.o|

|      o So + +o+.|

|       ..o+ oo+. |

|        +  .oo   |

|       . = ..E   |

|      ..=....    |

+----[SHA256]-----+

spawn ssh-copy-id 10.0.0.40

spawn ssh-copy-id 10.0.0.50

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

The authenticity of host '10.0.0.50 (10.0.0.50)' can't be established.

ECDSA key fingerprint is SHA256:ShTey9zjYvAvkDP2H5cXpZOa7xz21fvjF4EE/KEqiZA.

ECDSA key fingerprint is MD5:fb:70:c1:d4:a8:4f:96:b7:7e:a0:5f:75:2e:04:b6:7f.

Are you sure you want to continue connecting (yes/no)? The authenticity of host '10.0.0.40 (10.0.0.40)' can't be established.

ECDSA key fingerprint is SHA256:ShTey9zjYvAvkDP2H5cXpZOa7xz21fvjF4EE/KEqiZA.

ECDSA key fingerprint is MD5:fb:70:c1:d4:a8:4f:96:b7:7e:a0:5f:75:2e:04:b6:7f.

Are you sure you want to continue connecting (yes/no)? yes

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: 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

/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@10.0.0.50's password: root@10.0.0.40's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.0.0.50'"

and check to make sure that only the key(s) you wanted were added.

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.0.0.40'"

and check to make sure that only the key(s) you wanted were added.

finish...

=====直接连接远程机器执行ifconfig命令

[root@Manager scripts]# ssh root@10.0.0.40 "ifconfig"

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

inet 10.0.0.40  netmask 255.255.255.0  broadcast 10.0.0.255

inet6 fe80::401:d3d8:1c5f:9890  prefixlen 64  scopeid 0x20<link>

inet6 fe80::9a8:50bc:a079:f518  prefixlen 64  scopeid 0x20<link>

ether 00:0c:29:e5:fb:7f  txqueuelen 1000  (Ethernet)

RX packets 1444  bytes 146322 (142.8 KiB)

RX errors 0  dropped 0  overruns 0  frame 0

转载于:https://blog.51cto.com/9447803/2406264

100例shell脚本之八远程管理获得hosts ip以及推送公钥到hosts相关推荐

  1. 远程服务器ip地址可以更改吗,Web远程管理服务器端的IP地址如何进行设置?

    通过Web远程管理可以对服务器进行很多设置.而"远程桌面"是Windows Server 2003较Windows 2000 Server比较明显的改进,用户可以通过Web远程管理 ...

  2. 多主机推送公钥、修改配置、修改密码脚本

    推送公钥: !#/bin/bash if [! -f ~/.ssh/id_rsa ] ;thenssh-keygen -P "" -f ~/.ssh/id_rsa fifor i ...

  3. 100个Shell脚本经典案例解析

    1)编写 hello world 脚本 #!/bin/bash# 编写hello world脚本echo "Hello World!" 2)通过位置变量创建 Linux 系统账户及 ...

  4. shell脚本保姆级教程,附赠100个shell脚本案例!

    无论是系统运维,还是应用运维,均可分为"**纯手工"-> "脚本化"-> "自动化"->"智能化"** ...

  5. 这100个shell脚本案例,你都知道吗?一篇教会你写90%的shell脚本

    shell 是一个应用程序,它连接了用户和 Linux 内核,让用户能够更加高效.安全.低成本地使用 Linux 内核,这就是 Shell 的本质. shell脚本就是由Shell命令组成的执行文件, ...

  6. 【转】干货分享-100个shell脚本

    本文用于记录学习和日常中使用过的shell脚本 [脚本1]打印形状 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash # 等腰三角形 read -p "Please i ...

  7. Shell脚本遍历指定网段的在线ip

    Shell脚本遍历指定网段存活 ip,检测其是否可以ping通- #!/bin/sh# IP 从 1 遍历到 254 for IP in {1..254};do# 指定网段,遍历 ip 列表 ping ...

  8. 【Git】Git 分支管理 ( 解决分支合并冲突 | 推送主版本和分支版本到远程仓库 | 合并分支出现文件冲突 )

    文章目录 一.推送主版本和分支版本到远程仓库 二.合并分支出现文件冲突 一.推送主版本和分支版本到远程仓库 执行 git push origin master 命令 , 将 master 分支推送到远 ...

  9. 远程管理概念及IP KVM的优势解析

    什么是远程管理.其实简单来说,远程管理就是指在网络上由一台电脑(主控端Remote/客户端)远距离去控制另一台电脑(被控端Host/服务器端)的技术.这里的远程不是字面意思的远距离,一般指通过网络控制 ...

最新文章

  1. windoes server 关闭服务端口方法、漏洞补丁解决
  2. WebRTC / Jitsi / 使用 jitsi 官方源安装内网演示环境过程
  3. VTK:图片之ImageOpenClose3D
  4. 通过Spring Integration和RabbitMQ获得高可用性的AMQP支持的消息通道
  5. Window服务的创建与删除
  6. 简单的cpu飙升排查方法
  7. cisco hsrp备份小实验
  8. Steger算法(Line_Gauss)-光条中心线提取(基于Hessian矩阵)
  9. Zynq-7000基于zynq平台裸跑LWIP协议栈的详解(万字长文)
  10. 右浮动的顺序是反过来的,html中第一个是最后显示,而最后一个会在第一个显示。
  11. 计算机windows10怎么找word,win10系统word文件丢失如何找回?win10系统word文件丢失的找回方法...
  12. 双十一,程序员可太难了
  13. 2022大厂真题盘点!190道大数据高频面试题+答案详解
  14. 基于STM32的四足机器人
  15. 电脑怎么连接两个以上的显示器
  16. 1761:神奇的口袋(2)
  17. java生成随机邮箱_Java随机生成姓名、邮箱、手机号码
  18. 9.10 安卓常用工具类之一 权限 ---- PermissionUtil
  19. 零基础学Python 掌握这些让你一个月上手做项目
  20. 关键词过滤(脏字过滤)Trie Tree(Hash)和FastCheck两种过滤方式java版本

热门文章

  1. 使用nodejs安装Vue-cli
  2. iOS单独集成QQ分享功能
  3. 使用Maven管理Java项目
  4. hdu 4876 ZCC loves cards(暴力)
  5. C语言 fread()与fwrite()函数说明与示例
  6. 为什么不应该使用“volatile”类型
  7. 【观点】程序员应知——循序渐进
  8. javax/management/DynamicMBean
  9. 隐去浏览器中当鼠标移到图片上跳出的工具栏
  10. java链式编程_Java 中的链式编程