以下代码通过expect实现了登陆172.16.111.222服务器,并将该服务器中的一个目录打包,然后scp到本机的过程。
1 #!/usr/bin/expect
2
3 set timeout 20
4 spawn ssh root@172.16.111.222
5 expect "*password*"
6 send "root123\r"
7
8 expect "*#"
9 send "rm -rf /var/FTP.tar.gz\r";
10
11 expect "*#"
12 send "sudo -i\r"
13 expect "*password*"
14 send "root123\r"
15
16 expect "*#"
17 send "tar zcvf /var/FTP.tar.gz /var/ftp/\r"
18
19 expect "*#"
20 send "exit\r"
21 sleep 1
22 spawn scp root@172.16.111.222:/var/FTP.tar.gz ./
23 expect "*password"
24 send "root123\r"
25 expect "*#"
26
27 exit