61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
import com.jcraft.jsch.Channel as Channel
|
|
import com.jcraft.jsch.ChannelShell as ChannelShell
|
|
import com.jcraft.jsch.JSch as JSch
|
|
import com.jcraft.jsch.JSchException as JSchException
|
|
import com.jcraft.jsch.Session as Session
|
|
import java.lang.System as System
|
|
import java.io.PrintStream as PrintStream
|
|
|
|
|
|
#Parameters:
|
|
#CMD
|
|
#USR
|
|
#PWD
|
|
#HOST
|
|
#PORT
|
|
|
|
jsch= JSch()
|
|
session=jsch.getSession(USR, HOST, PORT)
|
|
session.setPassword(PWD)
|
|
session.setConfig("StrictHostKeyChecking", "no")
|
|
session.connect()
|
|
|
|
#channel=session.openChannel("shell")
|
|
#input_stream=channel.getInputStream()
|
|
#output_stream = channel.getOutputStream()
|
|
#print_stream = PrintStream(output_stream, True)
|
|
#print_stream.println("ls")
|
|
|
|
channel=session.openChannel("exec")
|
|
channel.setCommand(CMD)
|
|
channel.setInputStream(None)
|
|
channel.setOutputStream(System.out)
|
|
channel.setErrStream(System.err)
|
|
input_stream=channel.getInputStream()
|
|
|
|
|
|
def wait_ret():
|
|
global input_stream
|
|
rx = ""
|
|
while True:
|
|
while (input_stream.available() > 0):
|
|
i = input_stream.read()
|
|
if i < 0:
|
|
break
|
|
rx=rx+chr(i)
|
|
if channel.closed:
|
|
break
|
|
time.sleep(0.1)
|
|
return rx
|
|
|
|
|
|
channel.connect()
|
|
|
|
try:
|
|
ret = wait_ret()
|
|
except:
|
|
channel.disconnect()
|
|
session.disconnect()
|
|
raise
|
|
|
|
set_return(ret) |