Send commands via serial connection using plink from bash script and exit

171 views Asked by At

I'm writing a bash script on a Windows machine that enters a PetaLinux machine over a serial connection, performs some commands, and should exit back to the bash script to continue performing commands. I'm unable to exit back to the bash script so far.

Here is what I have tried:

#!/bin/bash

echo "Running board bring up script"

BOARD_A_SERIAL_PORT="COM22"

plink -serial "$BOARD_A_SERIAL_PORT" -sercfg 115200,8,n,1,X -batch <<EOF
root
root
sleep 2
ps aux | grep plink
ps aux | grep plink
killall -9 plink
ps aux | grep [p]link
exit
EOF

echo 'script successfully exited' #never reached. the serial terminal hangs
1

There are 1 answers

0
Martin Prikryl On

You are running the killall on the remote server. But you need to run it locally.

Here is an example how to achieve this in Windows batch file:
Send commands via COM port using plink and exit
It shows how to run the taskkill locally, while the plink in running. The key point is not to provide a static input via heredoc [<<EOF], but use a subshell [the ( )]. The subshell allows you to both produce the input using echo commands, but also to run the sleep and killall locally.

Translating that Widows batch file to Linux/bash tools and adjusting to your needs, you can do something like:

(
  echo root
  echo root
  echo your_command
  sleep 2
  killall -9 plink
) | plink -serial "$BOARD_A_SERIAL_PORT" -sercfg 115200,8,n,1,X -batch

echo 'script successfully exited'

Not tested. Plus, I'm not a bash expert, and I'm sure there's more elegant way to achieve this in bash.