Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java telnet session

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Location
    Durban
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java telnet session

    Good day,

    Please help as i am trying to create a java telnet session, however what is the correct jar file to download for this, i am trying to use
    "org.apache.commons.net.telnet.TelnetClient" but my TelnetSession does not know of this and therefor giving an error.

    For additional information please see the rest of my code below.

    [Code=JAVA]

    public class EasyTelnet extends javax.swing.JFrame {
    import java.net.*;
    import java.io.*;
    import org.apache.commons.net.telnet.TelnetClient;


    /**
    * Creates new form EasyTelnet
    */
    public EasyTelnet() {
    initComponents();
    }

    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jLabel2 = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    Host = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    username = new javax.swing.JTextField();
    Password = new javax.swing.JTextField();
    TelnetButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jLabel2.setText("Insert details below to telnet");

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(255, 0, 0));
    jLabel1.setText("Host:");

    Host.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    HostActionPerformed(evt);
    }
    });

    jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
    jLabel3.setForeground(new java.awt.Color(255, 0, 0));
    jLabel3.setText("Username:");

    jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
    jLabel4.setForeground(new java.awt.Color(255, 0, 0));
    jLabel4.setText("Password:");

    username.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    usernameActionPerformed(evt);
    }
    });

    Password.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    PasswordActionPerformed(evt);
    }
    });

    TelnetButton.setText("Telnet");
    TelnetButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    TelnetButtonActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel1)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
    .addComponent(Host, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
    .addGap(129, 129, 129)
    .addComponent(jLabel2)))
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel4)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel3)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addGroup(layout.createSequentialGroup()
    .addGap(155, 155, 155)
    .addComponent(TelnetButton)))
    .addContainerGap(129, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel2)
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel1)
    .addComponent(Host, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(38, 38, 38)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel3)
    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
    .addComponent(jLabel4)
    .addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(29, 29, 29)
    .addComponent(TelnetButton)
    .addContainerGap(45, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void HostActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void usernameActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void PasswordActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void TelnetButtonActionPerformed(java.awt.event.ActionE vent evt) {
    // TODO add your handling code here:
    String hostIP = Host.getText();
    String UsernameS = username.getText();
    String Pass = Password.getText();
    String shellPrompt = "$";



    TelnetSession session = new TelnetSession(hostIP);

    session.setLoginPrompt(UsernameS);

    session.setPasswordPrompt(Pass);

    session.setShellPrompt(shellPrompt);

    session.connect (UsernameS,Pass);

    //This is where i will put my Telnet commands and send them to the device






    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClass Name());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(EasyTelnet.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(EasyTelnet.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(EasyTelnet.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(EasyTelnet.clas s.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new EasyTelnet().setVisible(true);
    }
    });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField Host;
    private javax.swing.JTextField Password;
    private javax.swing.JButton TelnetButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField username;
    // End of variables declaration
    }

    /code


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Java telnet session

    It seems that your [code] wrapping tags in your post above are a bit off such that they didn't take effect on your code...

    A search on the Internet using the search term "org.apache.commons.net.telnet.TelnetClient" shows that it's in the Apache Commons Net library. That's where you can get the jar file. Remember to add the jar file to the classpath.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Location
    Durban
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java telnet session

    Thank you Jashburn for the information, Sorry about the Wrappings. I will apply what you have suggested

    Thanks

    Sihle

Similar Threads

  1. Handle Telnet Session Progmatically
    By Rihx in forum Java Networking
    Replies: 5
    Last Post: April 15th, 2012, 06:45 PM
  2. [ASK] Telnet and Java
    By ragilsms2746 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 27th, 2011, 05:51 AM
  3. Java Child process cannot execute properly for telnet command
    By mamunbu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 25th, 2011, 07:43 AM
  4. Replies: 2
    Last Post: October 29th, 2010, 03:14 AM
  5. opening Telnet Command Session
    By voyager in forum Java Networking
    Replies: 3
    Last Post: June 23rd, 2009, 10:34 AM

Tags for this Thread