Is it possible to do the following by expect script?

I have Linux server that connected to linux_machine1 and linux_machine1 connected to linux_machine2

My Linux server have access only to linux_machine1 ( linux server can telnet to linux_machine1 but can’t telnet to linux_machine2 )

But from linux_machine1 I have access VIA telnet to linux_machine2

So my question is:

I need to develop expect script that perform telnet from the Linux server to linux_machine1

then expect will perform telnet to linux_machine2 from linux_machine1 and will copy the /etc/hosts file from linux_machine2 to my Linux server

Is this scenario can be implemented by expect script?

Short example that descript the process:

  ( run expect from Linux server ) --> linux_machine1 --> linux_machine2 
  Linux server          <--   /etc/hosts    from linux_machine2 <--
  • remark - expect script need to be activate from the linux server
  • 1
    I guess you already know telnet is insecure and don't know if you can switch to ssh, which is more secure and solves your problem. If you are able to switch to ssh you can create a tunnel and access machine 2 through machine 1 from your linux box with just one simple ssh command (I think the parameter you need is -J).
    – YoMismo
    Jan 11, 2019 at 12:19

1 Answer 1


From Linux_Server, run the below script.

Don't use telnet as it is unencrypted. You can rather use ssh and sftp as below.

#This part connects to Machine1
#Just change username, password_here and Linux_machine1 to suit your requirement. 

#!/usr/bin/expect -f
spawn telnet -l username linux_machine1
expect "*?assword:*"
send -- "password_here\r"
send -- "\r"
expect eof

#Now we are connected to Linux_Machine1 from where we will connect to Linux_machine2
#Just change username, password_here and Linux_machine2 to suit your requirement. 

spawn ftp username@machine2
expect "*?assword:*"
send -- "password_here\r"
send -- "\r"
expect eof
expect "ftp>"
send -- "get /etc/hosts\r"
expect "ftp>"
send "quit \r"

In your local folder of Linux_Server, a copy of /etc/hosts will be present.

P.S: Tested in local machine and works perfectly fine. If you still wish to use telnet, the above script will give you an idea on how to do it. You can modify the ssh and sftp connections to telnet as you desire.

P.P.S: I would really appreciate if you respond back with either bricks or compliments as I had spent good half an hour in preparing this script :) Ok, not half an hour, but definitely 15 minutes :)

  • I have a problem with your solution - linux_machine1/2 not have ssh , only telnet because they are very old linux machine , could you advice how to fit your script for telnet ? ( remark I cant install ssh on the ,machines )
    – user58412
    Apr 29, 2014 at 4:17
  • can I replace the ssh with telnet ?
    – user58412
    Apr 29, 2014 at 4:28
  • I have made some changes. However, I could not test it as I do not have telnet in my server. Let me know how it goes.
    – Ramesh
    Apr 29, 2014 at 4:28
  • ok I need to test it on my machine it will take time because I need to drive to my work , lets say 2 hours and I will give you feedbake
    – user58412
    Apr 29, 2014 at 4:29
  • Sure. It is night for me here. If there is more issues, I will look into it tomorrow. Good night :)
    – Ramesh
    Apr 29, 2014 at 4:30

You must log in to answer this question.