-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsftp.clj
34 lines (30 loc) · 1.13 KB
/
sftp.clj
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
(ns sftp
"Sample code using JSch library to SFTP a file."
;; [com.jcraft/jsch "0.1.55"]]
(:import [com.jcraft.jsch JSch JSchException SftpException ChannelSftp]))
(def username "ftpuser")
(def host "ftp.destination.net")
(def password "s00p3rS3cR3T")
(def file-name "/tmp/file.xml")
(def to-name "file.xml")
(defn establish-session
[^JSch ssh host username password]
(let [session (.getSession ssh username host 22)]
(.setConfig session "StrictHostKeyChecking" "no")
(.setPassword session password)
session))
(let [ssh (JSch.)
session (establish-session ssh host username password)]
(try
(.connect session)
(let [channel (.openChannel session "sftp")]
(try
(.connect channel)
(.put channel file-name to-name)
(.exit session)
(catch JSchException e (str "JSch Problem: " (.getMessage e)))
(catch SftpException e (str "SFTP Problem: " (.getMessage e)))
(finally (.disconnect channel))))
(catch JSchException e (str "JSch Problem: " (.getMessage e)))
(catch SftpException e (str "SFTP Problem: " (.getMessage e)))
(finally (.disconnect session))))