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.
- › How Much Does It Cost to Operate an Electric Lawn Mower?
- › NZXT Signal 4K30 Capture Card Review: Lossless High-Quality Footage
- › A World Without Wires: 25 Years of Wi-Fi
- › T-Mobile Is Selling Your App Activity: Here’s How to Opt Out
- › What’s New in Windows 11’s 22H2 Update: Top 10 New Features
- › What Are the Best Nintendo Switch Games in 2022?