autoexpect自动生成expect脚本
在Linux下我们经常会重复性的做一些命令的操作,这时候我们就可以用expect脚本来自动的生成我们想要的expect脚本,比如你想远程ssh 到一个服务器,并查看硬盘状态:
[wenxuwan@hzling41 ~]$ autoexpect ssh sct@10.69.245.90
autoexpect started, file is script.exp
sct@10.69.245.90's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-95-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Fri Sep 15 06:08:37 EEST 2017
System load: 0.0 Processes: 92
Usage of /: 9.3% of 60.90GB Users logged in: 1
Memory usage: 3% IP address for eth0: 10.69.245.90
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
New release '16.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-95-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Fri Sep 15 06:10:02 EEST 2017
System load: 0.0 Processes: 93
Usage of /: 9.3% of 60.90GB Users logged in: 1
Memory usage: 3% IP address for eth0: 10.69.245.90
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
New release '16.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Fri Sep 15 06:08:37 2017 from 10.159.215.65
sct@l2robot201:~$ ls -al
total 21604
drwxr-xr-x 15 sct sct 4096 Aug 23 14:03 .
drwxr-xr-x 3 root root 4096 Oct 19 2012 ..
-rw-rw-r-- 1 sct sct 37 Jun 28 07:40 1.sh
-rw------- 1 sct sct 18878 Sep 15 06:08 .bash_history
-rw-r--r-- 1 sct sct 220 Oct 19 2012 .bash_logout
-rw-r--r-- 1 sct sct 3580 Oct 22 2012 .bashrc
drwx------ 4 sct sct 4096 Jun 20 2016 .cache
-rw-r--r-- 1 sct sct 6028 Jul 31 06:06 CCSEarly_and_RTCCS_configurator.sh
-rw-r--r-- 1 sct sct 959 Jul 31 06:06 ccsrtconf_l2.sh
-rw-r--r-- 1 sct sct 2870 Jul 31 06:06 CCSRT_VM_config_testPC.sh
lrwxrwxrwx 1 sct sct 3 Oct 19 2012 ci -> /ci
drwx------ 4 sct sct 4096 Jun 20 2016 .config
drwx------ 3 sct sct 4096 Oct 19 2012 .dbus
drwxr-xr-x 2 sct sct 4096 Oct 19 2012 .fontconfig
-rw-r--r-- 1 sct sct 5365729 Jul 4 12:27 front11.pcap
drwx------ 2 sct sct 4096 Oct 19 2012 .gconf
-rw------- 1 sct sct 3243 Jun 26 11:04 id_rsa
-rw-r--r-- 1 sct sct 737 Jun 26 11:04 id_rsa.pub
-rw-r--r-- 1 sct sct 408 Feb 9 2016 keys_in_vm.sh
-rw-r--r-- 1 sct sct 869 May 19 2016 keys.sh
-rwxr-xr-x 1 sct sct 16512456 Aug 1 07:00 libCCS.so
-rwxr-xr-x 1 sct sct 102 Oct 26 2016 loginaxm
drwx------ 4 sct sct 4096 Oct 19 2012 .mozilla
drwxr-xr-x 2 root root 4096 Jun 26 11:01 .pip
-rw-r--r-- 1 sct sct 12 Aug 23 14:19 product-code
-rw-r--r-- 1 sct sct 675 Oct 19 2012 .profile
drwxr-xr-x 17 sct sct 4096 Aug 25 11:51 RobotTests
drwx------ 2 sct sct 4096 Aug 1 05:26 .ssh
drwxrwxr-x 3 sct sct 4096 Oct 19 2012 .subversion
-rw-r--r-- 1 sct sct 62959 Jul 31 06:06 SysComRoute-.xml
-rw------- 1 sct sct 5954 Jul 31 09:08 .viminfo
-rw------- 1 sct sct 281 Aug 23 10:29 .Xauthority
-rw-rw-r-- 1 sct sct 46 Oct 19 2012 .xinitrc
-rw-r--r-- 1 sct sct 7453 Oct 19 2012 .xscreensaver
drwxrwxr-x 5 sct sct 4096 Aug 23 14:03 YAFT_IMAGE
drwxrwxr-x 2 sct sct 4096 Aug 23 05:59 YAFT_localPsZips
drwxrwxr-x 2 sct sct 12288 Aug 23 14:03 YAFT_logs
sct@l2robot201:~$ ls
1.sh front11.pcap libCCS.so YAFT_IMAGE
CCSEarly_and_RTCCS_configurator.sh id_rsa loginaxm YAFT_localPsZips
ccsrtconf_l2.sh id_rsa.pub product-code YAFT_logs
CCSRT_VM_config_testPC.sh keys_in_vm.sh RobotTests
ci keys.sh SysComRoute-.xml
sct@l2robot201:~$ exit
logout
Connection to 10.69.245.90 closed.
autoexpect done, file is script.exp
我们ssh到服务器并执行了ls指令,然后执行了exit指令,退出后生成了script.exp脚本文件。
然后执行脚本文件:
[wenxuwan@hzling41 ~]$ expect script.exp
spawn ssh sct@10.69.245.90
sct@10.69.245.90's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-95-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Fri Sep 15 06:19:10 EEST 2017
System load: 0.0 Processes: 93
Usage of /: 9.3% of 60.90GB Users logged in: 1
Memory usage: 3% IP address for eth0: 10.69.245.90
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
New release '16.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-95-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Fri Sep 15 06:19:53 EEST 2017
System load: 0.0 Processes: 92
Usage of /: 9.3% of 60.90GB Users logged in: 1
Memory usage: 3% IP address for eth0: 10.69.245.90
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
New release '16.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Last login: Fri Sep 15 06:19:10 2017 from 10.159.215.65
sct@l2robot201:~$ ls -al
total 21604
drwxr-xr-x 15 sct sct 4096 Aug 23 14:03 .
drwxr-xr-x 3 root root 4096 Oct 19 2012 ..
-rw-rw-r-- 1 sct sct 37 Jun 28 07:40 1.sh
-rw------- 1 sct sct 18908 Sep 15 06:19 .bash_history
-rw-r--r-- 1 sct sct 220 Oct 19 2012 .bash_logout
-rw-r--r-- 1 sct sct 3580 Oct 22 2012 .bashrc
drwx------ 4 sct sct 4096 Jun 20 2016 .cache
-rw-r--r-- 1 sct sct 6028 Jul 31 06:06 CCSEarly_and_RTCCS_configurator.sh
-rw-r--r-- 1 sct sct 959 Jul 31 06:06 ccsrtconf_l2.sh
-rw-r--r-- 1 sct sct 2870 Jul 31 06:06 CCSRT_VM_config_testPC.sh
lrwxrwxrwx 1 sct sct 3 Oct 19 2012 ci -> /ci
drwx------ 4 sct sct 4096 Jun 20 2016 .config
drwx------ 3 sct sct 4096 Oct 19 2012 .dbus
drwxr-xr-x 2 sct sct 4096 Oct 19 2012 .fontconfig
-rw-r--r-- 1 sct sct 5365729 Jul 4 12:27 front11.pcap
drwx------ 2 sct sct 4096 Oct 19 2012 .gconf
-rw------- 1 sct sct 3243 Jun 26 11:04 id_rsa
-rw-r--r-- 1 sct sct 737 Jun 26 11:04 id_rsa.pub
-rw-r--r-- 1 sct sct 408 Feb 9 2016 keys_in_vm.sh
-rw-r--r-- 1 sct sct 869 May 19 2016 keys.sh
-rwxr-xr-x 1 sct sct 16512456 Aug 1 07:00 libCCS.so
-rwxr-xr-x 1 sct sct 102 Oct 26 2016 loginaxm
drwx------ 4 sct sct 4096 Oct 19 2012 .mozilla
drwxr-xr-x 2 root root 4096 Jun 26 11:01 .pip
-rw-r--r-- 1 sct sct 12 Aug 23 14:19 product-code
-rw-r--r-- 1 sct sct 675 Oct 19 2012 .profile
drwxr-xr-x 17 sct sct 4096 Aug 25 11:51 RobotTests
drwx------ 2 sct sct 4096 Aug 1 05:26 .ssh
drwxrwxr-x 3 sct sct 4096 Oct 19 2012 .subversion
-rw-r--r-- 1 sct sct 62959 Jul 31 06:06 SysComRoute-.xml
-rw------- 1 sct sct 5954 Jul 31 09:08 .viminfo
-rw------- 1 sct sct 281 Aug 23 10:29 .Xauthority
-rw-rw-r-- 1 sct sct 46 Oct 19 2012 .xinitrc
-rw-r--r-- 1 sct sct 7453 Oct 19 2012 .xscreensaver
drwxrwxr-x 5 sct sct 4096 Aug 23 14:03 YAFT_IMAGE
drwxrwxr-x 2 sct sct 4096 Aug 23 05:59 YAFT_localPsZips
drwxrwxr-x 2 sct sct 12288 Aug 23 14:03 YAFT_logs
sct@l2robot201:~$ ls
1.sh front11.pcap libCCS.so YAFT_IMAGE
CCSEarly_and_RTCCS_configurator.sh id_rsa loginaxm YAFT_localPsZips
ccsrtconf_l2.sh id_rsa.pub product-code YAFT_logs
CCSRT_VM_config_testPC.sh keys_in_vm.sh RobotTests
ci keys.sh SysComRoute-.xml
sct@l2robot201:~$ exit
logout
Connection to 10.69.245.90 closed.
可以看到脚本文件把前面你的操作会自动执行一遍。
生成的脚本包含很多匹配的信息,有时候需要手动去修改一下脚本。但总体的代码不需要改动。对于经常执行的命令,是一利器。