Running a Bash script over ssh

bash script ssh and run command
ssh execute remote command with password
shell script to connect to remote server with password and execute commands
ssh run local script remotely
ssh to multiple servers and run commands
run a shell script ssh
execute remote script via ssh
ssh execute bash script remotely

I'm trying to write a Bash script that will SSH into a machine and create a directory. The long-term goal is a bit more complicated, but for now I'm starting simple. However, as simple as it is, I can't quite seem to get it. Here's my code:

#!/bin/bash
ssh -T tunneluser@111.222.333.444 <<EOI

# Fix "TERM environment variable undefined" error.
TERM=dumb
export TERM

# Store todays date.
NOW=$(date +"%F")
echo $NOW

# Store backup path.
BACKUP="/backup/$NOW"
[ ! -d $BACKUP ] && mkdir -p ${BACKUP}
echo $BACKUP

exit
EOI

It runs without any explicit errors. However, the echoed $NOW and $BACKUP variables appear empty, and the /backup directory is not created. How do I fix this?

The shell on the local host is doing variable substitution on $NOW and $BACKUP because the "EOI" isn't escaped. Replace

 ssh tunneluser@111.222.333.444 <<EOI

with

 ssh tunneluser@111.222.333.444 <<\EOI

SSH: Execute Remote Command or Script - Linux, How to execute remote command, multiple commands or shell (Bash) script over SSH (Secure Shell). Examples of SSH command in Linux  There are a lot of different ways of how it can be done, but i will show the most popular of them. Run multiple command on a remote host over SSH: $ ssh USER@HOST 'COMMAND1; COMMAND2; COMMAND3'. – or –. $ ssh USER@HOST 'COMMAND1 | COMMAND2 | COMMAND3'. – or –. $ ssh USER@HOST << EOF COMMAND1 COMMAND2 COMMAND3 EOF.

The variables are being evaluated in the script on the local machine. You need to subsitute the dollar signs with escaped dollar signs.

#!/bin/bash
ssh -T tunneluser@111.222.333.444 <<EOI

# Fix "TERM environment variable undefined" error.
TERM=dumb
export TERM

# Store todays date.
NOW=\$(date +"%F")
echo \$NOW

# Store backup path.
BACKUP="/backup/\$NOW"
[ ! -d \$BACKUP ] && mkdir -p \${BACKUP}
echo \$BACKUP

exit
EOI

How do I run a local bash script on remote machines via ssh , You can pass a script and have it execute ephemerally by piping it in and executing a shell. e.g. echo "ls -l; echo 'Hello World'" | ssh  One way you can do it is to write the function in the ssh command. Building on the example above to add the public key to the remote server, here is a script I wrote that will do it for an array of servers (all assuming they have the same username you use on the local machine).

Your script is doing substitution on the local host before being sent over.

Change your first line to:

ssh -T tunneluser@111.222.333.444 <<'EOI'

This will cause the raw script to get sent over and interpreted on your remote host.

If you wanted a mix (so for example, if you wanted the date command executed on your local host, you should leave ssh line unchanged and quote the individual command):

ssh -T tunneluser@111.222.333.444 <<EOI

# Execute the date command on the local machine.  The assignment still
# happens on the remote machine
NOW=$(date +"%F")

# Quote your $ so that the replacement happens on the remote machine
echo \$NOW

How To Run Multiple SSH Command On Remote Machine And Exit , Running local bash script on remote system. Create a shell script as follows: #!/​bin/bash # Name: test.sh  Put the commands that you need to execute in a script, transfer that script over to the host using scp and run it through ssh. Or, feed the here-document to the appropriate shell: ssh server sh <<'END_SCRIPT' script code goes here END_SCRIPT Or, use Ansible or similar system which is made for remote configuration of multiple hosts.

A few ways to execute commands remotely using SSH · zaiste.net, A few ways to execute commands remotely using SSH. ssh $HOST ls; pwd; cat /path/to/remote/file VAR1="Variable 1" ssh $HOST bash -c "' ls pwd if true; then echo $VAR1 else echo Multi-line command from local script. Make script executable and run it on remote server as follows: $ chmod +x system-info.sh $ ssh [email protected] ./system-info.sh As some of you might have guessed, it will generate below output:

Try:

NOW=`date +"%F"`

How To Run Shell Script or Command On Remote With SSH , Yes there are a lot of options. But the most simple and secure way is running scripts or commands over ssh. Run Command On Remote System. Using SSH with the bash Command As mentioned above, in order to parse the local variable HELLO so it is used in the remote if statement, the bash command is used: #!/bin/bash HELLO="world" ssh $HOST bash -c "' ls pwd if true; then echo $HELLO else echo "This is false" fi echo "Hello world" '"

How to Execute Linux Commands on Remote System over SSH, We just have to provide absolute path of local script to SSH command. Let us create a simple shell script with following contents and name it as  I'm trying to write a shell script that does a lengthy batch job on a remote server. I'll be running the script over SSH. The thing is, I intend to start the script in the evening and collect the results the next morning; I'd prefer not to have my local computer have to run all night, as it's not needed as part of the batch process.

How can I execute local script on remote machine and include , You can pacify bash by terminating it from taking any of the remaining command line arguments for itself using the -- argument. Like this: $ ssh root@​remoteServer  Say I have the following Bash script stored in the file foo.sh: #!/bin/bash echo foo Without having to scp the file, how could I execute the script stored in foo.sh on a remote machine? I have tried the following (with a few variations) to no success: $ ssh root@remote eval `cat foo.sh` eval `cat foo.sh`seems to expand to eval #!/bin/bash echo

Bash Script SSH: How to Use It| DiskInternals, 1. Example of executing Bash SSH command. To execute a Bash SSH command on a remote host over SSH, follow the example below:. How To Run Multiple SSH Command. Run date and hostname commands: $ ssh user@host "date && hostname" You can run sudo command as follows on a remote box called server1.cyberciti.biz: $ ssh -t vivek@server1.cyberciti.biz "sudo /sbin/shutdown -h now" And, finally: $ ssh root@server1.cyberciti.biz "sync && sync && /sbin/shutdown -h now" Bash here

Comments
  • Thanks. This required the fewest changes, and works perfectly.
  • Do either of you have an example that allows variables passed into the ssh session? For instance, if I had the block @Cerin used earlier in a bash script and before the ssh ... I declare REMOTE_PATH=root/stuff/buildpath , can I use $REMOTE_PATH or the like inside my ssh / heredoc ?
  • @Brian, Using hbar's example below, if you just don't escape $REMOTE_PATH, then it should be inserted into the ssh script ran on the remote server.
  • In addition to \, we can also use single quotes to wrap the LimitString, in this case EOI to escape special characters in here documents, for example ssh tunneluser@111.222.333.444 <<'EOI'.
  • Perfect :) :) Explanation ... I think this makes rest of the discussion pretty useless ..
  • Your second example quotes the here-doc delimiter and the variable. I think you intended to only escape the variable. Otherwise +1
  • What's with all the -es? POSIX doesn't require echo -e to do anything but print -e on output; in fact, doing anything else is explicitly noncompliant, and bash can be configured at either compile-time or runtime to comply with the letter of the standard, so behavior of echo -e is undefined and widely variable. (If your /bin/sh is provided by dash instead of bash, you'll get the POSIX-compliant behavior, so output will have a bunch of -e strings in it with this script written as it is).
  • Consider also running your code through shellcheck.net and fixing the quoting issues it identifies.
  • $() is the same as `` except the former easily allows nesting. This is something any POSIX-compliant sh can handle.
  • @GreenMatt, not just "newer bash versions", all shells compliant with the 1992 POSIX sh standard.
  • @GreenMatt, sure, but you were speaking about bash specifically, so we're not talking about Bourne here. And I'm not aware of any released version of bash that lacked $(...); it was certainly there in 2.0, and isn't listed as a change from 1.x either, making it presumptively something that wasn't new even then.