说明
1、2:提取IP和mac
单台【ifconfig命令】
- 单台提取很简单【真实环境ip不变展示。下面所有IP信息有修改】
[root@node-2 ccx]
1.2.3.4 : f6:a4:b4:aa:ac:49
[root@node-2 ccx]
[root@node-2 ccx]
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 ~]
[root@node-2 ccx]
[root@node-2 ccx]
[root@node-2 mak]
/root/ccx/mak
[root@node-2 mak]
scp拷贝
- 所以我每一台上需要做的就是【这是测试代码的,代码可行就放到脚本中】
- 将ip和mac放到文件中,文件不能重名【否则汇总的时候会冲突】,可以用ip来命名文件名【根据我下面的测试,我这是用的IP,但我最后改成用主机名了】
- scp将文件拷贝到上面node-2主机上
- 删除保存ipmak信息文件
这台主机竟然有2个一样的ip,不知道之前配置ip的咋搞的,真的绝。
[root@node-1 ccx]
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]
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]
[root@node-1 ccx]
[root@node-1 ccx]
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]
[root@node-2 ~]
[root@node-2 mak]
1.2.3.3\^J1.2.3.3
[root@node-2 mak]
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]
iplist3-4.txt
iplist5-23.txt
iplist61-82.txt
iplist83-104.txt
[root@node-2 ccx]
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 $1|while read line
do
a=($line)
/usr/local/bin/expect<<EOF
spawn ssh root@${a[0]}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "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}'`\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "scp `ifconfig | egrep 1.2.3 | awk '{print $2}'` 5.6.7.134:/root/ccx/mak\r"}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "rm `ifconfig | egrep 1.2.3 | awk '{print $2}'`\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo ________________________\r"}
expect "*]#" {send "exit\r"}
expect eof
EOF
done
[root@node-2 ccx]
执行效果
- 用2个ip执行试试效果
下面有个sleep 差个1 所以报错了,问题不大,更新脚本代码就好了。 还有问题,这特么的再每一台上面获取到的ip都是我执行的主机ip,离了个大谱。 我不知道哪里处问题了,反正这种方式不行了
[root@node-2 ccx]
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 ~]
[root@node-1 ~]
[root@node-1 ~]
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: missing operand
Try 'sleep --help' for more information.
[root@node-1 ~]
[root@node-1 ~]
[root@node-1 ~]
________________________
[root@node-1 ~]
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 ~]
[root@node-2 ~]
[root@node-2 ~]
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: missing operand
Try 'sleep --help' for more information.
[root@node-2 ~]
[root@node-2 ~]
[root@node-2 ~]
________________________
[root@node-2 ~]
logout
Connection to 1.2.3.4 closed.
[root@node-2 ccx]
代码汇总【可行】
- 上面我们已经知道了需要执行的几条命令,我们用expect的方式不行,获取的ip是执行机的ip,不知道为啥,那么我们就用普通的方式来执行,用shell的方式来执行,这样就没问题了
但又遇到一个新问题,前面说过,我们写入信息名称是用ip,可我下面这个主机,有2个一样的ip,那就报错了。如下。
[root@node-1 ccx]
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]
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]
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]
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]
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]
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]
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]
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输入密码的界面,这个应该没啥可说的。
sh代码【版本2,可行且无局限】
我其实也没做啥改动,就是把删除改到另外一个脚本中了而已。 注:必须是以scp结尾。
[root@node-2 ccx]
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 $1|while read line
do
a=($line)
/usr/local/bin/expect<<EOF
spawn ssh root@${a[0]}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "scp 5.6.7.134:/root/ccx/mac-sh.sh /root/\r"}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sh /root/mac-sh.sh\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "rm /root/mac-sh.sh\r"}
expect "*]#" {send "echo ________________________\r"}
expect "*]#" {send "exit\r"}
expect eof
EOF
done
[root@node-2 ccx]
执行
[root@node-2 ccx]
[root@node-2 mak]
1.2.3.4 node-1.domain.tld
[root@node-2 mak]
[root@node-2 mak]
[root@node-2 mak]
[root@node-2 mak]
[root@node-2 mak]
[root@node-2 mak]
[root@node-2 ccx]
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 ~]
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 ~]
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 ~]
[root@node-1 ~]
[root@node-1 ~]
________________________
[root@node-1 ~]
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 ~]
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 ~]
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 ~]
[root@node-2 ~]
[root@node-2 ~]
________________________
[root@node-2 ~]
logout
Connection to 1.2.3.4 closed.
[root@node-2 ccx]
- 现在回到node-2这个主机看看ip信息是否都有
都有,可以了
[root@node-2 ccx]
[root@node-2 mak]
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 \"$pass\r\"; exp_continue;}
\"yes/no\" {send \"yes\r\";}
}
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 $1|while read line
do
a=($line)
/usr/local/bin/expect<<EOF
#spawn ssh root@${a[0]}
#expect {
# "*assword" {send "${a[1]}\r";}
# "yes/no" {send "yes\r"; exp_continue}
#}
spawn scp {a[0]}:/root/`hostname` /root/ccx/mak/
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect eof
EOF
done
最终版代码
[root@node-2 ccx]
cat $1|while read line
do
a=($line)
/usr/local/bin/expect<<EOF
spawn ssh root@${a[0]}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "scp 1.2.3.4:/root/ccx/mac-sh.sh /root/\r"}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sh /root/mac-sh.sh\r"}
#我上面执行了拷贝过来的脚本,我下面直接跟密码验证并输入密码,这样就完美解决了
#前提是sh脚本必须是scp结尾啊。
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sleep 1\r"}
#sh脚本中的删除我放到这了,在这里面要加上-f,否则可能会弹出验证是否删除。
expect "*]#" {send "rm -f /root/mac-sh.sh\r"}
expect "*]#" {send "rm -f `hostname` \r"}
# 下面这些创建用户和上面无关,我这有新需求了,我一起和上面脚本就跑了。
expect "*]#" {send "userdel -r danzengciren\r"}
expect "*]#" {send "userdel -r wangying\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "useradd dzcr_yz\r"}
expect "*]#" {send "useradd wangying1\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "useradd luofengc\r"}
expect "*]#" {send "useradd zhangjing3\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3\r"}
expect "*]#" {send "echo ________________________\r"}
expect "*]#" {send "exit\r"}
expect eof
EOF
done
[root@node-2 ccx]
执行
- 我下面放了3台连续结果的内容【3台过程和300台一样】
[root@node-2 ccx]
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 ~]
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 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
Changing password for user wangying1.
passwd: all authentication tokens updated successfully.
[root@stor-20 ~]
Changing password for user dzcr_yz.
passwd: all authentication tokens updated successfully.
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
[root@stor-20 ~]
Changing password for user luofengc.
passwd: all authentication tokens updated successfully.
[root@stor-20 ~]
Changing password for user zhangjing3.
passwd: all authentication tokens updated successfully.
[root@stor-20 ~]
________________________
[root@stor-20 ~]
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 ~]
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
Changing password for user wangying1.
passwd: all authentication tokens updated successfully.
[root@stor-21 ~]
Changing password for user dzcr_yz.
passwd: all authentication tokens updated successfully.
[root@stor-21 ~]
[root@stor-21 ~]
[root@stor-21 ~]
[root@stor-21 ~]
[root@stor-21 ~]
Changing password for user luofengc.
passwd: all authentication tokens updated successfully.
[root@stor-21 ~]
Changing password for user zhangjing3.
passwd: all authentication tokens updated successfully.
[root@stor-21 ~]
________________________
[root@stor-21 ~]
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 ~]
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
Changing password for user wangying1.
passwd: all authentication tokens updated successfully.
[root@stor-22 ~]
Changing password for user dzcr_yz.
passwd: all authentication tokens updated successfully.
[root@stor-22 ~]
[root@stor-22 ~]
[root@stor-22 ~]
[root@stor-22 ~]
[root@stor-22 ~]
Changing password for user luofengc.
passwd: all authentication tokens updated successfully.
[root@stor-22 ~]
Changing password for user zhangjing3.
passwd: all authentication tokens updated successfully.
[root@stor-22 ~]
________________________
[root@stor-22 ~]
logout
Connection to 1.2.3.104 closed.
- 看记录文件
我就跑了一个ip信息文件【我ip都是分多个文件存放的】
[root@node-2 ccx]
[root@node-2 mak]
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]
files=$(ls mak)
for filename in $files ; do
echo $files
done
[root@node-2 ccx]
[root@node-2 ccx]
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]
ls mak|while read line
do
echo $line
done
[root@node-2 ccx]
[root@node-2 ccx]
node-1.domain.tld
node-2.domain.tld
[root@node-2 ccx]
汇总信息
- 上面文件名称已经可以获取到了,那么下面就简单了,cat结合>> 即可
代码如下
[root@node-2 ccx]
ls mak|while read line
do
cat mak/$line >> mactest.txt
done
[root@node-2 ccx]
[root@node-2 ccx]
ls mak|while read line
do
cat mak/$line >> mactest.txt
done
[root@node-2 ccx]
[root@node-2 ccx]
[root@node-2 ccx]
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]
ls mak|while read line
do
cat mak/$line >> mactest.txt
echo -e "\n"
done
[root@node-2 ccx]
[root@node-2 ccx]
[root@node-2 ccx]
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]
[root@node-2 ccx]
最终代码完整汇总
下面代码具体使用看上面哈
sh代码【被调用的脚本】
[root@node-2 ccx]
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 $1|while read line
do
a=($line)
/usr/local/bin/expect<<EOF
spawn ssh root@${a[0]}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "scp 1.2.3.4:/root/ccx/mac-sh.sh /root/\r"}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sh /root/mac-sh.sh\r"}
#我上面执行了拷贝过来的脚本,我下面直接跟密码验证并输入密码,这样就完美解决了
#前提是sh脚本必须是scp结尾啊。
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sleep 1\r"}
#sh脚本中的删除我放到这了,在这里面要加上-f,否则可能会弹出验证是否删除。
expect "*]#" {send "rm -f /root/mac-sh.sh\r"}
expect "*]#" {send "rm -f `hostname` \r"}
# 下面这些创建用户和上面无关,我这有新需求了,我一起和上面脚本就跑了。
expect "*]#" {send "userdel -r danzengciren\r"}
expect "*]#" {send "userdel -r wangying\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "useradd dzcr_yz\r"}
expect "*]#" {send "useradd wangying1\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "useradd luofengc\r"}
expect "*]#" {send "useradd zhangjing3\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3\r"}
expect "*]#" {send "echo ________________________\r"}
expect "*]#" {send "exit\r"}
expect eof
EOF
done
[root@node-2 ccx]
汇总代码
[root@node-2 ccx]
ls mak|while read line
do
cat mak/$line >> mactest.txt
done
[root@node-2 ccx]
上面代码执行了以后,就得到了下面的所有信息了 我之前说过,我这有些重复的ip,所以信息有个会多一个,我vi编辑删除以后,就可以复制到本地excel里面了。
现成脚本需要改的地方说明
- 上面的应该知道要修改些啥东西了吧?
我这换了一个地方跑,修改的地方其实只有ip【环境也要复刻哈,跟着上面一步步来,有不懂的留言或私信一起交流学习。】 而且也要先执行单个脚本命令,确定没问题了再把命令放到脚本中
[root@controller01 ccx]
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 $1|while read line
do
a=($line)
/usr/local/bin/expect<<EOF
spawn ssh root@${a[0]}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "scp 11.12.13.1:/root/ccx/mac-sh.sh /root/\r"}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sh /root/mac-sh.sh\r"}
expect {
"*assword" {send "${a[1]}\r";}
"yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "rm -f /root/mac-sh.sh\r"}
expect "*]#" {send "rm -f `hostname` \r"}
expect "*]#" {send "userdel -r danzengciren\r"}
expect "*]#" {send "userdel -r wangying\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "useradd dzcr_yz\r"}
expect "*]#" {send "useradd wangying1\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)wy1' | passwd --stdin wangying1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)dzcr' | passwd --stdin dzcr_yz\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "useradd luofengc\r"}
expect "*]#" {send "useradd zhangjing3\r"}
expect "*]#" {send "sleep 1\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)lfc' | passwd --stdin luofengc\r"}
expect "*]#" {send "echo 'XZ4aUser!!2#(^^)zj3' | passwd --stdin zhangjing3\r"}
expect "*]#" {send "echo ________________________\r"}
expect "*]#" {send "exit\r"}
expect eof
EOF
done
[root@controller01 ccx]
过程!
[root@controller01 ccx]
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 ~]
Authorized users only. All activity may be monitored and reported
mac-sh.sh 100% 194 0.2KB/s 00:00
[root@controller01 ~]
Authorized users only. All activity may be monitored and reported
controller01 100% 33 0.0KB/s 00:00
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
Changing password for user wangying1.
passwd: all authentication tokens updated successfully.
[root@controller01 ~]
Changing password for user dzcr_yz.
passwd: all authentication tokens updated successfully.
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
[root@controller01 ~]
Changing password for user luofengc.
passwd: all authentication tokens updated successfully.
[root@controller01 ~]
Changing password for user zhangjing3.
passwd: all authentication tokens updated successfully.
[root@controller01 ~]
________________________
[root@controller01 ~]
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 ~]
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 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
Changing password for user wangying1.
passwd: all authentication tokens updated successfully.
[root@computer1 ~]
Changing password for user dzcr_yz.
passwd: all authentication tokens updated successfully.
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
[root@computer1 ~]
Changing password for user luofengc.
passwd: all authentication tokens updated successfully.
[root@computer1 ~]
Changing password for user zhangjing3.
passwd: all authentication tokens updated successfully.
[root@computer1 ~]
________________________
[root@computer1 ~]
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 ~]
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 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
Changing password for user wangying1.
passwd: all authentication tokens updated successfully.
[root@computer2 ~]
Changing password for user dzcr_yz.
passwd: all authentication tokens updated successfully.
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
[root@computer2 ~]
Changing password for user luofengc.
passwd: all authentication tokens updated successfully.
[root@computer2 ~]
Changing password for user zhangjing3.
passwd: all authentication tokens updated successfully.
[root@computer2 ~]
________________________
[root@computer2 ~]
logout
Connection to 11.12.13.3 closed.
[root@controller01 ccx]
文件内容!
[root@controller01 ccx]
[root@controller01 mak]
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]
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]
- ipa多了个掩码嘛,我们后面可以再excel中替换就好了。【wps可能不行,office可以】
我上面wps不得行,下面用office才替换成功的。
excel匹配没有成功的数据
- 每一期都有那么几台,取不到值,有些是root被限制直登了,有些是没有ifconfig命令,目前就发现这2种情况。。。。.
那就放execel里面匹配看那几台没有取到值,手动登上去复制粘贴呗。。。。 vlookup,不会的取看我这篇文章【一般都是用少的匹配多的,这样看着轻松】: excel两行数据匹配【vlookup、countif】 - 如下,我这就缺32,那么就手动取复制呗
|