1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/usr/bin/expect <<EOF
         set  timeout 50
         spawn $SCP_LOG_TO_GATHER_SERVER
         expect {
                 "*yes/no)?" 
                 
                      send  "yes\n"
                      "*password:*"  {send  "GatherServerPassword\n" }
                 
                 "*password:"         
                  {
                      send  "$GatherServerPassword\n"
                  }
                }
         expect  "*password:"   { send  "$GatherServerPassword\n"  }
         expect  "100%"
         expect eof
 
EOF
 
or
#!/usr/bin/expect
 
set  timeout 1
spawn  su  root -c  "/opt/1.sh"
expect  "password: "
send  "123456\r"
interact
exit
 
最新示例
#!/usr/bin/expect  -f   //这个expect的路径就是用which expect 查看的结果
 
spawn  su  - nginx        // 切换用户
expect  "password:"       // 提示让输入密码
send  "testr"        // 输入nginx的密码
interact                 // 操作完成
 
 
#!/usr/bin/expect   
set  timeout 5 
set  server [lindex $argv 0] 
set  user [lindex $argv 1] 
set  passwd  [lindex $argv 2] 
  
spawn  ssh  -l $user $server 
expect { 
&quot;( yes /no )&quot; { send &quot;yesr&quot;; exp_continue } 
&quot;password:&quot; { send &quot;$passwdr&quot; } 
expect &quot;*Last login*&quot; interact 
 
 
#!/usr/bin/expect
set  timeout 10
set  host [lindex $argv 0]         // 第1个参数,其它2,3,4参数类似
set  username [lindex $argv 1]
set  password [lindex $argv 2]
set  src_file [lindex $argv 3]
set  dest_file [lindex $argv 4]
spawn  scp  $src_file $username@$host:$dest_file
  expect {
  &quot;( yes /no )?&quot;
    {
     send &quot;yesn&quot;
     expect &quot;*assword:&quot; { send &quot;$passwordn&quot;}
  }
  &quot;*assword:&quot;
{
  send &quot;$passwordn&quot;
}
}
expect &quot;100%&quot;
expect eof