Skip to content

Instantly share code, notes, and snippets.

@ymnk
Last active August 22, 2017 10:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ymnk/4482587 to your computer and use it in GitHub Desktop.
Save ymnk/4482587 to your computer and use it in GitHub Desktop.
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class foo {
public static void main(String[] arg){
try{
JSch jsch=new JSch();
String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host=JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name")+
"@localhost");
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);
Session session=jsch.getSession(user, host, 22);
String passwd = JOptionPane.showInputDialog("Enter password");
session.setPassword(passwd);
UserInfo ui = new MyUserInfo(){
public void showMessage(String message){
JOptionPane.showMessageDialog(null, message);
}
public boolean promptYesNo(String message){
Object[] options={ "yes", "no" };
int foo=JOptionPane.showOptionDialog(null,
message,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
return foo==0;
}
};
session.setUserInfo(ui);
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none");
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none");
session.setConfig("compression_level", "9");
session.connect(30000);
String file=JOptionPane.showInputDialog("Enter file", "");
Channel channel=session.openChannel("shell");
((ChannelShell)channel).setPty(false);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
String command="cat "+file+"\n;exit\n";
System.out.println("command: "+command);
channel.connect(3*1000);
out.write(command.getBytes());
out.flush();
File tempFile = File.createTempFile("foo","bar");
FileOutputStream fout = new FileOutputStream(tempFile);
byte[] tmp=new byte[102400];
while(true){
int i=in.read(tmp, 0, tmp.length);
if(i<0)break;
fout.write(tmp, 0, i);
}
fout.close();
System.out.println("file: "+tempFile.getAbsolutePath());
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
public static abstract class MyUserInfo
implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return null; }
public boolean promptYesNo(String str){ return false; }
public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return false; }
public boolean promptPassword(String message){ return false; }
public void showMessage(String message){ }
public String[] promptKeyboardInteractive(String destination,
String name,
String instruction,
String[] prompt,
boolean[] echo){
return null;
}
}
}
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.zip.GZIPInputStream;
public class Shell{
public static void main(String[] arg){
try{
JSch jsch=new JSch();
String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host=JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name")+
"@localhost");
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);
Session session=jsch.getSession(user, host, 22);
String passwd = JOptionPane.showInputDialog("Enter password");
session.setPassword(passwd);
UserInfo ui = new MyUserInfo(){
public void showMessage(String message){
JOptionPane.showMessageDialog(null, message);
}
public boolean promptYesNo(String message){
Object[] options={ "yes", "no" };
int foo=JOptionPane.showOptionDialog(null,
message,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
return foo==0;
}
};
session.setUserInfo(ui);
session.connect(30000);
String file=JOptionPane.showInputDialog("Enter file", "");
Channel channel=session.openChannel("shell");
((ChannelShell)channel).setPty(false);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
String command="gzip -c "+file+"\n;exit\n";
System.out.println("command: "+command);
channel.connect(3*1000);
out.write(command.getBytes());
out.flush();
InputStream gin = new GZIPInputStream(in);
File tempFile = File.createTempFile("foo","bar");
FileOutputStream fout = new FileOutputStream(tempFile);
byte[] tmp=new byte[102400];
while(true){
int i=gin.read(tmp, 0, tmp.length);
if(i<0)break;
fout.write(tmp, 0, i);
}
fout.close();
System.out.println("file: "+tempFile.getAbsolutePath());
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
public static abstract class MyUserInfo
implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return null; }
public boolean promptYesNo(String str){ return false; }
public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return false; }
public boolean promptPassword(String message){ return false; }
public void showMessage(String message){ }
public String[] promptKeyboardInteractive(String destination,
String name,
String instruction,
String[] prompt,
boolean[] echo){
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment