1

I try to use a bash script to watch the serial-port for a specific string. If this string is shown, some commands should be send back to the connected device. It´s a router I am connected to. It is a router I am connected to and I want to send the commands, after the uboot-menu has appeared.

When using minicom with a ftdi-USB-UART-device directly (no with the script) everything works. "It works" in that case means:

  • Bootmenu is shown while a time is counting down, the device is waiting for input until I enter a number or the countdown is over.
  • I am able to send a command/enter a number.
  • The router "boots" into the selected option. (in my case - the system starts tftp-bootmode where I could input stuff liker server IP and so on...)

Now, when I use the script. The router does not care about inpu also the countdown is not runnig. It just boots directly into the default option. Same behavior when using minicom + /dev/ttyAMA0 on a raspberry pi (ftdi device otherwise works as described).

I am thinking that in my script, there are some parameters missing. Maybe I have to choose special options for stty?

Or do I have to send a special "control character" right at the beginning, for the router to recognize that "there is someone out there who is able to send me some commands"?

I am able to detect the string I am searching for, but as I said before - the router doesn´t recognize the command(s) send:

#!/bin/bash
tty=/dev/ttyUSB0
exec 4<$tty 5>$tty
stty -F $tty 115200 -echo

while [ "${output}" != "Please choose the operation:" ]; do #wait for bootmenu to appear
    read output <&4 
    echo "$output"
done
printf "\t\n  ***** gotcha! *****  \n\n" #bootmenu is showing

echo -e "\x31" >&5 # echo '1' for taking boot option 1
printf "\t\n ***** echo '1' ***** \n\n"

while true; do #just for debugging (was command received?)...
    read output <&4
    echo "$output"
done

#commands for setting Target-IP, TFTP-Server-IP-address should follow here... 

Many thanks in advance :-)

4
  • I don't know if a shell can handle that. Wouldn't this be more in the line of a tool like "expect" ? Mar 26, 2020 at 15:05
  • Well, It handles the first part (watch for the string on the input) quite good. I am just struggling to get commands send. I think I have to initialize the tty in a special way or I have to send a "special character" before sending the original command, to tell the router that the shell should be interactive. I think the router must recognize if there is someone who is able to send commands, because when not - it just runs through without taking care about the delay. So there must be a way to tell the router that I am able to send commands - am I right?
    – some1
    Mar 26, 2020 at 16:21
  • "echo '1' for taking boot option 1", is that "1", or "1 Enter" ? Mar 26, 2020 at 16:33
  • echo will send a linefeed after the '1', but it's likely that the router is looking for a carriage return, or perhaps looking for both CR and LF. Try echo -e '1\x0d' (there's no need to write \x31 to represent the character 1) to introduce a CR before the automatic LF. If that works but following commands are messed up, try echo -n -e '1\x0d' to suppress the LF that echo sends by default. Mar 27, 2020 at 1:11

1 Answer 1

1

echo -n "1" >&5 did the trick for me. Seems like uboot doesn´t want to get a newline, otherwise it behaves weird.

Thanks a lot for your help!

Here is the "complete" script, maybe it is helpful for someone.

#!/bin/bash
tty=/dev/ttyUSB0
exec 4<$tty 5>$tty
stty -F $tty 115200 -brkint -icrnl -imaxbel iutf8 -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke

tftp_client_ip="10.10.30.111"
tftp_server_ip="10.10.30.1"
tftp_file="test.file"

while [ "$firstword" != "Please" ]; do # wait for bootmenu to apear
    read -e output <&4
    firstword=$(echo "$output" | cut -f1 -d" ")
    echo "$output"
done
#printf "\t\n  ***** ***** gotcha! ***** *****  \n\n"
sleep 0.5

# DONT SEND NEWLINEs - otherwise uboot doesn´t recognize commands !!!!!
echo -n "1" >&5 # echo '1' for taking boot option 1
printf "\t\n ***** ***** echo '1' ***** ***** \n\n"
# MUST TO WAIT FOR DELAY - try to fix that by "emulate" RETURN button?
sleep 5

#input TFTP-client IP
for((i=0;i<20;i++)); do
    echo -ne "\b \b" >&5 #erase characters
    sleep 0.05
done
printf  "%s\r" "$tftp_client_ip" >&5

#input TFTP-server IP
for((i=0;i<20;i++)); do
    echo -ne "\b \b" >&5 #erase characters
    sleep 0.05
done
printf  "%s\r" "$tftp_server_ip" >&5

#input TFTP-file
printf  "%s\r" "$tftp_file" >&5

while true; do #just for debugging...
    read -e output <&4
    echo "$output"
done

# router should boot to RAM with "$tftp_file"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.