Evan Rosica
Evan Rosica

Reputation: 1242

Bash Script Quits After Exiting SSH

I'm trying to write a Bash script that logs into 2 different linux based power-strips (Ubiquiti Mpower Pros) and turns 2 different lights off (one on each strip). To do this I login to the 1st strip, change the appropriate file to 0 (thus turning off the light), and exit, repeating the same process on the next power-strip. However, after I exit the first SSH connection, the script stops working. Could someone please suggest a fix? My only idea would be to encase this script in a python program. Here's my code:

#!/bin/bash
ssh [email protected]
echo "0" > /proc/power/relay1
exit
# hits the enter key
cat <(echo "") | <command>

ssh [email protected]
echo "logged in"
echo "0" > /proc/power/relay1
exit
cat <(echo "") | <command>

Upvotes: 0

Views: 1306

Answers (2)

Eric Renouf
Eric Renouf

Reputation: 14490

The commands you're apparently trying to run through ssh are actually being executed locally. You can just pass the command you want to run to ssh and it will do it (without needing an explicit exit)

ssh [email protected] 'echo "0" > /proc/power/relay1'

will do that, and similar for the other ssh command

Upvotes: 2

Marc B
Marc B

Reputation: 360572

ssh as an app BLOCKS while it's running, the echo and exit are executed by the local shell, not by the remote machine. so you are doing:

  1. ssh to remote machine
  2. exit remote shell
  3. echo locally
  4. exit locally

and boom, your script is dead. If that echo/exit is supposed to be run on the remote system, then you should be doing:

ssh user@host command
               ^^^^^---executed on the remote machine

e.g.

ssh foo@bar 'echo ... ; exit'

Upvotes: 4

Related Questions