Telnet vbs到Java

时间:2017-04-22 15:16:33

标签: java vbscript telnet

我是这里的新成员,所以如果我没有列出任何你需要回答的问题。

我的问题是,我花了一段时间回复了一个vbs,它基本上输入了telnet中通过telnet配置telewell调制解调器所需的所有信息。

我想要做的是将其转换为java并制作一个漂亮的框架,询问IP-adress分配给调制解调器而不使用vb​​s正在使用的笨重方法。 vbs在一个点停止并给出一个窗口,告诉你应该在telnet命令窗口中写入“xxx.xxx.xxx.xxx”代表你想要分配给调制解调器的IP地址,然后在它再次执行时断开连接并再次尝试连接。

在sub.pause之后它弹出一个窗口,不幸的是它在瑞典语中,但它转换为“在telnet命令窗口而不是xxx.xxx.xxx.xxx中写入你想要的IP地址”。

希望somone可以给我一些方向,所以我可以开始提高我的java技能:)

溴。 Shmooss

set cloner=createobject("WScript.shell")
cloner.run"cmd"
WScript.Sleep 500
cloner.SendKeys"telnet 192.168.0.254"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"admin"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"admin"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set 4WireEnable false"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set 4WMode CO"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set 4WType Annex_B"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set 4WAutoStart true"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set 4WConnected true"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set BitRateMod Fixed"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set FixedBitRate 2056kbps"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set Mode CO"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set FramerType Level2"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set Whip Inactive"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set ActivateLine true"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"port s1 set 4WActivateLine true"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"rfc1483 set transport wanlink vci 32"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"rfc1483 set transport wanlink vpi 0"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"ip set interface iplan ipaddress xxx.xxx.xxx.xxx"
Pause
Sub Pause()
    WScript.Echo ("Ersätt xxx.xxx.xxx.xxx med den IP-ADRESS du vill att modemet skall ha. SEDAN TRYCK OK! OBS. Om du är osäker fråga någon vettigare :)")
End Sub
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.run"cmd"
WScript.Sleep 500
cloner.SendKeys"telnet xxx.xxx.xxx.xxx"
Pause
Sub Pause()
    WScript.Echo ("Ersätt xxx.xxx.xxx.xxx med den IP-ADRESS du vill att modemet skall ha. SEDAN TRYCK OK! OBS. Om du är osäker fråga någon vettigare :)")
End Sub
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"admin"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"admin"
cloner.SendKeys("{Enter}")
WScript.Sleep 500
cloner.SendKeys"system config save"
cloner.SendKeys("{Enter}")
Wscript.Sleep 500

这是我到目前为止所做的事情

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.xml.ws.handler.MessageContext;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.management.ImmutableDescriptor;
import javax.swing.DropMode;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;

public class Window extends JFrame {

    private JPanel contentPane;
    private JTextField txtNewadress;
    private JTextField textFieldwriteip;
    private JButton btnApply;
    private JTextField textProgress;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window frame = new Window();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Window() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 400, 148);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        txtNewadress = new JTextField();
        txtNewadress.setFont(new Font("Tahoma", Font.BOLD, 11));
        txtNewadress.setEditable(false);
        txtNewadress.setText("IP-Adress for your modem, write below");
        contentPane.add(txtNewadress, BorderLayout.NORTH);
        txtNewadress.setColumns(10);

        textFieldwriteip = new JTextField();
        textFieldwriteip.setFont(new Font("Tahoma", Font.PLAIN, 20));
        contentPane.add(textFieldwriteip, BorderLayout.CENTER);
        textFieldwriteip.setColumns(10);

        btnApply = new JButton("SET/APPLY");
        btnApply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                connect (textFieldwriteip.getText());
            }
        });
        contentPane.add(btnApply, BorderLayout.EAST);

        textProgress = new JTextField();
        textProgress.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


            }
        });
        contentPane.add(textProgress, BorderLayout.SOUTH);
        textProgress.setColumns(10);





    }
    void connect (String ip)
    {
      Socket sock = null;
      PrintWriter pw = null;

      try
      {
        sock = new Socket(ip = "192.168.0.254", 23);

        pw = new PrintWriter(sock.getOutputStream());


        System.out.println("Sending username");
        pw.println("admin");

        System.out.println("Sending password");
        pw.println("admin");

        System.out.println("Sending Configuration 1/15");
        pw.println("port s1 set 4WireEnable false");

        System.out.println("Sending Configuration 2/15");
        pw.println("port s1 set 4WMode CO");

        System.out.println("Sending Configuration 3/15");
        pw.println("port s1 set 4WType Annex_B");

        System.out.println("Sending Configuration 4/15");
        pw.println("port s1 set 4WautoStart true");

        System.out.println("Sending Configuration 5/15");
        pw.println("port s1 set 4WConnected true");

        System.out.println("Sending Configuration 6/15");
        pw.println("port s1 set BitRateMod Fixed");

        System.out.println("Sending Configuration 7/15");
        pw.println("port s1 set FixedBitRate 2056kbps");

        System.out.println("Sending Configuration 8/15");
        pw.println("port s1 set Mode CO");

        System.out.println("Sending Configuration 9/15");
        pw.println("port s1 set FramerType Leve12");

        System.out.println("Sending Configuration 10/15");
        pw.println("port s1 set Whip Inactive");

        System.out.println("Sending Configuration 11/15");
        pw.println("port s1 set ActivateLine true");

        System.out.println("Sending Configuration 12/15");
        pw.println("port s1 set 4WActivateLine true");

        System.out.println("Sending Configuration 13/15");
        pw.println("rfc1483 set transport wanlink vci 32");

        System.out.println("Sending Configuration 14/15");
        pw.println("rfc1483 set transport wanlink vpi 0");

        pw.close();

        sock.close();

        Thread.sleep(1000);


      } catch (IOException | InterruptedException e)
      {
            e.printStackTrace(); 
            JOptionPane.showMessageDialog(this, "SPERMABÖG");
            }
}
}

1 个答案:

答案 0 :(得分:0)

所以我在一周后发了一篇帖子,在那里我问了一些人是否有任何想法如何做到这一点。但是,没有人可以给我一个关于如何处理它或给我任何提示的答案。我自己搜索并自己学习如何去做。

所以我想与大家分享我所取得的成就

这样做可以在CO配置或CPE配置之间进行选择以放入您的Telewell调制解调器(可能与其他调制解调器配合使用但尚未尝试过)

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.OutputStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.xml.ws.handler.MessageContext;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.management.ImmutableDescriptor;
import javax.swing.DropMode;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.Window.Type;
import javax.swing.SwingConstants;
import javax.swing.JMenuBar;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.ButtonGroup;

public class Window extends JFrame {

    private JPanel contentPane;
    private JTextField txtInfo;
    private JTextField textFieldwriteip;
    private JButton btnSet;
    private JTextField textProgress;
    private JButton btnHelp;
    private final ButtonGroup buttonGroup = new ButtonGroup();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window frame = new Window();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Window() {
        setTitle("Telewell Modem Config");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 439, 166);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        btnHelp = new JButton("Help");
        btnHelp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                new HelpWindow().setVisible(true);
            }
        });
        menuBar.add(btnHelp);

        JCheckBoxMenuItem chckbxmntmCPE = new JCheckBoxMenuItem("CPE (Slave)");
        buttonGroup.add(chckbxmntmCPE);
        chckbxmntmCPE.setSelected(true);
        menuBar.add(chckbxmntmCPE);

        JCheckBoxMenuItem chckbxmntmCO = new JCheckBoxMenuItem("CO (Master)");
        buttonGroup.add(chckbxmntmCO);
        menuBar.add(chckbxmntmCO);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        txtInfo = new JTextField();
        txtInfo.setBounds(0, 0, 434, 25);
        txtInfo.setFont(new Font("Tahoma", Font.BOLD, 11));
        txtInfo.setEditable(false);
        txtInfo.setText("Press SET to start process");
        contentPane.add(txtInfo);
        txtInfo.setColumns(10);

        textFieldwriteip = new JTextField();
        textFieldwriteip.setBounds(0, 25, 284, 58);
        textFieldwriteip.setFont(new Font("Tahoma", Font.PLAIN, 30));

        contentPane.add(textFieldwriteip);
        textFieldwriteip.setColumns(10);

        btnSet = new JButton("SET");
        btnSet.setBounds(284, 25, 75, 58);
        btnSet.setFont(new Font("Lucida Console", Font.BOLD, 11));
        btnSet.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                boolean ok=false;


                if (!textFieldwriteip.getText().isEmpty()){
                    if (chckbxmntmCO.isSelected()){
                ok= connect ("192.168.0.254");
                    } else { if (chckbxmntmCPE.isSelected()) {
                        ok= connect3 ("192.168.0.254");}
                    }
                if(ok) JOptionPane.showMessageDialog(null, "Configuration Set");
                } else {
                    JOptionPane.showMessageDialog(null, "Write the wanted IP-adress for your modem");}
            }
        });
        contentPane.add(btnSet);

        textProgress = new JTextField();
        textProgress.setBounds(0, 84, 434, 27);
        textProgress.setText("-- Not Connected --");
        textProgress.setEditable(false);
        contentPane.add(textProgress);
        textProgress.setColumns(10);

        JButton buttonApply = new JButton("APPLY");
        buttonApply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                boolean ok=false;

                if (!textFieldwriteip.getText().isEmpty()){
                ok= connect2 (textFieldwriteip.getText());
                if(ok) JOptionPane.showMessageDialog(null, "Configuration Saved");
                if(ok) txtInfo.setText("Press APPLY to save configuration");
                } else {
                    JOptionPane.showMessageDialog(null, "Write the wanted IP-adress for your modem");}
            }
        });
        buttonApply.setFont(new Font("Lucida Console", Font.BOLD, 11));
        buttonApply.setBounds(359, 25, 75, 58);
        contentPane.add(buttonApply);




    }



    boolean connect (String ip)
    {
      Socket sock = null;
      PrintWriter pw = null;


      try
      {
        textProgress.setText("Connecting to 192.168.0.254");          
        sock = new Socket(ip, 23);

        pw = new PrintWriter(sock.getOutputStream());

        Thread.sleep(500);

        textProgress.setText("Sending username");
        pw.println("admin");

        Thread.sleep(500);

        textProgress.setText("Sending password");
        pw.println("admin");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 1/15");
        pw.println("port s1 set 4WireEnable false");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 2/15");
        pw.println("port s1 set 4WMode CO");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 3/15");
        pw.println("port s1 set 4WType Annex_B");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 4/15");
        pw.println("port s1 set 4WautoStart true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 5/15");
        pw.println("port s1 set 4WConnected true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 6/15");
        pw.println("port s1 set BitRateMod Fixed");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 7/15");
        pw.println("port s1 set FixedBitRate 2056kbps");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 8/15");
        pw.println("port s1 set Mode CO");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 9/15");
        pw.println("port s1 set FramerType Leve12");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 10/15");
        pw.println("port s1 set Whip Inactive");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 11/15");
        pw.println("port s1 set ActivateLine true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 12/15");
        pw.println("port s1 set 4WActivateLine true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 13/15");
        pw.println("rfc1483 set transport wanlink vci 32");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 14/15");
        pw.println("rfc1483 set transport wanlink vpi 0");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 15/15");
        pw.println("ip set interface iplan ipaddress " + textFieldwriteip.getText());

        pw.close();

        sock.close();

        Thread.sleep(1000);


      } catch (IOException | InterruptedException e)
      {
            e.printStackTrace(); 
            JOptionPane.showMessageDialog(this, "NO CONNECTION");
            return false;
            }
      return true;
    }

    boolean connect2 (String ip) {


        Socket sock = null;
      PrintWriter pw = null;


      try
      {
        textProgress.setText("Connecting " + textFieldwriteip.getText());


        sock = new Socket(ip, 23);

        pw = new PrintWriter(sock.getOutputStream());

        Thread.sleep(500);

        textProgress.setText("Sending username");
        pw.println("admin");

        Thread.sleep(500);

        textProgress.setText("Sending password");
        pw.println("admin");

        Thread.sleep(500);

        textProgress.setText("Saving System Config");
        pw.println("system config save");

        pw.close();

        sock.close();

        Thread.sleep(1000);


      } catch (IOException | InterruptedException e)
      {
            e.printStackTrace(); 
            JOptionPane.showMessageDialog(this, "Could not save config to modem");
            return false;
            }
    return true;
      }

    boolean connect3 (String ip)
    {
      Socket sock = null;
      PrintWriter pw = null;


      try
      {
        textProgress.setText("Connecting to 192.168.0.254");          
        sock = new Socket(ip, 23);

        pw = new PrintWriter(sock.getOutputStream());

        Thread.sleep(500);

        textProgress.setText("Sending username");
        pw.println("admin");

        Thread.sleep(500);

        textProgress.setText("Sending password");
        pw.println("admin");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 1/15");
        pw.println("port s1 set 4WireEnable false");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 2/15");
        pw.println("port s1 set 4WMode CPE");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 3/15");
        pw.println("port s1 set 4WType Annex_B");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 4/15");
        pw.println("port s1 set 4WautoStart true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 5/15");
        pw.println("port s1 set 4WConnected true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 6/15");
        pw.println("port s1 set BitRateMod Fixed");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 7/15");
        pw.println("port s1 set FixedBitRate 2056kbps");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 8/15");
        pw.println("port s1 set Mode CPE");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 9/15");
        pw.println("port s1 set FramerType Leve12");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 10/15");
        pw.println("port s1 set Whip Inactive");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 11/15");
        pw.println("port s1 set ActivateLine true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 12/15");
        pw.println("port s1 set 4WActivateLine true");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 13/15");
        pw.println("rfc1483 set transport wanlink vci 32");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 14/15");
        pw.println("rfc1483 set transport wanlink vpi 0");

        Thread.sleep(500);

        textProgress.setText("Sending Configuration 15/15");
        pw.println("ip set interface iplan ipaddress " + textFieldwriteip.getText());

        pw.close();

        sock.close();

        Thread.sleep(1000);


      } catch (IOException | InterruptedException e)
      {
            e.printStackTrace(); 
            JOptionPane.showMessageDialog(this, "NO CONNECTION");
            return false;
            }
      return true;
    }

} 

和帮助窗口

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class HelpWindow extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HelpWindow frame = new HelpWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public HelpWindow() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        setLocation(520 + 20 , 80 + 20 );

        JTextPane txtpnAsdasd = new JTextPane();
        txtpnAsdasd.setFont(new Font("Calibri", Font.PLAIN, 12));
        txtpnAsdasd.setEditable(false);
        txtpnAsdasd.setText("Help\r\n- The configuration will be set through factory settings IP-Adress of the modem (\"192.168.0.254\") so remember to change your pc to the same network with the modem\r\n- Choose the wanted configuration to set for your modem ( CO / CPE )\r\n- Write the wanted IP-Adress for your modem\r\n- Press \"SET\" -----> Wait for \"Configuration set\"\r\n- If using different IP-Adress for your modem than your current network is. Remember to   change your computers IP-Adress before pressing \"APPLY\"\r\n-Press \"APPLY\" ----> Wait for \"Configuration saved\"\r\n\r\nCongrats your modem is ready to be used             /// Created by: \u00D6s M J ///");
        txtpnAsdasd.setBounds(10, 11, 414, 198);
        contentPane.add(txtpnAsdasd);

        JButton btnNewButton = new JButton("RETURN TO THE CONFIG WINDOW");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);

            }
        });
        btnNewButton.setBounds(10, 209, 414, 51);
        contentPane.add(btnNewButton);
    }
}

感谢您阅读此内容并希望somone可以向我提供有关ti外观以及如何构建代码的反馈:)

溴。 Shmooss