92

I am working on automating some telnet related tasks, using Bash scripts.

Once automated, there will be no interaction of the user with telnet (that is, the script will be totally automated).

The scripts looks something like this:

# execute some commands on the local system
# access a remote system with an IP address: 10.1.1.1 (for example)

telnet 10.1.1.1

# execute some commands on the remote system
# log all the activity (in a file) on the local system
# exit telnet
# continue with executing the rest of the script

There are two problems I am facing here:

  1. How to execute the commands on the remote system from the script (without human interaction)?

    From my experience with some test code, I was able to deduce that when telnet 10.1.1.1 is executed, telnet goes into an interactive session and the subsequent lines of code in the script are executed on the local system. How can I run the lines of code on the remote system rather than on the local one?

  2. I am unable to get a log file for the activity in the telnet session on the local system. The stdout redirect I used makes a copy on the remote system (I do not want to perform a copy operation to copy the log to the local system). How can I achieve this functionality?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
khan
  • 2,070
  • 1
  • 17
  • 14

13 Answers13

110

While I'd suggest using expect, too, for non-interactive use the normal shell commands might suffice. telnet accepts its command on stdin, so you just need to pipe or write the commands into it through heredoc:

telnet 10.1.1.1 <<EOF
remotecommand 1
remotecommand 2
EOF

(Edit: Judging from the comments, the remote command needs some time to process the inputs or the early SIGHUP is not taken gracefully by telnet. In these cases, you might try a short sleep on the input:)

{ echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1

In any case, if it's getting interactive or anything, use expect.

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
thiton
  • 35,651
  • 4
  • 70
  • 100
  • 3
    +1 `sleep` works like a dream ;-) I've also added `| tee /dev/tty |` in between to have full session on the screen ;-) – paluh Jan 13 '17 at 13:32
  • 1
    in my case, the most helpfull answer, but i had to add `echo -e "command\r";` – mf_ Aug 10 '18 at 09:27
  • 1
    How do I supply a password to telnet non-interactively? – Geremia Apr 28 '19 at 22:53
  • `{ echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1` : This example meets my requirements. Just want to know how do we add error check if telnet fails and the shell script should `exit 1`. – Yash Oct 13 '20 at 06:37
  • i had to do `(echo "command" && echo && sleep 1) | telnet ${hostname} ${port}` the main thing was the second `echo`. – Trevor Boyd Smith Jul 22 '22 at 17:22
95

Write an expect script.

Here is an example:

#!/usr/bin/expect

#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name 
#The script expects login
expect "login:" 
#The script sends the user variable
send "$user "
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password "
#This hands control of the keyboard over to you (Nice expect feature!)
interact

To run:

./myscript.expect name user password
Geremia
  • 4,745
  • 37
  • 43
joemooney
  • 1,467
  • 1
  • 13
  • 17
  • 2
    the 'expect' is mostly for interactive sessions. What I want is an automated non-interactive session. the problem I am facing is that I am unable run the scripts on the remote system (via telnet) as all the lines following the _telnet 10.1.1.1_ are executed on the local machine rather than the machine I am accessing. – khan Aug 11 '11 at 05:32
  • 3
    As Thomas Telensky pointed out depending on your needs ssh is ultimately easier. But expect can be completely non-interactive. I am assuming that you mean that all the lines following the telnet are what you want executed on the remote machine in that case just add them to the expect script as send commands and delete the interact command. But ssh is easier and more secure. – joemooney Aug 11 '11 at 06:40
51

Telnet is often used when you learn the HTTP protocol. I used to use that script as a part of my web scraper:

echo "open www.example.com 80"
sleep 2
echo "GET /index.html HTTP/1.1"
echo "Host: www.example.com"
echo
echo
sleep 2

Let's say the name of the script is get-page.sh, then this will give you an HTML document:

get-page.sh | telnet

I hope this will be helpful to someone ;)

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
biera
  • 2,608
  • 1
  • 24
  • 26
  • 1
    Most helpful comment ! This actually lets you automate sending commands by putting them in a loop – RomanM Oct 07 '14 at 17:48
  • Another help post http://blog.tonycode.com/tech-stuff/http-notes/making-http-requests-via-telnet – leomeurer Dec 04 '14 at 12:54
  • One-liner equivalent: `{ echo "GET / HTTP/1.1"; echo "Host: www.example.com"; echo; sleep 1; } | telnet www.example.com 80`. Note that the sleep is not needed at the beginning (unless you use the open command) and that only one empty echo and sleep 1 instead of 2 is needed at the end. – baptx Jun 15 '19 at 12:17
  • But if you need to do HTTPS requests, you should use netcat, also known as ncat or nc: `{ echo "GET / HTTP/1.1"; echo "Host: example.com"; echo; sleep 1; } | ncat --ssl example.com 443` – baptx Jun 15 '19 at 12:33
  • 1
    Very helpful. This get the output on the screen. Is there anyway I can wait for a known string from telnet and then run my next command, instead of sleep? – Mithun B Jan 15 '21 at 09:04
  • Another case is if I get an error like: `telnet: Unable to connect to remote host: No route to host`, I should exit. How can I handle that? So the sleep can't be a good option here. – Mithun B Jan 15 '21 at 12:30
13

This worked for me..

I was trying to automate multiple telnet logins which require a username and password. The telnet session needs to run in the background indefinitely since I am saving logs from different servers to my machine.

telnet.sh automates telnet login using the 'expect' command. More info can be found here: http://osix.net/modules/article/?id=30

telnet.sh

#!/usr/bin/expect
set timeout 20
set hostName [lindex $argv 0]
set userName [lindex $argv 1]
set password [lindex $argv 2]

spawn telnet $hostName

expect "User Access Verification"
expect "Username:"
send "$userName\r"
expect "Password:"
send "$password\r";
interact

sample_script.sh is used to create a background process for each of the telnet sessions by running telnet.sh. More information can be found in the comments section of the code.

sample_script.sh

#!/bin/bash
#start screen in detached mode with session-name 'default_session' 
screen -dmS default_session -t screen_name 
#save the generated logs in a log file 'abc.log' 
screen -S default_session -p screen_name -X stuff "script -f /tmp/abc.log $(printf \\r)"
#start the telnet session and generate logs
screen -S default_session -p screen_name -X stuff "expect telnet.sh hostname username password $(printf \\r)"
  1. Make sure there is no screen running in the backgroud by using the command 'screen -ls'.
  2. Read http://www.gnu.org/software/screen/manual/screen.html#Stuff to read more about screen and its options.
  3. '-p' option in sample_script.sh preselects and reattaches to a specific window to send a command via the ‘-X’ option otherwise you get a 'No screen session found' error.
Nikhil
  • 161
  • 1
  • 4
3

You can use expect scripts instaed of bash. Below example show how to telnex into an embedded board having no password

#!/usr/bin/expect

set ip "<ip>"

spawn "/bin/bash"
send "telnet $ip\r"
expect "'^]'."
send "\r"
expect "#"
sleep 2

send "ls\r"
expect "#"

sleep 2
send -- "^]\r"
expect "telnet>"
send  "quit\r"
expect eof
A R
  • 2,697
  • 3
  • 21
  • 38
3

The answer by @thiton was helpful but I wanted to avoid the sleep command. Also telnet didn't exit the interactive mode, so my script got stuck.

I solved that by sending telnet command with curl (which seems to wait for the response) and by explicitly telling telnet to quit like this:

curl telnet://10.1.1.1:23 <<EOF
remotecommand 1
remotecommand 2
quit
EOF
kapex
  • 28,903
  • 6
  • 107
  • 121
  • Whether the remote server requires `quit` or `exit` or something else depends entirely on what's at the other end. The command gets sent to the remote server, not to the `telnet` client as such (other than as input which it outputs to the other end). Perhaps you can edit to clarify that? – tripleee Jun 02 '21 at 15:18
  • @tripleee Thanks that's good to know. I was actually surprised that `quit` worked without using the `^]` escape, when I tried it in the `telnet` client. I'll clarify that in the answer later. Do you know if it's possible to quit from the client side in a script, when using `telnet` (or `curl`), without relying on the server behaviour? – kapex Jun 03 '21 at 01:22
  • 1
    Actually I would be very surprised if `curl` obeyed the same escape mechanism as traditional `telnet`, and based on brief experimentation, it seems it indeed doesn't. https://stackoverflow.com/questions/42347271/terminate-curl-telnet-session-in-bashscript suggests using a timeout; the manual does not mention anything for out-of-band commands. – tripleee Jun 03 '21 at 04:47
2

Following is working for me... put all of your IPs you want to telnet in IP_sheet.txt

while true
read a
do
{
    sleep 3
    echo df -kh
    sleep 3
    echo exit
} | telnet $a
done<IP_sheet.txt
iBug
  • 35,554
  • 7
  • 89
  • 134
1
#!/bin/bash
ping_count="4"
avg_max_limit="1500"
router="sagemcom-fast-2804-v2"
adress="192.168.1.1"
user="admin"
pass="admin"

VAR=$(
expect -c " 
        set timeout 3
        spawn telnet "$adress"
        expect \"Login:\" 
        send \"$user\n\"
        expect \"Password:\"
        send \"$pass\n\"
        expect \"commands.\"
        send \"ping ya.ru -c $ping_count\n\"
        set timeout 9
        expect \"transmitted\"
        send \"exit\"
        ")

count_ping=$(echo "$VAR" | grep packets | cut -c 1)
avg_ms=$(echo "$VAR" | grep round-trip | cut -d '/' -f 4 | cut -d '.' -f 1)

echo "1_____ping___$count_ping|||____$avg_ms"
echo "$VAR"
shaman888
  • 283
  • 1
  • 10
0

Here is how to use telnet in bash shell/expect

#!/usr/bin/expect
# just do a chmod 755 one the script
# ./YOUR_SCRIPT_NAME.sh $YOUHOST $PORT
# if you get "Escape character is '^]'" as the output it means got connected otherwise it has failed

set ip [lindex $argv 0]
set port [lindex $argv 1]

set timeout 5
spawn telnet $ip $port
expect "'^]'."
grepit
  • 21,260
  • 6
  • 105
  • 81
0

Script for obtain version of CISCO-servers:

#!/bin/sh

servers='
192.168.34.1
192.168.34.3
192.168.34.2
192.168.34.3
'
user='cisco_login'
pass='cisco_password'

show_version() {
host=$1
expect << EOF
set timeout 20
set host $host
set user $user
set pass $pass
spawn telnet $host
expect "Username:"
send "$user\r"
expect "Password:"
send "$pass\r"
expect -re ".*#"
send "show version\r"
expect -re ".*-More-.*"
send " "
expect -re ".*#"
send "exit\r"
EOF
}

for ip in $servers; do
  echo '---------------------------------------------'
  echo "$ip"
  show_version $ip | grep -A3 'SW Version'
done
AnselmRu
  • 51
  • 2
0

Here is a solution that will work with a list of extenders. This only requires bash - some of the answers above require expect and you may not be able to count on expect being installed.

#!/bin/bash

declare -a  Extenders=("192.168.1.48" "192.168.1.50" "192.168.1.51") 
# "192.168.1.52" "192.168.1.56" "192.168.1.58" "192.168.1.59" "192.168.1.143")
sleep 5
# Iterate the string array using for loop
for val in ${Extenders[@]}; do
   { sleep 0.2; echo "root"; sleep 0.2; echo "ls"; sleep 0.2; }  | telnet $val
done
dejanualex
  • 3,872
  • 6
  • 22
  • 37
wayner
  • 1
0

Use ssh for that purpose. Generate keys without using a password and place it to .authorized_keys at the remote machine. Create the script to be run remotely, copy it to the other machine and then just run it remotely using ssh.

I used this approach many times with a big success. Also note that it is much more secure than telnet.

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • 1
    `ssh` is surely more secure than `telnet`, but some IP devices just do not provide `ssh` service and rely on `telnet` protocol for using the device. – fduff Nov 06 '13 at 08:25
  • 2
    telnet (the program) can be used as a client for ANY (or more practically MOST) text-based client/server connections. It can be used for easy testing, for example, HTTP and IMAP. @Tomas' suggestion is to replace the use of Telnet (the remote login service) with SSH. whilst his comment is true, it is probably not what the OP was requiring. – Rob Shepherd Jan 11 '14 at 14:26
  • Rob, telnet can be indeed used to connect to any port, but see the OPs question - in his context he wants to use telnet only for *login* (citing: *"execute some commands on the remote system"*). Using telnet for this purpose is very risky. – Tomas Jan 11 '14 at 14:31
  • This is not an answer to the question which specifically says 'automating some telnet related tasks' using his existing scripts. – FractalSpace Dec 06 '18 at 20:40
-4

Play with tcpdump or wireshark and see what commands are sent to the server itself

Try this

printf (printf "$username\r\n$password\r\nwhoami\r\nexit\r\n") | ncat $target 23

Some servers require a delay with the password as it does not hold lines on the stack

printf (printf "$username\r\n";sleep 1;printf "$password\r\nwhoami\r\nexit\r\n") | ncat $target 23**
str8
  • 242
  • 2
  • 4
  • 9
    Newness is no excuse for rudeness. Please see the FAQ http://stackoverflow.com/faq. – Verbeia Mar 07 '12 at 02:07
  • 1
    wasn't meant to be insulting, more like... i dont know, apologies. I just keep seeing people using expect command as a solution. I just want people to realize there is a better solution to understanding what you are trying to accomplish. The expect command doesn't show you how its working, its just a workaround for not knowing. It is more confusing then not. When in doubt go to the source. If the source is not available, and if its a network protocol, tcpdump,wireshark,snoop the packets and follow the command flow and you should be able to put something together with working knowledge. – str8 Mar 07 '12 at 02:50
  • 7
    If you say so - I'm a Mathematica programmer so the substance of your answer and comment is a mystery to me. But I am not alone in interpreting the word "idiots" to be intentionally insulting. I think you need to spend a bit more time on the site and see how people behave here. – Verbeia Mar 07 '12 at 03:14