LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Scripting for telnet (https://www.linuxquestions.org/questions/linux-server-73/scripting-for-telnet-4175703296/)

vershooter 11-08-2021 10:24 PM

Scripting for telnet
 
HI,

Newbie here.

I just want to ask how to do bash script like to telnet IP PORT() then after connected I will type status. then I will see how many connection I created. I want to create a script that for example connections is above 5 then it will restart a service. Do you have insight for this one.


Thanks

Turbocapitalist 11-09-2021 12:21 AM

That could probably be scripted using expect, which is written in Tcl. Most of the links from the project home page are dead, but you might find them via the Internet Archive, if it's that important: https://core.tcl-lang.org/expect/

But what problem are you trying to solve? There are probably better tools for the task than the telnet client.

GentleThotSeaMonkey 11-09-2021 01:35 AM

This web-search finds some LQ stuff: bash script telnet site:linuxquestions.org

shruggy 11-09-2021 03:40 AM

Must it be Bash? If not, see https://metacpan.org/dist/Net-Telnet

TB0ne 11-09-2021 07:56 AM

Quote:

Originally Posted by vershooter (Post 6299623)
HI, Newbie here.

I just want to ask how to do bash script like to telnet IP PORT() then after connected I will type status. then I will see how many connection I created. I want to create a script that for example connections is above 5 then it will restart a service. Do you have insight for this one.

As others have pointed out, there are MANY easily found examples of telnet scripts on this site, and thousands more you can find with a brief Internet search.

To add to that, I would strongly suggest you rethink your approach. First, telnet isn't even installed on most distros of Linux these days because it is horribly insecure, so your first step in your 'script' would be to install and configure an insecure service on your server. Then open your firewall ports to accommodate that service...neither is good. Second, you can use SSH easily, and (after you do a keyswap) can have your command on the same line, such as:
Code:

ssh user@server.com '<your command goes here>'
...and have it be secure and easier to use.

Aside from those suggestions, bear in mind that you can also easily have a script ON the server itself to monitor that service, and act accordingly, without the need to run any commands over the network.

jefro 11-09-2021 02:30 PM

I might look at expect and autoexpect.


All times are GMT -5. The time now is 10:37 PM.