The problem with running commands over SSH is that generally you either have to type them yourself or upload a script file. However, with a bit of bash knowledge, you can pass entire scripts over SSH without having the
.sh file on the remote machine.
The Solution: Pass The Script Over Standard Input
The SSH command has a mode where you can run any single command on a remote server. In order to run multiple commands, you’ll have to use the following hack:
ssh user@remotehost 'bash -s' < script.sh
bash -s command means “execute the following commands in a new bash session.” The
-s flag makes it read from standard input, and the
< script.sh bit will read a local script file into standard input.
The file is read entirely locally, and all gets sent to the remote server without uploading anything. This does require you to put all the commands into a separate script file.
Running Many Remote Commands Inside a Script
If you instead want to run a portion of a shell script on another server, but not the entire thing, you can include nested blocks like the following in your script:
ssh user@remotehost 'bash -s' <<'ENDSSH' # The following commands run on the remote host echo "test" cd /home/ pwd ENDSSH
This works because
bash -s is expecting any kind of standard input. The
<<'ENDSSH' directive makes a “here-document” structure, basically passing all characters between it and the ending “ENDSSH” to standard input, and hence to the remote host over SSH.
Doing it this way means you can keep everything in one script file, rather than making a new one to run on the remote.
- › It’s Okay to Skimp on These 10 Tech Products
- › JBL Live Free 2 Review: Great Noise Cancelling, Decent Sound
- › Android 13 Is Out: What’s New, and When You’ll Get It
- › How to Add Winamp Visualizations to Spotify, YouTube, and More
- › 6 Things Slowing Down Your Wi-Fi (And What to Do About Them)
- › 10 Hidden Android 13 Features You Might Have Missed