当前位置:网站首页>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/hegui
file 【 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]#
边栏推荐
猜你喜欢
随机推荐
开门小例子学习十种用例图
数据库-索引
leetcode299周赛记录
C Primer Plus 第11章_字符串和字符串函数_代码和练习题
Open the door small example to learn ten use case diagrams
【Cocos Creator 3.5.1】input. Use of on
信息系统项目管理师---第七章 项目成本管理
Asp. Net core6 websocket simple case
双位置继电器DLS-34A DC0.5A 220VDC
Leetcode99 week race record
双位置继电器XJLS-8G/220
洛谷P2939 [USACO09FEB]Revamping Trails G 题解
【Cocos Creator 3.5.1】坐标的加法
【QT小点】实现看门狗功能,检测外部程序是否在运行
Unity中跨平台获取系统音量
Two position relay hjws-9440
Assembly language - Wang Shuang Chapter 13 int instruction - Notes
Software testing year end summary report template
【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
汇编语言-王爽 第11章 标志寄存器-笔记