本文已参加「新人创造礼」活动,一起敞开创造之路

阐明

  • 其实我之前文章中已经有说过这个的解决办法
    expect结合scp主动输入密码的脚本提取linux主机的ip和其mac地址、scp结合expect运用【不用spawn发动scp】、while遍历目录获取一切文件称号【cat文件>>到同一目录】

  • 但是今天,我在expect中运用scp的时分,我又忘了上面脚本中已经遇到过这个问题,所以一直报错,搞得我真是难过,良久才想起来之前遇到过的,但想起了没用啊,发现本次的需求和上一次的底子不相同,不能直接套现来用,所以我觉得有必要独自拿一个文章来说说这个问题!!!!

  • 标题中你可能没能理解是啥意思,反正便是,当你在expect脚本中运用scp指令的时分,咱们知道scp 后边是需求跟文件名参数的,而咱们批次跑scp后边文件称号不可能相同,那么问题就来了,已然每台主机上的scp文件称号不能相同,那么咱们可能就需求用其他办法来动态获取这个scp称号【如下指令行】。

    • 所以我想表达的便是,这个动态称号,在expect中是无法动态获取的,获取到的参数是你履行机的参数,而非expect登录的主机参数,所以就会造成参数获取失利。
    • 如下履行代码,ls /tmp/|grep 10.20.获取到的值只能是履行机的,而非登录主机,至于为什么获取到的只能是履行机的参数,我这不做阐明,牵扯到履行的子bash了,解说起来有点复杂。
expect "*]#" {send "scp `ls /tmp/|grep 10.20.` 10.20.101.6:/tmp/hegui\r"}
  • 解决办法便是,把这个指令放在登录主机上,然后expect调用登录主机上的这个指令,简略来说便是把这行指令剥离开expect脚本。
  • 我文章最初脚本中的办法是将scp脚本固定到履行机上,然后登录主机复制履行机上的这个脚本再调用履行,我这换一种办法,直接用echo动态的办法将这个脚本内容写入到登录机,然后再调用,相对来说更灵活一点?

需求&脚本代码

  • 上面文章中脚本和本脚本的区别,这2种区别基本上也便是scp所遇到的一切场景了:
    • 上面文章中脚本是将指令回来的结果重定向到一个文件名中,此时文件名是固定的【内容是变化的,但不影响】,scp的文件名也便是固定的 ,所以能够直接将scp代码写入sh脚本中,将sh脚本放到某个主机上,expect脚本复制sh脚本过来履行即可。
    • ==本篇文章中的脚本是经过脚本获取到一个文件,该文件名不是固定的,所以上面办法不能用,必须想办法再本机上获取到跑脚本生成的文件名,再将该文件名放到scp后边【难就难在 expect脚本中不能用 $()这种办法来获取值,得想办法获取到文件名,然后界说到scp后边】==
  • 我这的需求是:
    • 1、经过rpm包装置2个指令
    • 2、履行合规脚本
    • 3、复制文件到履行机上汇总【首要是这个】
  • 先解说下本文章的首要困难完成代码
    上面文章中 思路是对的,将scp代码放到一个sh脚本中,本文章便是难在,如何生成这个scp的sh代码,scp 后边跟的是文件名,这个文件名到底咋动态获取并跟在scp后边!
# 主机上,下面代码就能定位到脚本生成的文件
[root@compute01 ~]# ls /tmp/ | grep 10.241
10.20.101.1_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
[root@compute01 ~]#
# 手动履行的话就简略了  scp `ls /tmp/ | grep 10.241`10.20.101.6:/tmp/hegui' >/tmp/scp.sh 
# 可是expect中不支持 ` ` 回来结果,所以ls /tmp/ | grep 10.241的值咋放到scp后边真的有点折磨人
# 脚本可能是不能放到其他地方复制过来的,因为文件名不固定,本机上不能用`` 动态获取也就完成不了。
# 我尝试了许多许多办法,最后发现下面代码是正解,也是最简略的! 直接将代码写入文件 ,让`不履行就行了。
expect "*]#" {send "cd /tmp/\r"}
#下面grep条件要改
expect "*]#" {send " echo 'scp \`ls /tmp | grep 10.20.\` 10.20.101.6:/tmp/hegui' >/tmp/scp.sh \r"}
expect "*]#" {send "sh scp.sh\r"}
  • 下面全体结构我一直再用,变化的仅仅send中的内容,所以我就不对代码做啥解说了,有看不懂的,留言或私信即可。
[root@controller01 ccx]# cat hegui.sh 
#!/bin/bash
cat $1|while read line
do
a=($line)
/usr/bin/expect<<EOF      
#/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 "mkdir /root/hegui\r"}
expect "*]#" {send "scp 10.20.101.6:/root/hegui/* /root/hegui\r"}
expect {
        "*assword" {send "${a[1]}\r";} 
        "yes/no" {send "yes\r"; exp_continue}
}
expect "*]#" {send "mkdir /root/hegui/sys\r"}
expect "*]#" {send "cd /root/hegui/\r"}
expect "*]#" {send "mv -f lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64.rpm net-tools-2.0-0.24.20131004git.el7.x86_64.rpm sysstat-10.1.5-17.el7.x86_64.rpm /root/hegui/sys/\r"}
expect "*]#" {send "cd /root/hegui/sys\r"}
expect "*]#" {send "rpm -ivhU *\r"}
expect "*]#" {send "cd /root/hegui/\r"}
expect "*]#" {send "chmod 777 46c221be-6ab2-ef53-1589-fe16877914f4.sh\r"}
expect "*]#" {send "chmod 777 46c221be-6ab2-ef53-1589-fe16877914f4.pl\r"}
expect "*]#" {send "sh 46c221be-6ab2-ef53-1589-fe16877914f4.sh root 'Gu()ar&d91!d' null\r"}
expect "*]#" {send "cd /tmp/\r"}
expect "*]#" {send " echo 'scp \`ls /tmp | grep 10.20.\` 10.20.101.6:/tmp/hegui' >/tmp/scp.sh 
#expect "*]#" {send " echo 'rm  \`ls /tmp | grep 10.20.\`\r"}
        "*assword" {send "${a[1]}\r";}
        "yes/no" {send "yes\r"; exp_continue}
}
#expect "*]#" {send "rm  /tmp/scp.sh\r"}
#send "exit\r"            
expect eof
EOF
done
[root@controller01 ccx]# 

代码履行

  • 履行前需求在履行机中创建一个/tmp/hegui文件【上面界说的文件都放到这个文件夹中】
[root@controller01 ccx]# mkdir /tmp/hegui
  • 然后就能够履行代码了 : sh hegui.sh ip.list
[root@controller01 ccx]# sh hegui.sh ip1-6.txt 
spawn ssh root@10.20.101.2
Last login: Fri Jun 24 21:46:15 2022 from controller01
 Authorized users only. All activity may be monitored and reported 
[root@compute02 ~]# mkdir /root/hegui
mkdir: cannot create directory ‘/root/hegui’: File exists
[root@compute02 ~]# scp 10.20.101.6:/root/hegui/* /root/hegui
 Authorized users only. All activity may be monitored and reported 
root@10.20.101.6's password: 
46c221be-6ab2-ef53-1589-fe16877914f4.pl                                  100%   30KB  20.8MB/s   00:00    
46c221be-6ab2-ef53-1589-fe16877914f4.sh                                  100% 1911     4.9MB/s   00:00    
lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64.rpm                100%   42KB  24.1MB/s   00:00    
net-tools-2.0-0.24.20131004git.el7.x86_64.rpm                            100%  306KB  46.0MB/s   00:00    
scp: /root/hegui/sys: not a regular file
sysstat-10.1.5-17.el7.x86_64.rpm                                         100%  315KB  49.9MB/s   00:00    
[root@compute02 ~]# mkdir /root/hegui/sys
mkdir: cannot create directory ‘/root/hegui/sys’: File exists
[root@compute02 ~]# cd /root/hegui/
[root@compute02 hegui]# mv -f lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64.rpm net-tools-2.0-0.24.20131004git.el7.x86_64.rpm sysstat-10.1.5-17.el7.x86_64.rpm /root/hegui/sys/
[root@compute02 hegui]# cd /root/hegui/sys
[root@compute02 sys]# rpm -ivhU *
warning: lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
	package lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64 is already installed
	package sysstat-10.1.5-17.el7.x86_64 is already installed
	package net-tools-2.0-0.24.20131004git.el7.x86_64 is already installed
[root@compute02 sys]# cd /root/hegui/
[root@compute02 hegui]# chmod 777 46c221be-6ab2-ef53-1589-fe16877914f4.sh
[root@compute02 hegui]# chmod 777 46c221be-6ab2-ef53-1589-fe16877914f4.pl
[root@compute02 hegui]# sh 46c221be-6ab2-ef53-1589-fe16877914f4.sh root 'Gu()ar&d91!d' null
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
ls: cannot access /var/log/mail: No such file or directory
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.
      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.
10.20.101.2_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
end write xml
DONE ALL
[root@compute02 hegui]# cd /tmp/
[root@compute02 tmp]#  echo 'scp `ls /tmp | grep 10.20.` 10.20.101.6:/tmp/hegui/' >/tmp/scp.sh 
[root@compute02 tmp]# sh scp.sh
 Authorized users only. All activity may be monitored and reported 
root@10.20.101.6's password: 
10.20.101.2_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml                100%   33KB  19.2MB/s   00:00    
[root@compute02 tmp]# spawn ssh root@10.20.101.3
Last login: Fri Jun 24 21:44:45 2022 from controller01
 Authorized users only. All activity may be monitored and reported 
[root@compute03 ~]# mkdir /root/hegui
mkdir: cannot create directory ‘/root/hegui’: File exists
[root@compute03 ~]# scp 10.20.101.6:/root/hegui/* /root/hegui
 Authorized users only. All activity may be monitored and reported 
root@10.20.101.6's password: 
46c221be-6ab2-ef53-1589-fe16877914f4.pl                                  100%   30KB  21.8MB/s   00:00    
46c221be-6ab2-ef53-1589-fe16877914f4.sh                                  100% 1911     4.9MB/s   00:00    
lm_sensors-libs-3.4.0-6.20160601gitf9185e5.el7.x86_64.rpm                100%   42KB  26.5MB/s   00:00    
net-tools-2.0-0.24.20131004git.el7.x86_64.rpm                            100%  306KB  47.0MB/s   00:00    
scp: /root/hegui/sys: not a regular file
sysstat-10.1.5-17.el7.x86_64.rpm                                         100%  315KB  50.1MB/s   00:00    
[root@compute03 ~]# mkdir /root/hegui/sys
。。。。

验证

上面脚本跑完,诺诺的,真棒!

[root@compute29 tmp]# [root@controller01 ccx]# ls /tmp/hegui/
10.20.101.10_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.11_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.12_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.13_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.14_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.1_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.15_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.16_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.17_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.18_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.19_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.20_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.21_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.22_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.23_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.24_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.2_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.25_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.26_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.27_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.28_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.29_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.30_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.31_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.33_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.34_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.3_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.35_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.36_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.4_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.5_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.6_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.7_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.8_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
10.20.101.9_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.12_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.13_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.14_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.1_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.15_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.16_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.2_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.3_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.4_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.5_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.6_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.7_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.8_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
11.11.11.9_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
[root@controller01 ccx]# 
[root@controller01 ccx]#