File tree Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Expand file tree Collapse file tree 4 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 138
138
[session channel]
139
139
(block session (handle-errors session (libssh2-channel/send-eof channel))))
140
140
141
+ (defn setenv
142
+ " Set a selection of environment variables on the channel. These will be
143
+ visible to the next exec command. Setting environment variables may be
144
+ prohibited by the remote sshd based on the value of the AcceptEnv
145
+ configuration value.
146
+
147
+ Arguments:
148
+
149
+ channel
150
+ env A map where the keys are environment variable names and the values
151
+ are the values for those environment variables. If either the name
152
+ or value is a keyword, they will be stringified using the name
153
+ function. Everything else will be stringified using str. Nil values
154
+ will be converted to empty strings. For maximum predictability, you
155
+ should provide a map of String -> String only.
156
+
157
+ Return:
158
+
159
+ 0 on success. Errors will result in exceptions."
160
+ [session channel env]
161
+ (let [->str (fn [v]
162
+ (cond (nil? v) " "
163
+ (keyword? v) (name v)
164
+ :else (str v)))
165
+ fail-if-forbidden (fn [ret]
166
+ (if (= libssh2/ERROR_CHANNEL_REQUEST_DENIED ret)
167
+ (throw (Exception. " Setting environment variables is not permitted." ))
168
+ ret))]
169
+ (doseq [[k v] env]
170
+ (block session
171
+ (handle-errors session
172
+ (fail-if-forbidden
173
+ (libssh2-channel/setenv channel (->str k) (->str v))))))))
174
+
141
175
(defn pull
142
176
" Read some output from a given stream on a channel.
143
177
Original file line number Diff line number Diff line change 50
50
If this is not provided, then the output will be returned as a String.
51
51
:err An OutputStream which will be connected to the standard error of the
52
52
remote process and in every other way behaves like :out.
53
+ :env A map of environment variables which will be set before the command is
54
+ executed. The keys are the environment variable names. The values are
55
+ the values for those variables. The setting of environment variables is
56
+ controlled by the remote sshd and the value of its AcceptEnv
57
+ configuration variable.
53
58
54
59
Return:
55
60
78
83
inputs (if stdin {0 stdin} {})
79
84
outputs {0 stdout 1 stderr}]
80
85
(channel/with-channel session channel
86
+ (channel/setenv session channel (:env io))
81
87
(channel/exec session channel commandline)
82
88
(let [streams (channel/pump session channel inputs outputs)
83
89
out (:stream (get streams 0 ))
Original file line number Diff line number Diff line change 65
65
(is (= " " (:err result)))
66
66
(is (= 0 (:exit result))))))))
67
67
68
+ (deftest exec-can-be-given-environment-variables
69
+ (testing " Exec can be given environment variables."
70
+ (ssh/with-session session {:port 2222 }
71
+ (let [env {:FOO " foo" :BAR :bar :BAZ 1 :QUUX nil }
72
+ result (ssh/exec session " env" :env env)
73
+ out-env (->> result
74
+ :out
75
+ str/split-lines
76
+ (map #(str/split % #"=" 2 ))
77
+ (into {}))]
78
+ (is (= " foo" (get out-env " FOO" )))
79
+ (is (= " bar" (get out-env " BAR" )))
80
+ (is (= " 1" (get out-env " BAZ" )))
81
+ (is (= " " (get out-env " QUUX" )))
82
+ (is (= " " (:err result)))
83
+ (is (= 0 (:exit result)))))))
84
+
68
85
(deftest exec-times-out-when-commands-take-too-long
69
86
(testing " Commands that take too long result in a timeout"
70
87
(is (thrown? Exception (ssh/exec {:port 2222 :read-timeout 500 }
Original file line number Diff line number Diff line change @@ -104,6 +104,7 @@ launch_sshd() {
104
104
-h " ${TMP_DIR} /ssh_host_key" \
105
105
-h " ${TMP_DIR} /ssh_host_dsa_key" \
106
106
-h " ${TMP_DIR} /ssh_host_rsa_key" \
107
+ -o " AcceptEnv=*" \
107
108
-o " AuthorizedKeysFile=${TMP_DIR} /authorized_keys" \
108
109
-o " ListenAddress=${host} :${port} "
109
110
SSHD_PID=$( pgrep -f " ${TMP_DIR} /ssh_host_dsa_key" )
You can’t perform that action at this time.
0 commit comments