expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法

说明

  • 其实我之前文章中已经有说过这个的解决方法
    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 [email protected]
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 
[email protected]'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 
[email protected]'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 [email protected]
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 
[email protected]'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]# 

猜你喜欢

转载自blog.csdn.net/cuichongxin/article/details/125452449