回答編集履歴

1

サンプルを追加

2018/08/09 12:21

投稿

angel_p_57
angel_p_57

スコア1672

test CHANGED
@@ -11,3 +11,113 @@
11
11
 
12
12
 
13
13
  …ってすると、対話が必要なのって、`sudo`のパスワード入れる時だけだと気付くと思います。
14
+
15
+
16
+
17
+ 手元で試してみました。
18
+
19
+ エスケープに使うバックスラッシュが目減りしていく点に注意して、ですね。なお、`pkill`のところで、`jar`の間にバックスラッシュを入れているのはややdirty hackですが、`pkill`が`expect`自体を殺さないようにするためです。
20
+
21
+
22
+
23
+ ```スクリプト
24
+
25
+ #!/bin/sh
26
+
27
+
28
+
29
+ ssh -t USER@HOST expect -d -c "'
30
+
31
+ set timeout 60
32
+
33
+ spawn sudo bash -xc \"cp -vf hoge.jar test/; pkill -f /tmp/hoge-.\\*\\\\.j\\ar && echo killed\"
34
+
35
+ expect \":\"
36
+
37
+ send \"PASSWORD\n\"
38
+
39
+ expect
40
+
41
+ wait
42
+
43
+ '"
44
+
45
+ ```
46
+
47
+
48
+
49
+ ```実行結果
50
+
51
+ $ ./expect.sh
52
+
53
+ expect version XXXX
54
+
55
+ spawn sudo bash -xc cp -vf hoge.jar test/; pkill -f /tmp/hoge-.*\.j\ar && echo killed
56
+
57
+ parent: waiting for sync byte
58
+
59
+ parent: telling child to go ahead
60
+
61
+ parent: now unsynchronized from child
62
+
63
+ spawn: returns {9121}
64
+
65
+
66
+
67
+ expect: does "" (spawn_id exp6) match glob pattern ":"? no
68
+
69
+ [sudo] password for USER:
70
+
71
+ expect: does "[sudo] password for USER: " (spawn_id exp6) match glob pattern ":"? yes
72
+
73
+ expect: set expect_out(0,string) ":"
74
+
75
+ expect: set expect_out(spawn_id) "exp6"
76
+
77
+ expect: set expect_out(buffer) "[sudo] password for USER:"
78
+
79
+ send: sending "PASSWORD\n" to { exp6 }
80
+
81
+
82
+
83
+ + cp -vf hoge.jar test/
84
+
85
+ ‘hoge.jar’ -> ‘test/hoge.jar’
86
+
87
+ + pkill -f '/tmp/hoge-.*.jar'
88
+
89
+ + echo killed
90
+
91
+ killed
92
+
93
+ expect: read eof
94
+
95
+ expect: set expect_out(spawn_id) "exp6"
96
+
97
+ expect: set expect_out(buffer) " \r\n+ cp -vf hoge.jar test/\r\n\u2018hoge.jar\u2019 -> \u2018test/hoge.jar\u2019\r\n+ pkill -f '/tmp/hoge-.*.jar'\r\n+ echo killed\r\nkilled\r\n"
98
+
99
+ argv[0] = expect argv[1] = -d argv[2] = -c argv[3] =
100
+
101
+ set timeout 60
102
+
103
+ spawn sudo bash -xc "cp -vf hoge.jar test/; pkill -f /tmp/hoge-.\*\\.j\ar && echo killed"
104
+
105
+ expect ":"
106
+
107
+ send "PASSWORD\n"
108
+
109
+ expect
110
+
111
+ wait
112
+
113
+
114
+
115
+ set argc 0
116
+
117
+ set argv0 "expect"
118
+
119
+ set argv ""
120
+
121
+ Connection to HOST closed.
122
+
123
+ ```