def run(command, server): output = [] error = [] jsch = JSch() session = jsch.getSession("root", server, 22) session.setPassword("couchbase") session.setConfig("StrictHostKeyChecking", "no") try: session.connect(10000) except JSchException: failed.append(server) try: _ssh_client = session.openChannel("exec") _ssh_client.setInputStream(None) _ssh_client.setErrStream(None) instream = _ssh_client.getInputStream() errstream = _ssh_client.getErrStream() _ssh_client.setCommand(command) _ssh_client.connect() fu1 = FileUtil.wrap(instream) for line in fu1.readlines(): output.append(line) fu1.close() fu2 = FileUtil.wrap(errstream) for line in fu2.readlines(): error.append(line) fu2.close() _ssh_client.disconnect() session.disconnect() except JSchException as e: print("JSch exception on %s: %s" % (server, str(e))) return output, error
def connection(server): try: jsch = JSch() session = jsch.getSession("root", server, 22) session.setPassword("couchbase") session.setConfig("StrictHostKeyChecking", "no") session.connect(5000) return session except: failed.append(server) return None
def ssh(host, usr, pwd, timeout): jsch = JSch() session = jsch.getSession(usr, host, 22) session.setPassword(pwd) config = Hashtable() config.put('StrictHostKeyChecking', 'no') session.setConfig(config) session.setDaemonThread(True) ''' connect timeout ''' session.connect(timeout*1000) channel = session.openChannel('shell') channel.setPtyType('vt102') env = Hashtable() channel.setEnv(env) expect4j = Expect4j(channel.getInputStream(), channel.getOutputStream()) ''' expect return timeout ''' channel.connect(timeout*1000) return expect4j
if gui.wasOKed(): LOCAL_MODEL_FILENAME = gui.getNextString() gui = GenericDialogPlus("Parameters") gui.addFileField("Select a python script file to upload on the server (*.py) : ", "") gui.showDialog() if gui.wasOKed(): LOCAL_PREDICTION_SCRIPT = gui.getNextString() jsch=JSch() # SSH Connection to DeepLearning server IJ.log("-- Connection to FiGoCo Server") time0 = time.time() session=jsch.getSession("root", HOST, PORT); session.setPassword("12345"); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); IJ.log("Execution Time : " + str(time.time()-time0) +" seconds") # Install all necessary python packages if (True): command = "pip3 install -U segmentation-models tifffile" channel=session.openChannel("exec"); channel.setCommand(command); channel.setInputStream(None); channel.setErrStream(System.err); instream=channel.getInputStream(); IJ.log("-- Installing python libraries on the server ...")
def execSshRemoteUsrPwd(hostname, username, password, commandsSemiColonSeperated, sessionTimeoutSecs=0, waitForOutput=True): _hostname = hostname _username = username _password = password _command = commandsSemiColonSeperated jsch = JSch() session = jsch.getSession(_username, _hostname, 22) session.setPassword(_password) config = Properties() config.put("StrictHostKeyChecking", "no") config.put("GSSAPIAuthentication", "no") config.put("UnknownHostVerification", "no") #config.put("PreferredAuthentications", "publickey"); session.setConfig(config) if (sessionTimeoutSecs > 0): session.setTimeout(sessionTimeoutSecs * 10) print 'Logging into Remote SSH Shell u/p Auth...' try: if (sessionTimeoutSecs > 0): session.connect(sessionTimeoutSecs * 1000) else: session.connect() except: return 'None' channel = session.openChannel("exec") channel.setCommand('source ~/.bash_profile 2>/dev/null; ' + _command) outputBuffer = String() stdin = channel.getInputStream() stdinExt = channel.getExtInputStream() channel.connect(sessionTimeoutSecs * 1000) if (waitForOutput == True): while (1): n = stdin.read() if n == -1: break if (chr(n) == '\n'): outputBuffer.append('|') elif (chr(n) == '\r'): outputBuffer.append('|') else: outputBuffer.append(chr(n)) while (1): n = stdinExt.read() if n == -1: break if (chr(n) == '\n'): outputBuffer.append('|') elif (chr(n) == '\r'): outputBuffer.append('|') else: outputBuffer.append(chr(n)) else: sleep(sessionTimeoutSecs) print "Command on: " + hostname + " : " + _command print "\toutput: " + outputBuffer.toString() channel.disconnect() session.disconnect() del channel del session return outputBuffer.toString()
"172.23.98.196", "172.23.98.20", "172.23.98.209", "172.23.98.21", "172.23.98.213", "172.23.98.214", "172.23.98.215", "172.23.98.79", "172.23.98.80", "172.23.98.81", "172.23.98.82", "172.23.98.83", "172.23.99.128", "172.23.99.253", "172.23.99.63", "172.23.99.93" ] from com.jcraft.jsch import JSchException, JSchAuthCancelException, \ JSchPartialAuthException, SftpException from com.jcraft.jsch import JSch from org.python.core.util import FileUtil failed = [] i = 1 for index, server in enumerate(servers): jsch = JSch() session = jsch.getSession("root", server, 22) session.setPassword("couchbase") session.setConfig("StrictHostKeyChecking", "no") try: session.connect(5000) except JSchException: failed.append(server) continue try: _ssh_client = session.openChannel("exec") _ssh_client.setInputStream(None) _ssh_client.setErrStream(None) instream = _ssh_client.getInputStream() errstream = _ssh_client.getErrStream() _ssh_client.setCommand("free -m")
from com.jcraft.jsch import JSch from org.python.core.util import FileUtil from java.lang import System jsch=JSch() host="10.3.407.170"; #intentionally wrong here user="******" session=jsch.getSession(user, host, 22); session.setPassword("coolpassword"); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); command="ls" channel=session.openChannel("exec"); channel.setCommand(command); # X Forwarding # channel.setXForwarding(true); #channel.setInputStream(System.in); channel.setInputStream(None); #channel.setOutputStream(System.out); #FileOutputStream fos=new FileOutputStream("/tmp/stderr");