Script for Telnet

Asked

Viewed 651 times

1

How to create a script with previously established commands for Telnet? In the company where I work, we use Telnet daily to configure Onus, however, because we do not have a programming team, it is a very manual and repetitive task, because we always give the same commands.

I would like to know how to create a script, preferably in Java, for Telnet or if you have how to create.

1 answer

1


Being java there is nothing ready that will do just that, you yourself have to create, however if you already have experience with Java, then the way is to use this:

It should look something like this:

import java.io.*;
import java.lang.String;

public class Commands
{
    static final Runtime run = Runtime.getRuntime();
    static Process pro;
    static BufferedReader read;

    public static void main(String[] args)
    {
        String[] cmds = {
            "telnet ...", //primeiro telnet
            "comando após o telnet",
            "telnet ...", //segundo telnet
            "comando após o telnet",
        };

        try {
            ProcessBuilder builder = new ProcessBuilder("cmd", "/c",
                String.join("& ", cmds));

            builder.redirectErrorStream(true);

            Process p = builder.start();

            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;

            while (true) {
                line = r.readLine();
                if (line == null) {
                    break;
                }

                System.out.println(line); //Exibe a resposta
            }
        } catch(Exception e) {
            System.err.println(e);
        }
    }
}

you said preferably java, this means that it does not delete other ways, you can use . windows bat, or . sh on linux or Unix systems (depending on your case), see below:

Windows

If it is Windows you can put all the commands in a file with the extension .bat, something like creating a file, comandos.bat and add a content like this:

@echo off

telnet etc etc etc

pause

Unix/Linux

If it is a system with Unix or Linux nucleus, you can create a file with the extension .sh (is optional), can call it comando.sh and add the commands like this:

#!/bin/bash

telnet etc etc etc

After creating the . sh file you must set the executable permission:

cd pasta/aonde/esta/o/seu/script
chmod +x comando.sh

The previous command sets permission for anyone to run your script, but if only your user (owner of the script) can be the one to use it, then run the chmod thus:

chmod u+x comando.sh

Important note

The /bin/bash is the path of bash, but it can change from operating system to operating system, there is no standard path between the various systems with Linux and Unix based kernels, read this for more details:

  • The only problem of this command of Telnet that you mentioned, is that to access the UN, I need to enter Login and Password, but I do not know any command that does this, would tell me some?

  • @Fernandocampanate this UN would be a web page?

  • UN is an optical fiber equipment. It converts the Optical Fiber signal into a signal that our Router can interpret. One can say that it is the same access of a Router.

  • 1

    @Fernandocampanate you access it via CMD or terminal too? If this is it, could you give me an example with dummy data for me to create something that works approximately for you?

  • I access from the Linux terminal. telnet <ip> login password

Browser other questions tagged

You are not signed in. Login or sign up in order to post.