Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.64 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package filesystemconnection;
  7.  
  8. import com.jcraft.jsch.Channel;
  9. import com.jcraft.jsch.ChannelExec;
  10. import com.jcraft.jsch.ChannelSftp;
  11. import com.jcraft.jsch.JSch;
  12. import com.jcraft.jsch.Session;
  13. import com.jcraft.jsch.UIKeyboardInteractive;
  14. import com.jcraft.jsch.UserInfo;
  15. import java.io.InputStream;
  16. import javax.swing.JOptionPane;
  17.  
  18. /**
  19. *
  20. * @author ppestka
  21. */
  22. public class FileSystemConnectionGui extends javax.swing.JFrame {
  23.  
  24. /**
  25. * Creates new form FileSystemConnectionGui
  26. */
  27. public FileSystemConnectionGui() {
  28. initComponents();
  29. }
  30.  
  31. /**
  32. * This method is called from within the constructor to initialize the form.
  33. * WARNING: Do NOT modify this code. The content of this method is always
  34. * regenerated by the Form Editor.
  35. */
  36. @SuppressWarnings("unchecked")
  37. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  38. private void initComponents() {
  39.  
  40. fsconnpropjLabel1 = new javax.swing.JLabel();
  41. jSeparator1 = new javax.swing.JSeparator();
  42. userNamejLabel1 = new javax.swing.JLabel();
  43. userjTextField1 = new javax.swing.JTextField();
  44. passwordjLabel1 = new javax.swing.JLabel();
  45. passwordjTextField1 = new javax.swing.JTextField();
  46. hostjLabel1 = new javax.swing.JLabel();
  47. hostjTextField1 = new javax.swing.JTextField();
  48. portjLabel1 = new javax.swing.JLabel();
  49. portjTextField1 = new javax.swing.JTextField();
  50. testConnectionjButton1 = new javax.swing.JButton();
  51.  
  52. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  53. setResizable(false);
  54.  
  55. fsconnpropjLabel1.setText("Filesystem connection properties");
  56.  
  57. userNamejLabel1.setText("User :");
  58.  
  59. passwordjLabel1.setText("Password :");
  60.  
  61. hostjLabel1.setText("Host :");
  62.  
  63. portjLabel1.setText("Port :");
  64.  
  65. testConnectionjButton1.setText("TEST");
  66. testConnectionjButton1.addActionListener(new java.awt.event.ActionListener() {
  67. public void actionPerformed(java.awt.event.ActionEvent evt) {
  68. testConnectionjButton1ActionPerformed(evt);
  69. }
  70. });
  71.  
  72. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  73. getContentPane().setLayout(layout);
  74. layout.setHorizontalGroup(
  75. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76. .addGroup(layout.createSequentialGroup()
  77. .addContainerGap()
  78. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79. .addComponent(jSeparator1)
  80. .addComponent(fsconnpropjLabel1)
  81. .addGroup(layout.createSequentialGroup()
  82. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83. .addComponent(passwordjLabel1)
  84. .addComponent(hostjLabel1)
  85. .addComponent(portjLabel1)
  86. .addComponent(userNamejLabel1))
  87. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  88. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89. .addComponent(userjTextField1)
  90. .addGroup(layout.createSequentialGroup()
  91. .addComponent(portjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
  92. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  93. .addComponent(testConnectionjButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
  94. .addComponent(passwordjTextField1)
  95. .addComponent(hostjTextField1))))
  96. .addContainerGap())
  97. );
  98. layout.setVerticalGroup(
  99. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  100. .addGroup(layout.createSequentialGroup()
  101. .addContainerGap()
  102. .addComponent(fsconnpropjLabel1)
  103. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  104. .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
  105. .addGap(18, 18, 18)
  106. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  107. .addComponent(userNamejLabel1)
  108. .addComponent(userjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  109. .addGap(18, 18, 18)
  110. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  111. .addComponent(passwordjLabel1)
  112. .addComponent(passwordjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  113. .addGap(18, 18, 18)
  114. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  115. .addComponent(hostjLabel1)
  116. .addComponent(hostjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  117. .addGap(18, 18, 18)
  118. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  119. .addComponent(portjLabel1)
  120. .addComponent(portjTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  121. .addComponent(testConnectionjButton1))
  122. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  123. );
  124.  
  125. pack();
  126. }// </editor-fold>
  127.  
  128. private void testConnectionjButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  129. String user = "gdc2";// userjTextField1.getText();
  130. String pass = "aFYVW4uX";// passwordjTextField1.getText();
  131. String host = "exmxapp01";// hostjTextField1.getText();
  132. int port = 22;// Integer.parseInt(portjTextField1.getText());
  133.  
  134.  
  135.  
  136.  
  137. try{
  138. JSch jsch=new JSch();
  139.  
  140. Session session=jsch.getSession(user, host, 22);
  141.  
  142. String passwd = JOptionPane.showInputDialog("Enter password");
  143. session.setPassword(passwd);
  144.  
  145. UserInfo ui = new MyUserInfo(){
  146. public void showMessage(String message){
  147. JOptionPane.showMessageDialog(null, message);
  148. }
  149. public boolean promptYesNo(String message){
  150. Object[] options={ "yes", "no" };
  151. int foo=JOptionPane.showOptionDialog(null,
  152. message,
  153. "Warning",
  154. JOptionPane.DEFAULT_OPTION,
  155. JOptionPane.WARNING_MESSAGE,
  156. null, options, options[0]);
  157. return foo==0;
  158. }
  159.  
  160. // If password is not given before the invocation of Session#connect(),
  161. // implement also following methods,
  162. // * UserInfo#getPassword(),
  163. // * UserInfo#promptPassword(String message) and
  164. // * UIKeyboardInteractive#promptKeyboardInteractive()
  165.  
  166. };
  167.  
  168. session.setUserInfo(ui);
  169.  
  170. // It must not be recommended, but if you want to skip host-key check,
  171. // invoke following,
  172. // session.setConfig("StrictHostKeyChecking", "no");
  173.  
  174. //session.connect();
  175. session.connect(30000); // making a connection with timeout.
  176.  
  177. Channel channel=session.openChannel("shell");
  178.  
  179. // Enable agent-forwarding.
  180. //((ChannelShell)channel).setAgentForwarding(true);
  181.  
  182. channel.setInputStream(System.in);
  183. /*
  184. // a hack for MS-DOS prompt on Windows.
  185. channel.setInputStream(new FilterInputStream(System.in){
  186. public int read(byte[] b, int off, int len)throws IOException{
  187. return in.read(b, off, (len>1024?1024:len));
  188. }
  189. });
  190. */
  191.  
  192. channel.setOutputStream(System.out);
  193.  
  194. /*
  195. // Choose the pty-type "vt102".
  196. ((ChannelShell)channel).setPtyType("vt102");
  197. */
  198.  
  199. /*
  200. // Set environment variable "LANG" as "ja_JP.eucJP".
  201. ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
  202. */
  203.  
  204. //channel.connect();
  205. channel.connect(3*1000);
  206. }
  207. catch(Exception e){
  208. System.out.println(e);
  209. }
  210. }
  211.  
  212. public static abstract class MyUserInfo
  213. implements UserInfo, UIKeyboardInteractive{
  214. public String getPassword(){ return null; }
  215. public boolean promptYesNo(String str){ return false; }
  216. public String getPassphrase(){ return null; }
  217. public boolean promptPassphrase(String message){ return false; }
  218. public boolean promptPassword(String message){ return false; }
  219. public void showMessage(String message){ }
  220. public String[] promptKeyboardInteractive(String destination,
  221. String name,
  222. String instruction,
  223. String[] prompt,
  224. boolean[] echo){
  225. return null;
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. }
  234.  
  235.  
  236. // Variables declaration - do not modify
  237. private javax.swing.JLabel fsconnpropjLabel1;
  238. private javax.swing.JLabel hostjLabel1;
  239. private javax.swing.JTextField hostjTextField1;
  240. private javax.swing.JSeparator jSeparator1;
  241. private javax.swing.JLabel passwordjLabel1;
  242. private javax.swing.JTextField passwordjTextField1;
  243. private javax.swing.JLabel portjLabel1;
  244. private javax.swing.JTextField portjTextField1;
  245. private javax.swing.JButton testConnectionjButton1;
  246. private javax.swing.JLabel userNamejLabel1;
  247. private javax.swing.JTextField userjTextField1;
  248. // End of variables declaration
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement