-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from AlexWheeler/add-custom-sftp-capability
add jsch/ssh for sftp capabilities
- Loading branch information
Showing
4 changed files
with
48 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(ns airlift.channel | ||
(:require [airlift.sftp :as session]) | ||
(:import | ||
[com.jcraft.jsch JSch] | ||
[java.io FileInputStream | ||
File])) | ||
|
||
(defn channel [creds] | ||
(let [chan (.openChannel (session/session creds) "sftp")] | ||
(.connect chan) | ||
chan)) | ||
|
||
(defn upload [channel file_path] | ||
(let [file (File. file_path)] | ||
(.setWritable file true) | ||
(let [stream (FileInputStream. file)] | ||
(.put channel stream file_path) | ||
(.close stream)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
(ns airlift.core | ||
(:gen-class) | ||
(:require [airlift.db :as db])) | ||
(:require [airlift.db :as db] | ||
[airlift.channel :as ch] | ||
[airlift.csv :as csv])) | ||
|
||
(defn export-tables | ||
[] | ||
(let [config (read-string (slurp "config.edn"))] | ||
(println config))) | ||
|
||
(def creds {:host "" :username "" :password ""}) | ||
|
||
;;(defn -main [& args] | ||
;; (let [chan (ch/channel creds)] | ||
;; (ch/upload chan "hello.txt"))) | ||
|
||
(defn -main [& args] | ||
(let [conn (db/init "table_name" "username" "password")] | ||
(let [res (db/query conn "SELECT * FROM TABLE")] | ||
(let [columns (db/columns res)] | ||
(while (.next res) | ||
(println (map #(.getString res %) columns))))))) | ||
(let [conn (db/init "" "" "") | ||
chan (ch/channel creds)] | ||
(doseq [table []] | ||
(let [res (db/query conn (str "SELECT * FROM " table))] | ||
(let [columns (db/columns res) data (atom [])] | ||
(while (.next res) | ||
(swap! data conj (map #(.getString res %) columns))) | ||
(csv/write @data (str table ".csv")) | ||
(ch/upload chan (str table ".csv"))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
(ns airlift.sftp | ||
(:require | ||
[clj-ssh.ssh :as ssh])) | ||
[clj-ssh.ssh :as ssh]) | ||
(:import | ||
[com.jcraft.jsch JSch])) | ||
|
||
(defn init-session [{:keys [username password host]}] | ||
"returns session given map with keys username, password, host" | ||
(ssh/session (ssh/ssh-agent {}) host {:username username :password password :strict-host-key-checking :no})) | ||
(defn config [] | ||
(doto (java.util.Properties.) | ||
(.put "StrictHostKeyChecking" "no"))) | ||
|
||
(defn put [session in-file out-file] | ||
"Given session, in-file path and out-file path uploads file" | ||
(ssh/with-connection session | ||
(let [channel (ssh/ssh-sftp session)] | ||
(ssh/with-channel-connection channel | ||
(ssh/sftp channel {} :put in-file out-file))))) | ||
(defn session [{:keys [host username password]}] | ||
(doto (.getSession (JSch.) username host 22) | ||
(.setPassword password) | ||
(.setConfig (config)) | ||
(.connect))) |