当前位置:网站首页>The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
2022-06-27 05:56:00 【/*Protect her smile】
List of articles
explain
In fact, I have already mentioned the solution to this problem in my previous article
expect combination scp Script extraction for automatic password input linux The host ip And its mac Address 、scp combination expect Use 【 no need spawn start-up scp】、while Traverse the directory to get all the file names 【cat file >> To the same directory 】But today , I am here expect Use in scp When , I forgot that I had encountered this problem in the above script , So I've been reporting mistakes , It makes me feel bad , It took me a long time to remember what I met before , But it's no use remembering , It is found that the requirements of this time are fundamentally different from those of the last time , You can't cash it out directly , So I think it is necessary to take a separate article to talk about this problem !!!!
You may not understand what the title means , Anyway. , When you are in expect Use in script scp When ordered , We know scp This is followed by the file name parameter , And we run in batches scp The following file names cannot be the same , So here's the problem , Since... On each host scp The file name cannot be the same , Then we may need to use other methods to dynamically obtain this scp name 【 The following command line 】.
- So what I want to say is , This dynamic name , stay expect Cannot be dynamically obtained in , The obtained parameters are the parameters of your execution machine , Instead of expect Login Host parameters , So it will cause parameter acquisition failure .
- Execute the code as follows ,
ls /tmp/|grep 10.20.The obtained value can only be that of the executing machine , Instead of logging into the host , As for why we can only get the parameters of the executing machine , I won't explain , Involved in the implementation of the sub bash 了 , It's a bit complicated to explain .
expect "*]#" {
send "scp `ls /tmp/|grep 10.20.` 10.20.101.6:/tmp/hegui\r"}
- The solution is , Put this command on the login host , then expect Call this command on the login host , Simply put, this line of command is stripped away expect Script .
- The method in the script at the beginning of my article is to scp The script is fixed to the execution machine , Then log in to the host, copy the script on the execution machine, and then call the execution , I'll try another way , Direct use echo The dynamic method writes the script contents to the login machine , Then call , Relatively speaking, it is more flexible ?
demand & Script code
- The difference between the script in the above article and this script , this 2 The difference is basically scp All the scenes encountered :
- The script in the above article redirects the result returned by the command to a file name , The file name is fixed 【 The content is changing , But it doesn't affect 】,scp The file name of is fixed , So you can directly put scp Code writing sh Script , take sh Put the script on a host ,expect Script copy sh Just execute the script .
- The script in this article is to get a file through the script , The file name is not fixed , So the above method cannot be used , You must find a way to get the file name generated by the running script on this computer , Then put the file name in scp Back 【 Difficult is difficult expect Cannot use... In script
$()This method is used to get the value , You have to find a way to get the file name , Then define to scp Back 】
- My needs here are :
- 1、 adopt rpm Package installation 2 An order
- 2、 Execute compliance scripts
- 3、 Copy the file to the executive machine for summary 【 Mainly this 】
- First, explain the main difficult implementation code of this article
In the above article The idea is right , take scp Put the code in a sh Script , The difficulty of this article is , How to generate this scp Of sh Code ,scp Followed by the file name , How can this file name be dynamically obtained and followed scp Back !
# On a host , The following code can locate the file generated by the script
[[email protected] ~]# ls /tmp/ | grep 10.241
10.20.101.1_46c221be-6ab2-ef53-1589-fe16877914f4_chk.xml
[[email protected] ~]#
# It's easy to do it manually scp `ls /tmp/ | grep 10.241`10.20.101.6:/tmp/hegui' >/tmp/scp.sh
# But expect China does not support it. ` ` Return results , therefore ls /tmp/ | grep 10.241 How to put the value of scp It's really a bit tormenting in the back
# Scripts may not be copied anywhere else , Because the file name is not fixed , Cannot be used on this machine `` Dynamic acquisition cannot be realized .
# I tried many, many ways , Finally, it is found that the following code is a positive solution , It's the easiest ! Write the code directly to the file , Give Way ` Just don't execute .
expect "*]#" {
send "cd /tmp/\r"}
# below grep The conditions should be changed
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"}
- I have been using the following overall framework , All that changes is send The content in , So I won't explain the code , There's something I can't understand , Just leave a message or a private letter .
[[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]#
Code execution
- Before execution, you need to create a
/tmp/heguifile 【 All the files defined above are placed in this folder 】
[[email protected] ccx]# mkdir /tmp/hegui
- Then you can execute the code :
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
....
verification
The above script has finished , Nono , That's great !
[[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]#
边栏推荐
- Some articles about component packaging and my experience
- C Primer Plus 第11章_字符串和字符串函数_代码和练习题
- Assembly language - Wang Shuang Chapter 3 notes and experiments
- 双位置继电器RXMVB2 R251 204 110DC
- 【合辑】点云基础知识及点云催化剂软件功能介绍
- Spark 之 Projection
- 表单校验 v-model 绑定的变量,校验失效的解决方案
- NEON优化1:软件性能优化、降功耗怎么搞?
- 创建一个基础WDM驱动,并使用MFC调用驱动
- Netease cloud music params and encseckey parameter generation code
猜你喜欢

我对于测试团队建设的意见

NLP-D62-nlp比赛D31&刷题D15

双位置继电器RXMVB2 R251 204 110DC

多线程基础部分Part 1

Qt使用Valgrind分析内存泄漏

Program ape learning Tiktok short video production

Go log -uber open source library zap use
![Senior [Software Test Engineer] learning route and necessary knowledge points](/img/51/1be2e0812a6bca9e5e8d14bf254254.png)
Senior [Software Test Engineer] learning route and necessary knowledge points

How JQ gets the reciprocal elements

开门小例子学习十种用例图
随机推荐
mysql 查询时将状态改为相对应的文字
网易云音乐params和encSecKey参数生成代码
Epics record reference 5 -- array analog input recordarray analog input (AAI)
Double position relay jdp-1440/dc110v
Obtenir le volume du système à travers les plateformes de l'unit é
项目-h5列表跳转详情,实现后退不刷新,修改数据则刷新的功能(记录滚动条)
1317. 将整数转换为两个无零整数的和
leetcode299周赛记录
软件测试年终总结报告模板
How win 10 opens the environment variables window
IAR systems fully supports Centrino technology 9 series chips
Luogu p2939 [usaco09feb]revamping trails G
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
函数式 连续式
QT using Valgrind to analyze memory leaks
Code is data
cpu-z中如何查看内存的频率和内存插槽的个数?
leetcode298周赛记录
Avoid asteroids