0

for my work I need to connect to a lot of different servers every day: telnet ti the host, enter username, enter password - commence work. Now I wanted to make life easier by automatically entering the username - I managed to do that, but telnet quits afterwards, that's obviously not what I wanted.

I work from a system with BASH and I can't install any programs there, so please don't give answers like "Use expect, that solves your problem easily..."

My tries led me to this:

function tn() { (echo "user"
                sleep 1) | telnet $1 23
        }

Calling the function with tn 123.45.67.89 connects to the server at 123.45.67.89, where the username is asked, which is entered automatically - great! But then the password is asked, and instead of letting me enter it and begin my work, the connection is closed.

I really hope someone knows a solution for this! Thanks in advance!

2 Answers2

0

You might want to look at the expect command to script interactions with telnet:

#!/usr/bin/env bash

function tn() {

  expect -f <<EOF
spawn telnet $1
expect "login"
send "${2}\r"
interact 
EOF
}

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
  • Thank you for your comment, but since I'm not an admin I can't use anything like _expect_ - just commands that are provided with bash. – Julia Linsmayer Jul 08 '19 at 07:19
0

telnetis designed for interactive usage. Use netcat, ncat, nc, socat or any other tool of this family.

Example:

( echo "user"; sleep 1) | ncat $1 23 

But if you want to simulate interactive behavior, use socat and redirect stdin+stdout to a script:

Example:

socat TCP:$1:23 EXEC:my-shell.sh

In this case, a TCP connection for address $1 port 23 is established and stdin+stdout are redirected to stdout+stdin of the script. See man socat for details and more options.

my-shell.sh look for example like:

#!/bin/sh

read line
do_domething "$line"
printf "reply\n"

read line
do_domething "$line"
printf "reply\n"

btw, I have tested nothing (just written down)

Wiimm
  • 2,971
  • 1
  • 15
  • 25