Question

Automatically sending over files through telnet works fine. But how do I make telnet "wait" for a process which I started in this way, in this case nc that receives a file? The robot here is at 192.168.0.45 and my laptop is at 192.168.0.10.

{
sleep 1
echo "root"
echo "cd /flash"
echo "nc 192.168.0.10 10001 >lasertest"
sleep 5
} | telnet 192.168.0.45 23

There are a few questions like this [1], but non of them seem to want to have this synchronous behaviour. In the above the process waits for 5 seconds, just assuming that this is enough for the transmission. If it is not long enough, the file transmitted in this way will just be truncated and is corrupted.

[1] Telnet inside a shell script

Was it helpful?

Solution

An expect script should do.

#!/usr/bin/expect -f
spawn telnet 192.168.0.45 23
expect login
send "root\n"
send "cd /flash\n"
send "nc 192.168.0.10 10001 >lasertest\n"
send "exit\n"
expect eof

If the login prompt doesn't contain "login", change the "expect" command suitably.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top