- 说明
- 1、2:提取IP和mac
- 单台【ifconfig命令】
- 记录
- scp拷贝
- expect脚本ip密码准备
- 代码汇总【不可行】
- 执行效果
- 代码汇总【可行】
- sh代码【版本1,有局限,了解为主】
- 执行
- sh代码【版本2,可行且无局限】
- expect代码【版本1,对应上面sh代码的版本1】
- 执行
- expect代码【版本2,对应上面sh代码的版本2】
- scp代码在expect中使用说明
- 最终版代码
- 执行
- 3、汇总信息【遍历目录文件】
- 获取目录下所有文件名【while】
- 汇总信息
- 最终代码完整汇总
- sh代码【被调用的脚本】
- expect代码【执行的是这个】
- 汇总代码
- 现成脚本需要改的地方说明
- 没有ifconfig命令解决
- excel匹配没有成功的数据
-
今天又有一个新需求
提取所有物理机的IP和其对应的mac地址。 -
实现过程【你也可以先思考一下我下面的每一步咋实现的,然后再看我的代码,要学会独立思考,才能更快成长。】
- 1、分别提取每台的ip和mac并记录到文件中【也可以手动复制记录,但我这几百台,不可能每一台都手动复制的】
- 2、汇总每一台文件中记录的IP和mac到一台主机上面。
- 3、将汇总的文件分别提取并汇总到一个文件中。
- 4、导出该文件中的内容即可。
- 单台提取很简单【真实环境ip不变展示。下面所有IP信息有修改】
[root@node-2 ccx]# sh test.sh
1.2.3.4 : f6:a4:b4:aa:ac:49
[root@node-2 ccx]#
[root@node-2 ccx]# cat test.sh
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`"
[root@node-2 ccx]#
记录
- 提取到以后我们还需要思考一个问题,这是每一台的值,后面我们需要把这个代码放到expect脚本中遍历执行获取所有物理机。
同时还需要考虑一个问题,每一台独立提取的值怎么汇总?
我这想到的就是将值放到一个文件中,然后用scp拷贝到某一台上,后面用cat每一个文件用>>追加的方式放到一个文件夹中。
我这打算全部将每一台的结果放到我node-2主机上的下面目录中,先创建出来
[root@node-2 ~]# cd ccx/ [root@node-2 ccx]# mkdir mak [root@node-2 ccx]# cd mak [root@node-2 mak]# pwd /root/ccx/mak [root@node-2 mak]#scp拷贝
- 所以我每一台上需要做的就是【这是测试代码的,代码可行就放到脚本中】
- 将ip和mac放到文件中,文件不能重名【否则汇总的时候会冲突】,可以用ip来命名文件名【根据我下面的测试,我这是用的IP,但我最后改成用主机名了】
- scp将文件拷贝到上面node-2主机上
- 删除保存ipmak信息文件
这台主机竟然有2个一样的ip,不知道之前配置ip的咋搞的,真的绝。
[root@node-1 ccx]# ifconfig | grep 1.2.3.
inet 1.2.3.3 netmask 255.255.255.0 broadcast 1.2.3.255
inet 1.2.3.3 netmask 255.255.255.0 broadcast 0.0.0.0
[root@node-1 ccx]#
获取信息并放到文件中,没问题
[root@node-1 ccx]# cat mak.sh
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `ifconfig | egrep 1.2.3 | awk '{print $2}'`
[root@node-1 ccx]#
[root@node-1 ccx]#
[root@node-1 ccx]#
[root@node-1 ccx]# sh mak.sh
[root@node-1 ccx]#
[root@node-1 ccx]# cat '1.2.3.3
1.2.3.3'
1.2.3.3
1.2.3.3 : 02:2a:e8:bc:05:4a
8e:99:1d:d1:d5:67
[root@node-1 ccx]#
[root@node-1 ccx]#
# scp拷贝试试
[root@node-1 ccx]# scp '1.2.3.3
1.2.3.3' 5.6.7.134:/root/ccx/mak
Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts.
Authorized users only. All activity may be monitored and reported
1.2.3.3^J1.2.3.3 100% 66 0.1KB/s 00:00
[root@node-1 ccx]#
#去node-2这个主机上查看文件,没问题
[root@node-2 ~]# cd ccx/mak
[root@node-2 mak]# ls
1.2.3.3^J1.2.3.3
[root@node-2 mak]# cat 1.2.3.3\^J1.2.3.3
1.2.3.3
1.2.3.3 : 02:2a:e8:bc:05:4a
8e:99:1d:d1:d5:67
[root@node-2 mak]#
expect脚本ip密码准备
建议分开存放,如下,我就分了好几个文件放的。
格式前面ip后面密码,一行一个。
[root@node-2 ccx]# ls | grep iplist iplist3-4.txt iplist5-23.txt iplist61-82.txt iplist83-104.txt [root@node-2 ccx]# cat iplist3-4.txt 1.2.3.3 Dm21*8&qT!9(0) 1.2.3.4 Dm21*8&qT!9(0) [root@node-2 ccx]#代码汇总【不可行】
- 那么开始代码汇总吧
root@node-2 ccx]# cat mak.sh #!/bin/bash cat $1|while read line do a=($line) #/usr/bin/expect<执行效果`ifconfig | egrep 1.2.3 | awk '{print $2}'`r"} expect "*]#" {send "sleep 1r"} expect "*]#" {send "scp `ifconfig | egrep 1.2.3 | awk '{print $2}'` 5.6.7.134:/root/ccx/makr"} expect { "*assword" {send "${a[1]}r";} "yes/no" {send "yesr"; exp_continue} } expect "*]#" {send "sleep 1r"} expect "*]#" {send "rm `ifconfig | egrep 1.2.3 | awk '{print $2}'`r"} expect "*]#" {send "sleep 1r"} expect "*]#" {send "echo ________________________r"} expect "*]#" {send "exitr"} expect eof EOF done [root@node-2 ccx]#
- 用2个ip执行试试效果
下面有个sleep 差个1 所以报错了,问题不大,更新脚本代码就好了。
还有问题,这特么的再每一台上面获取到的ip都是我执行的主机ip,离了个大谱。
我不知道哪里处问题了,反正这种方式不行了
[root@node-2 ccx]# sh mak.sh iplist3-4.txt spawn ssh root@1.2.3.3 Warning: Permanently added '1.2.3.3' (ECDSA) to the list of known hosts. Last login: Wed May 11 11:11:09 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@node-1 ~]# echo -e 1.2.3.4 : f6:a4:b4:aa:ac:49 > 1.2.3.4 [root@node-1 ~]# sleep 1 [root@node-1 ~]# scp 1.2.3.4 5.6.7.134:/root/ccx/mak Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported 1.2.3.4 100% 34 0.0KB/s 00:00 [root@node-1 ~]# sleep sleep: missing operand Try 'sleep --help' for more information. [root@node-1 ~]# rm 1.2.3.4 [root@node-1 ~]# sleep 1 [root@node-1 ~]# echo ________________________ ________________________ [root@node-1 ~]# exit logout Connection to 1.2.3.3 closed. spawn ssh root@1.2.3.4 Warning: Permanently added '1.2.3.4' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported Last login: Wed May 11 11:11:19 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@node-2 ~]# echo -e 1.2.3.4 : f6:a4:b4:aa:ac:49 > 1.2.3.4 [root@node-2 ~]# sleep 1 [root@node-2 ~]# scp 1.2.3.4 5.6.7.134:/root/ccx/mak Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported 1.2.3.4 100% 34 0.0KB/s 00:00 [root@node-2 ~]# sleep sleep: missing operand Try 'sleep --help' for more information. [root@node-2 ~]# rm 1.2.3.4 [root@node-2 ~]# sleep 1 [root@node-2 ~]# echo ________________________ ________________________ [root@node-2 ~]# exit logout Connection to 1.2.3.4 closed. [root@node-2 ccx]#代码汇总【可行】
- 上面我们已经知道了需要执行的几条命令,我们用expect的方式不行,获取的ip是执行机的ip,不知道为啥,那么我们就用普通的方式来执行,用shell的方式来执行,这样就没问题了
但又遇到一个新问题,前面说过,我们写入信息名称是用ip,可我下面这个主机,有2个一样的ip,那就报错了。如下。
[root@node-1 ccx]# sh mak.sh
1.2.3.3
1.2.3.3
Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts.
Authorized users only. All activity may be monitored and reported
1.2.3.3: No such file or directory
1.2.3.3: No such file or directory
[root@node-1 ccx]#
[root@node-1 ccx]# cat mak.sh
#!/bin/bash
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `ifconfig | egrep 1.2.3 | awk '{print $2}'`
scp `ifconfig | egrep 1.2.3 | awk '{print $2}'` 5.6.7.134:/root/ccx/mak
[root@node-1 ccx]#
- 所以,名称我们不能用ip了,改成主机名吧,这样就正常了
[root@node-1 ccx]# cat mak.sh
#!/bin/bash
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `hostname`
scp `hostname` 5.6.7.134:/root/ccx/mak
[root@node-1 ccx]#
[root@node-1 ccx]# sh mak.sh
Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts.
Authorized users only. All activity may be monitored and reported
node-1.domain.tld 100% 66 0.1KB/s 00:00
[root@node-1 ccx]#
sh代码【版本1,有局限,了解为主】
- 所以每一台上面需要执行的最终代码如下
这个代码放到某一台上面就好,后面执行的时候scp把代码拷贝过去执行。
[root@node-1 ccx]# cat mak.sh
#!/bin/bash
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `hostname`
scp `hostname` 5.6.7.134:/root/ccx/mak
rm `hostname`
[root@node-1 ccx]#
- 上面代码我放到存放所有代码的主机node-2上,并命名为mac-sh.sh
[root@node-2 ccx]# cat mac-sh.sh
#!/bin/bash
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `hostname`
scp `hostname` 5.6.7.134:/root/ccx/mak
rm `hostname`
[root@node-2 ccx]#
执行
- 上面代码有一个条件,就是如果你的虚拟机不是互相免密的话,文件就拷不成功,会弹出scp拷贝输入密码界面,同时也会卡在这个界面。
我下面是双方免密的主机,所以是正常的。
[root@node-1 ccx]# sh mak.sh Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported node-1.domain.tld 100% 66 0.1KB/s 00:00 [root@node-1 ccx]# [root@node-1 ccx]# cat node-1.domain.tld 1.2.3.3 1.2.3.3 : 02:2a:e8:bc:05:4a 8e:99:1d:d1:d5:67 [root@node-1 ccx]#
- 卡住的界面我就不测试了,不然代码还得改回去,麻烦很。
反正就是会卡在scp输入密码的界面,这个应该没啥可说的。
我其实也没做啥改动,就是把删除改到另外一个脚本中了而已。
注:必须是以scp结尾。
[root@node-2 ccx]# cat mac-sh.sh
#!/bin/bash
echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `hostname`
scp `hostname` 1.2.3.4:/root/ccx/mak
[root@node-2 ccx]#
expect代码【版本1,对应上面sh代码的版本1】
- 上面代码是需要执行的代码嘛
那我这还需要写一个交互脚本运行这些脚本噻。
[root@node-2 ccx]# cat mak.sh #!/bin/bash cat $1|while read line do a=($line) #/usr/bin/expect<执行
- 执行前我先删了之前存在的文件
[root@node-2 ccx]# cd mak [root@node-2 mak]# ls 1.2.3.4 node-1.domain.tld [root@node-2 mak]# [root@node-2 mak]# [root@node-2 mak]# [root@node-2 mak]# rm -rf * [root@node-2 mak]# ls [root@node-2 mak]#
- 现在执行2个试试
[root@node-2 ccx]# sh mak.sh iplist3-4.txt spawn ssh root@1.2.3.3 Warning: Permanently added '1.2.3.3' (ECDSA) to the list of known hosts. Last login: Wed May 11 14:40:28 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@node-1 ~]# scp 5.6.7.134:/root/ccx/mac-sh.sh /root/ Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported mac-sh.sh 100% 212 0.2KB/s 00:00 [root@node-1 ~]# sh /root/mac-sh.sh Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported node-1.domain.tld 100% 66 0.1KB/s 00:00 [root@node-1 ~]# sleep 1 [root@node-1 ~]# rm /root/mac-sh.sh [root@node-1 ~]# echo ________________________ ________________________ [root@node-1 ~]# exit logout Connection to 1.2.3.3 closed. spawn ssh root@1.2.3.4 Warning: Permanently added '1.2.3.4' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported Last login: Wed May 11 14:40:49 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@node-2 ~]# scp 5.6.7.134:/root/ccx/mac-sh.sh /root/ Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported mac-sh.sh 100% 212 0.2KB/s 00:00 [root@node-2 ~]# sh /root/mac-sh.sh Warning: Permanently added '5.6.7.134' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported node-2.domain.tld 100% 34 0.0KB/s 00:00 [root@node-2 ~]# sleep 1 [root@node-2 ~]# rm /root/mac-sh.sh [root@node-2 ~]# echo ________________________ ________________________ [root@node-2 ~]# exit logout Connection to 1.2.3.4 closed. [root@node-2 ccx]#
- 现在回到node-2这个主机看看ip信息是否都有
都有,可以了[root@node-2 ccx]# cd mak [root@node-2 mak]# ls node-1.domain.tld node-2.domain.tld [root@node-2 mak]#expect代码【版本2,对应上面sh代码的版本2】 scp代码在expect中使用说明
- 我前面不是说过嘛,如果直接把scp放到expect中执行的话, scp获取到是值是我执行主机的信息。
- 我去网上找了一下,原来scp要用spawn来执行。。。。。我下面复制的网上内容。
- -c 表示可以在命令行下执行except脚本;
- spawn 命令激活一个unix程序来交互,就是在之后要执行的命令;
- expect “aaa” 表示程序在等待这个aaa的字符串;
- send 向程序发送字符串,expect和send经常是成对出现的,比如当expect“aaa”的时候,send“bbb”。
- scp执行脚本,如下
#! /bin/sh expect -c " spawn scp -r /home/tseg/hello $name@10.103.240.33:/home/$name/ expect { "*assword" {set timeout 300; send "$passr"; exp_continue;} "yes/no" {send "yesr";} } expect eof"解释:
第二行: -c 表示可以不用与控制台交互;
第三行:spawn激活一个scp的unix程序;
第五行:expect期待含有“assword”的字符串,设置连接时间最大为300毫秒,如果出现这个字符串,就send 变量pass代表的密码字符串, exp_continue表示执行下面的匹配;
第六航:expect期待含有“assword”的字符串,设置连接时间最大为300毫秒,如果出现这个字符串,就send 变量pass代表的密码字符串;
第八行:表示结束。
- 我根据这个其实做了好些尝试,发现也有局限,如果按上面的方式,首先必须要当前主机上有expect命令,可是这个命令我并不会再每一台上面都安装,所以我肯定是不会用的。
我们可以换一种思路嘛,把ssh直接换成scp不就好了,这样的话只需要我执行机有expect命令就好了。
而且实现方法也很简单,就是再当前主机scp拷贝别的主机文件,这个应该会吧?
而且批量的话, 我们一样嘛,遍历文件ip嘛,而且密码也可以遍历文件,这样更方便呢。
下面要实现的话得单独跑一次脚本,我这肯定不用的,只是告诉你这样可以实现,我后面有更牛逼的使用方法。[root@node-2 ccx]# cat scp.sh #!/bin/bash cat $1|while read line do a=($line) #/usr/bin/expect<最终版代码 [root@node-2 ccx]# cat mak.sh #!/bin/bash cat $1|while read line do a=($line) #/usr/bin/expect<执行
- 我下面放了3台连续结果的内容【3台过程和300台一样】
[root@node-2 ccx]# sh mak.sh iplist83-104.txt spawn ssh root@1.2.3.102 Warning: Permanently added '1.2.3.102' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported Last login: Thu May 12 11:36:55 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@stor-20 ~]# scp 1.2.3.4:/root/ccx/mac-sh.sh /root/ Authorized users only. All activity may be monitored and reported root@1.2.3.4's password: mac-sh.sh 100% 197 0.2KB/s 00:00 [root@stor-20 ~]# sh /root/mac-sh.sh Authorized users only. All activity may be monitored and reported root@1.2.3.4's password: stor-20 100% 36 0.0KB/s 00:00 [root@stor-20 ~]# sleep 1 [root@stor-20 ~]# sleep 1 [root@stor-20 ~]# rm -f /root/mac-sh.sh [root@stor-20 ~]# rm -f node-2.domain.tld [root@stor-20 ~]# userdel -r danzengciren [root@stor-20 ~]# userdel -r wangying [root@stor-20 ~]# sleep 1 [root@stor-20 ~]# useradd dzcr_yz [root@stor-20 ~]# useradd wangying1 [root@stor-20 ~]# sleep 1 [root@stor-20 ~]# echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1 Changing password for user wangying1. passwd: all authentication tokens updated successfully. [root@stor-20 ~]# echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz Changing password for user dzcr_yz. passwd: all authentication tokens updated successfully. [root@stor-20 ~]# sleep 1 [root@stor-20 ~]# useradd luofengc [root@stor-20 ~]# useradd zhangjing3 [root@stor-20 ~]# sleep 1 [root@stor-20 ~]# echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc Changing password for user luofengc. passwd: all authentication tokens updated successfully. [root@stor-20 ~]# echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3 Changing password for user zhangjing3. passwd: all authentication tokens updated successfully. [root@stor-20 ~]# echo ________________________ ________________________ [root@stor-20 ~]# exit logout Connection to 1.2.3.102 closed. spawn ssh root@1.2.3.103 Warning: Permanently added '1.2.3.103' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported root@1.2.3.103's password: Last login: Thu May 12 11:36:59 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@stor-21 ~]# scp 1.2.3.4:/root/ccx/mac-sh.sh /root/ Authorized users only. All activity may be monitored and reported root@1.2.3.4's password: mac-sh.sh 100% 197 0.2KB/s 00:00 [root@stor-21 ~]# sh /root/mac-sh.sh Authorized users only. All activity may be monitored and reported root@1.2.3.4's password: stor-21 100% 36 0.0KB/s 00:00 [root@stor-21 ~]# sleep 1 [root@stor-21 ~]# sleep 1 [root@stor-21 ~]# rm -f /root/mac-sh.sh [root@stor-21 ~]# rm -f node-2.domain.tld [root@stor-21 ~]# userdel -r danzengciren [root@stor-21 ~]# userdel -r wangying [root@stor-21 ~]# sleep 1 [root@stor-21 ~]# useradd dzcr_yz [root@stor-21 ~]# useradd wangying1 [root@stor-21 ~]# sleep 1 [root@stor-21 ~]# echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1 Changing password for user wangying1. passwd: all authentication tokens updated successfully. [root@stor-21 ~]# echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz Changing password for user dzcr_yz. passwd: all authentication tokens updated successfully. [root@stor-21 ~]# sleep 1 [root@stor-21 ~]# useradd luofengc [root@stor-21 ~]# useradd zhangjing3 [root@stor-21 ~]# sleep 1 [root@stor-21 ~]# echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc Changing password for user luofengc. passwd: all authentication tokens updated successfully. [root@stor-21 ~]# echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3 Changing password for user zhangjing3. passwd: all authentication tokens updated successfully. [root@stor-21 ~]# echo ________________________ ________________________ [root@stor-21 ~]# exit logout Connection to 1.2.3.103 closed. spawn ssh root@1.2.3.104 Warning: Permanently added '1.2.3.104' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported root@1.2.3.104's password: Last login: Thu May 12 11:37:00 2022 from 1.2.3.4 Authorized users only. All activity may be monitored and reported [root@stor-22 ~]# scp 1.2.3.4:/root/ccx/mac-sh.sh /root/ Authorized users only. All activity may be monitored and reported root@1.2.3.4's password: mac-sh.sh 100% 197 0.2KB/s 00:00 [root@stor-22 ~]# sh /root/mac-sh.sh Authorized users only. All activity may be monitored and reported root@1.2.3.4's password: stor-22 100% 36 0.0KB/s 00:00 [root@stor-22 ~]# sleep 1 [root@stor-22 ~]# sleep 1 [root@stor-22 ~]# rm -f /root/mac-sh.sh [root@stor-22 ~]# rm -f node-2.domain.tld [root@stor-22 ~]# userdel -r danzengciren [root@stor-22 ~]# userdel -r wangying [root@stor-22 ~]# sleep 1 [root@stor-22 ~]# useradd dzcr_yz [root@stor-22 ~]# useradd wangying1 [root@stor-22 ~]# sleep 1 [root@stor-22 ~]# echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1 Changing password for user wangying1. passwd: all authentication tokens updated successfully. [root@stor-22 ~]# echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz Changing password for user dzcr_yz. passwd: all authentication tokens updated successfully. [root@stor-22 ~]# sleep 1 [root@stor-22 ~]# useradd luofengc [root@stor-22 ~]# useradd zhangjing3 [root@stor-22 ~]# sleep 1 [root@stor-22 ~]# echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc Changing password for user luofengc. passwd: all authentication tokens updated successfully. [root@stor-22 ~]# echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3 Changing password for user zhangjing3. passwd: all authentication tokens updated successfully. [root@stor-22 ~]# echo ________________________ ________________________ [root@stor-22 ~]# exit logout Connection to 1.2.3.104 closed.
- 看记录文件
我就跑了一个ip信息文件【我ip都是分多个文件存放的】[root@node-2 ccx]# cd mak [root@node-2 mak]# ls node-1.domain.tld node-2.domain.tld node-3.domain.tld stor-1 stor-10 stor-12 stor-13 stor-14 stor-15 stor-16 stor-17 stor-18 stor-19 stor-2 stor-20 stor-21 stor-22 stor-3 stor-4 stor-5 stor-6 stor-7 stor-8 stor-9 [root@node-2 mak]#3、汇总信息【遍历目录文件】
- 所谓汇总
- 1、获取目录下的所有文件名
- 2、cat 这些文件名,将里面的内容追加到一个文件中即可。
- 获取目录下的所有文件名
我网上找了下这种代码,都是复制粘贴的如下代码【没营养,还不好使】
下面代码,执行结果发现没,遍历了2次确实没啥问题,但是每一次都是获取了所有内容啊【我目录下当前就2个文件】,这样我还cat个der,我必须要一次一个值,这样遍历所有目录文件。[root@node-2 ccx]# cat while.sh #!/bin/bash files=$(ls mak) for filename in $files ; do echo $files done [root@node-2 ccx]# [root@node-2 ccx]# sh while.sh node-1.domain.tld node-2.domain.tld node-1.domain.tld node-2.domain.tld [root@node-2 ccx]#获取目录下所有文件名【while】
- 百度不行,靠自己写呗,for有问题我就用while
好像也不难,反正while可以就行了,如下,一次一个值,遍历完目录下所有的值。[root@node-2 ccx]# cat while.sh #!/bin/bash ls mak|while read line do echo $line done [root@node-2 ccx]# [root@node-2 ccx]# sh while.sh node-1.domain.tld node-2.domain.tld [root@node-2 ccx]#汇总信息
- 上面文件名称已经可以获取到了,那么下面就简单了,cat结合>> 即可
代码如下[root@node-2 ccx]# cat while.sh #!/bin/bash #files=$(ls mak) #for filename in $files ; do # echo $files #done ls mak|while read line do # echo $line cat mak/$line >> mactest.txt done [root@node-2 ccx]#
- 执行代码看看效果
没问题了。[root@node-2 ccx]# cat while.sh #!/bin/bash #files=$(ls mak) #for filename in $files ; do # echo $files #done ls mak|while read line do # echo $line cat mak/$line >> mactest.txt done [root@node-2 ccx]# [root@node-2 ccx]# sh while.sh [root@node-2 ccx]# cat mactest.txt 1.2.3.3 1.2.3.3 : 02:2a:e8:bc:05:4a 8e:99:1d:d1:d5:67 1.2.3.4 : f6:a4:b4:aa:ac:49 [root@node-2 ccx]#
- 但是上面呢,因为123.3有2个ip,所以值有点乱,后面得手动删除这种重复ip信息,有点烦,不过应该不多,无所谓了,
当然,你想有个跨行隔开每一个值也是可以的,echo打印n就行了,如下[root@node-2 ccx]# cat while.sh #!/bin/bash ls mak|while read line do # echo $line cat mak/$line >> mactest.txt echo -e "n" done [root@node-2 ccx]# # 这样清晰多了 [root@node-2 ccx]# sh while.sh [root@node-2 ccx]# cat mactest.txt 1.2.3.3 1.2.3.3 : 02:2a:e8:bc:05:4a 8e:99:1d:d1:d5:67 1.2.3.4 : f6:a4:b4:aa:ac:49 1.2.3.3 1.2.3.3 : 02:2a:e8:bc:05:4a 8e:99:1d:d1:d5:67 1.2.3.4 : f6:a4:b4:aa:ac:49 1.2.3.3 1.2.3.3 : 02:2a:e8:bc:05:4a 8e:99:1d:d1:d5:67 1.2.3.4 : f6:a4:b4:aa:ac:49 [root@node-2 ccx]# [root@node-2 ccx]# > mactest.txt [root@node-2 ccx]#最终代码完整汇总下面代码具体使用看上面哈
sh代码【被调用的脚本】[root@node-2 ccx]# cat mac-sh.sh #!/bin/bash echo -e "`ifconfig | egrep 1.2.3 | awk '{print $2}'` : `ifconfig | egrep -A3 1.2.3 | grep ether|awk '{print $2}'`" > `hostname` scp `hostname` 1.2.3.4:/root/ccx/mak [root@node-2 ccx]#expect代码【执行的是这个】[root@node-2 ccx]# cat mak.sh #!/bin/bash cat $1|while read line do a=($line) #/usr/bin/expect<汇总代码 [root@node-2 ccx]# cat while.sh #!/bin/bash ls mak|while read line do # echo $line cat mak/$line >> mactest.txt # echo -e "n" >> mactest.txt done [root@node-2 ccx]#上面代码执行了以后,就得到了下面的所有信息了
现成脚本需要改的地方说明
我之前说过,我这有些重复的ip,所以信息有个会多一个,我vi编辑删除以后,就可以复制到本地excel里面了。
- 上面的应该知道要修改些啥东西了吧?
我这换了一个地方跑,修改的地方其实只有ip【环境也要复刻哈,跟着上面一步步来,有不懂的留言或私信一起交流学习。】
而且也要先执行单个脚本命令,确定没问题了再把命令放到脚本中[root@controller01 ccx]# cat mac-sh.sh #!/bin/bash echo -e "`ifconfig | egrep 11.12.13 | awk '{print $2}'` : `ifconfig | egrep -A3 11.12.13 | grep ether|awk '{print $2}'`" > `hostname` scp `hostname` 11.12.13.1:/root/ccx/mak [root@controller01 ccx]# cat mak.sh #!/bin/bash cat $1|while read line do a=($line) #/usr/bin/expect<过程!
[root@controller01 ccx]# sh mak.sh ip1-3.txt spawn ssh root@11.12.13.1 Authorized users only. All activity may be monitored and reported Last login: Thu May 12 16:19:56 2022 from 10.234.183.226 Authorized users only. All activity may be monitored and reported [root@controller01 ~]# scp 11.12.13.1:/root/ccx/mac-sh.sh /root/ Authorized users only. All activity may be monitored and reported mac-sh.sh 100% 194 0.2KB/s 00:00 [root@controller01 ~]# sh /root/mac-sh.sh Authorized users only. All activity may be monitored and reported controller01 100% 33 0.0KB/s 00:00 [root@controller01 ~]# sleep 1 [root@controller01 ~]# rm -f /root/mac-sh.sh [root@controller01 ~]# rm -f controller01 [root@controller01 ~]# userdel -r danzengciren [root@controller01 ~]# userdel -r wangying [root@controller01 ~]# sleep 1 [root@controller01 ~]# useradd dzcr_yz [root@controller01 ~]# useradd wangying1 [root@controller01 ~]# sleep 1 [root@controller01 ~]# echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1 Changing password for user wangying1. passwd: all authentication tokens updated successfully. [root@controller01 ~]# echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz Changing password for user dzcr_yz. passwd: all authentication tokens updated successfully. [root@controller01 ~]# sleep 1 [root@controller01 ~]# useradd luofengc [root@controller01 ~]# useradd zhangjing3 [root@controller01 ~]# sleep 1 [root@controller01 ~]# echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc Changing password for user luofengc. passwd: all authentication tokens updated successfully. [root@controller01 ~]# echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3 Changing password for user zhangjing3. passwd: all authentication tokens updated successfully. [root@controller01 ~]# echo ________________________ ________________________ [root@controller01 ~]# exit logout Connection to 11.12.13.1 closed. spawn ssh root@11.12.13.2 Last login: Thu May 12 10:04:52 2022 from 10.234.183.226 Authorized users only. All activity may be monitored and reported [root@computer1 ~]# scp 11.12.13.1:/root/ccx/mac-sh.sh /root/ Warning: Permanently added '11.12.13.1' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported root@11.12.13.1's password: mac-sh.sh 100% 194 0.2KB/s 00:00 [root@computer1 ~]# sh /root/mac-sh.sh Warning: Permanently added '11.12.13.1' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported root@11.12.13.1's password: computer1 100% 33 0.0KB/s 00:00 [root@computer1 ~]# sleep 1 [root@computer1 ~]# rm -f /root/mac-sh.sh [root@computer1 ~]# rm -f controller01 [root@computer1 ~]# userdel -r danzengciren [root@computer1 ~]# userdel -r wangying [root@computer1 ~]# sleep 1 [root@computer1 ~]# useradd dzcr_yz [root@computer1 ~]# useradd wangying1 [root@computer1 ~]# sleep 1 [root@computer1 ~]# echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1 Changing password for user wangying1. passwd: all authentication tokens updated successfully. [root@computer1 ~]# echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz Changing password for user dzcr_yz. passwd: all authentication tokens updated successfully. [root@computer1 ~]# sleep 1 [root@computer1 ~]# useradd luofengc [root@computer1 ~]# useradd zhangjing3 [root@computer1 ~]# sleep 1 [root@computer1 ~]# echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc Changing password for user luofengc. passwd: all authentication tokens updated successfully. [root@computer1 ~]# echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3 Changing password for user zhangjing3. passwd: all authentication tokens updated successfully. [root@computer1 ~]# echo ________________________ ________________________ [root@computer1 ~]# exit logout Connection to 11.12.13.2 closed. spawn ssh root@11.12.13.3 Last login: Tue Apr 19 12:15:47 2022 from controller01 Authorized users only. All activity may be monitored and reported [root@computer2 ~]# scp 11.12.13.1:/root/ccx/mac-sh.sh /root/ Warning: Permanently added '11.12.13.1' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported root@11.12.13.1's password: mac-sh.sh 100% 194 0.2KB/s 00:00 [root@computer2 ~]# sh /root/mac-sh.sh Warning: Permanently added '11.12.13.1' (ECDSA) to the list of known hosts. Authorized users only. All activity may be monitored and reported root@11.12.13.1's password: computer2 100% 33 0.0KB/s 00:00 [root@computer2 ~]# sleep 1 [root@computer2 ~]# rm -f /root/mac-sh.sh [root@computer2 ~]# rm -f controller01 [root@computer2 ~]# userdel -r danzengciren [root@computer2 ~]# userdel -r wangying [root@computer2 ~]# sleep 1 [root@computer2 ~]# useradd dzcr_yz [root@computer2 ~]# useradd wangying1 [root@computer2 ~]# sleep 1 [root@computer2 ~]# echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1 Changing password for user wangying1. passwd: all authentication tokens updated successfully. [root@computer2 ~]# echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz Changing password for user dzcr_yz. passwd: all authentication tokens updated successfully. [root@computer2 ~]# sleep 1 [root@computer2 ~]# useradd luofengc [root@computer2 ~]# useradd zhangjing3 [root@computer2 ~]# sleep 1 [root@computer2 ~]# echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc Changing password for user luofengc. passwd: all authentication tokens updated successfully. [root@computer2 ~]# echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3 Changing password for user zhangjing3. passwd: all authentication tokens updated successfully. [root@computer2 ~]# echo ________________________ ________________________ [root@computer2 ~]# exit logout Connection to 11.12.13.3 closed. [root@controller01 ccx]#文件内容!
[root@controller01 ccx]# cd mak [root@controller01 mak]# ls computer1 computer10 computer11 computer12 computer13 computer14 computer15 computer16 computer2 computer3 computer4 computer5 computer6 computer7 computer8 computer9 controller01 [root@controller01 mak]#没有ifconfig命令解决
- 我上面sh脚本中是用的ifconfig命令嘛,后面我发现我们有好多的主机没有ifconfig命令,所以也就没提取到值,离了个普,ifconfig都没有,系统也太精简了!!
所以就将ifconfig改为ip a 嘛,但条件也得变,如我这sh脚本中的提取ip和mac代码就变成下面了
其他执行方式这些都一样,就是将echo那行代码改一下就好了。[root@controller01 ccx]# cat mac-sh.sh #!/bin/bash echo -e "`ip a | grep 11.12.13 | awk '{print $2}'` : `ip a | egrep -B2 11.12.13 | grep ether | awk '{print $2}'`" > `hostname` scp `hostname` 11.12.13.6:/root/ccx/mak [root@controller01 ccx]#excel匹配没有成功的数据
- ipa多了个掩码嘛,我们后面可以再excel中替换就好了。【wps可能不行,office可以】
我上面wps不得行,下面用office才替换成功的。
- 每一期都有那么几台,取不到值,有些是root被限制直登了,有些是没有ifconfig命令,目前就发现这2种情况。。。。.
那就放execel里面匹配看那几台没有取到值,手动登上去复制粘贴呗。。。。
vlookup,不会的取看我这篇文章【一般都是用少的匹配多的,这样看着轻松】:
excel两行数据匹配【vlookup、countif】- 如下,我这就缺32,那么就手动取复制呗



