当前位置:网站首页>expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法
expect脚本中使用scp命令的方法,expect脚本中scp命令获取不了值的问题完美解决方法
2022-06-27 05:40:00 【/*守护她的笑容】
说明
其实我之前文章中已经有说过这个的解决方法
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后面!
# 主机上,下面代码就能定位到脚本生成的文件
[[email protected] ~]# ls /tmp/ | grep 10.241
10.20.101.1_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
[[email protected] ~]#
# 手动执行的话就简单了 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中的内容,所以我就不对代码做啥解释了,有看不懂的,留言或私信即可。
[[email protected] 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 [email protected]${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
[[email protected] ccx]#
代码执行
- 执行前需要在执行机中创建一个
/tmp/hegui文件【上面定义的文件都放到这个文件夹中】
[[email protected] ccx]# mkdir /tmp/hegui
- 然后就可以执行代码了 :
sh hegui.sh ip.list
[[email protected] 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
[[email protected] ~]# mkdir /root/hegui
mkdir: cannot create directory ‘/root/hegui’: File exists
[[email protected] ~]# 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 [[email protected] ~]# mkdir /root/hegui/sys mkdir: cannot create directory ‘/root/hegui/sys’: File exists [[email protected] ~]# cd /root/hegui/ [[email protected] 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/ [[email protected] hegui]# cd /root/hegui/sys [[email protected] 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 [[email protected] sys]# cd /root/hegui/ [[email protected] hegui]# chmod 777 46c221be-6ab2-ef53-1589-fe16877914f4.sh [[email protected] hegui]# chmod 777 46c221be-6ab2-ef53-1589-fe16877914f4.pl [[email protected] 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 [[email protected] hegui]# cd /tmp/ [[email protected] tmp]# echo 'scp `ls /tmp | grep 10.20.` 10.20.101.6:/tmp/hegui/' >/tmp/scp.sh [[email protected] 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
[[email protected] 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
[[email protected] ~]# mkdir /root/hegui
mkdir: cannot create directory ‘/root/hegui’: File exists
[[email protected] ~]# 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
[[email protected] ~]# mkdir /root/hegui/sys
。。。。
验证
上面脚本跑完,诺诺的,真棒!
[[email protected] tmp]# [[email protected] 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
[[email protected] ccx]#
[[email protected] ccx]#
边栏推荐
- Comprehensive application of OpenCV in contour detection and threshold processing
- 微信小程序刷新当前页面
- Edge在IE模式下加载网页 - Edge设置IE兼容性
- IAR Systems全面支持芯驰科技9系列芯片
- 面试:Selenium 中有哪几种定位方式?你最常用的是哪一种?
- LeetCode-515. 在每个树行中找最大值
- 双位置继电器RXMVB2 R251 204 110DC
- EasyExcel合并相同内容单元格及动态标题功能的实现
- neo4j图数据库基本概念
- Halon common affine transformation operators
猜你喜欢

Codeforces Round #802 (Div. 2)

Junda technology - centralized monitoring scheme for multi brand precision air conditioners

体验 win10 下 oceanbase 数据库

LeetCode-515. Find the maximum value in each tree row

Leetcode298 weekly race record

leetcode299周赛记录

双位置继电器JDP-1440/DC110V

Pycharm 中 Terminal 无法进入 venv 环境的问题

How JQ gets the ID name of an element

流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
随机推荐
Nlp-d62-nlp competition d31 & question brushing D15
Two position relay xjls-8g/220
Machunmei, the first edition of principles and applications of database... Compiled final review notes
Wechat applet refreshes the current page
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
【Cocos Creator 3.5.1】input.on的使用
Open the door small example to learn ten use case diagrams
数据库-索引
网易云音乐params和encSecKey参数生成代码
Neon optimization 1: how to optimize software performance and reduce power consumption?
【Cocos Creator 3.5.1】event.getButton()的使用
DAST black box vulnerability scanner part 6: operation (final)
What is BFC? What's the usage?
Experience oceanbase database under win10
Dual position relay dls-34a dc0.5a 220VDC
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
[unity] button of UI interactive component & summary of optional base classes
Chapter 1 Introduction
Navigation [machine learning]
021 basics of C language: recursion, variable parameters