Skip to content

Instantly share code, notes, and snippets.

@jreighley
Last active August 10, 2018 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jreighley/2acee67c179eba762844561115d865cd to your computer and use it in GitHub Desktop.
Save jreighley/2acee67c179eba762844561115d865cd to your computer and use it in GitHub Desktop.
Clojure on the IBMi.
~~~~
(ns jtcompojure.ASconn
(:require [clojure.java.jdbc :as j])
(:gen-class)
(:import
(com.ibm.as400.access AS400ConnectionPool
CommandCall)))
;; I make a list of systems I am going to connnect to...
;; I can add keys for anything I would like that might be different amoungst system
(def syslist [{:name "SPOKANE" :iseries true :address "ipaddress" :dftlib "Libary"}]
;; I have a function that finds the system in the list, and appends it's keys to a default set of keys and values.
(defn sysinfo [sysname]
(conj {:user "Default-USER" :pass "Default-PASSWORD" :mdlib "Default-lib"} (first (filter #(= sysname (:name %) )syslist))))
;; make a connection pool
(def pool (AS400ConnectionPool.))
;;Here is a function to run any command.
(defn as400command "Given a system and a command line command, sends the command for execution" [system command]
(let [creds (sysinfo system) ;; fetches the credentials for the system argument
AS400conn (.getConnection pool (:address creds) (:user creds) (:pass creds)) ;;gets a connection from the pool
AS400Comm (CommandCall. AS400conn) ;;creates a Java CommandCall object)
success (.run AS400Comm command) ;; Calls the .run command with the command argument
results (mapv #(str %) (.getMessageList AS400Comm))] ;; gathers the resulting messages
(.returnConnectionToPool pool AS400conn) ;; releases the connection back to the pool
results)) ;returns the results
~~~~
;; trying it from the repl:
(as400command "SPOKANE" "PING '172.16.1.2'")
=>
["AS400Message (ID: TCP3204 text: Verifying connection to host system 172.16.1.2.):com.ibm.as400.access.AS400Message@5975fe7c"
"AS400Message (ID: TCP3215 text: PING reply 1 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@216c7a48"
"AS400Message (ID: TCP3215 text: PING reply 2 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@1c713e9e"
"AS400Message (ID: TCP3215 text: PING reply 3 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@7521c01e"
"AS400Message (ID: TCP3215 text: PING reply 4 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@35156d8"
"AS400Message (ID: TCP3215 text: PING reply 5 from 172.16.1.2 took 0 ms. 256 bytes. TTL 64.):com.ibm.as400.access.AS400Message@41adc2c0"
"AS400Message (ID: TCP3211 text: Round-trip (in milliseconds) min/avg/max = 0/0/0.):com.ibm.as400.access.AS400Message@543d91a5"
"AS400Message (ID: TCP3210 text: Connection verification statistics: 5 of 5 successful (100 %)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment