A trick I use sometimes is to use base64 to encode the commands, and pipe it to bash on the other site:
MYCOMMAND=$(base64 -w0 script.sh)
ssh user@remotehost "echo $MYCOMMAND | base64 -d | sudo bash"
This will encode the script, with any commas, backslashes, quotes and variables inside a safe string, and send it to the other server. (-w0
is required to disable line wrapping, which happens at column 76 by default). On the other side, $(base64 -d)
will decode the script and feed it to bash to be executed.
I never got any problem with it, no matter how complex the script was. Solves the problem with escaping, because you don’t need to escape anything. It does not creates a file on the remote host, and you can run vastly complicated scripts with ease.
Related Posts:
- Copying a local file from Windows to a remote server using scp
- ssh-agent forwarding and sudo to another user
- How can I fully log all bash scripts actions?
- How do I split a string on a delimiter in Bash?
- How do I tell if a regular file does not exist in Bash?
- Git: How to solve Permission denied (publickey) error when using Git?
- Why do you need to put #!/bin/bash at the beginning of a script file?
- How do I parse command line arguments in Bash?
- Putty: Getting Server refused our key Error
- ssh: The authenticity of host ‘hostname’ can’t be established
- ssh “permissions are too open” error
- How can I declare and use Boolean variables in a shell script?
- In a Bash script, how can I exit the entire script if a certain condition occurs?
- How can I declare and use Boolean variables in a shell script?
- “[ ]” vs. “[[ ]]” in Bash shell
- How do I split a string on a delimiter in Bash?
- How does “cat << EOF" work in bash?
- Bash scripting missing ‘]’
- Pseudo-terminal will not be allocated because stdin is not a terminal
- How do I compare two string variables in an ‘if’ statement in Bash?
- Permission denied (publickey,keyboard-interactive)
- How to deal with “Pseudo-terminal will not be allocated because stdin is not a terminal.”
- What is actually in known_hosts?
- Pseudo-terminal will not be allocated because stdin is not a terminal
- ‘heroku’ does not appear to be a git repository
- How to solve “sign_and_send_pubkey: signing failed: agent refused operation”?
- How to fix request failed on channel 0
- How to scp in Python?
- Forward X11 failed: Network error: Connection refused
- Bash script: bad interpreter
- How to count lines in a document?
- What does `set -x` do?
- PuTTY PSCP error “Local to local copy not supported” when username contains a slash
- How to specify the private SSH-key to use when executing shell command on Git?
- Starting ssh-agent on Windows 10 fails: “unable to start ssh-agent service, error :1058”
- Git error: “Host Key Verification Failed” when connecting to remote repository
- Convert PEM to PPK file format
- Copying files using rsync from remote server to local machine
- How to read iPhone files without jailbreaking?
- SSH to Vagrant box in Windows?
- Shell script not running, command not found
- How to ssh from within a bash script?
- How to use Sublime over SSH
- Error handling in Bash
- Use qdel to delete all my jobs at once, not one at a time
- How do I know the script file name in a Bash script?
- Automating command/script execution using PuTTY
- Public and Private Keys are Incorrect for user
- How to determine if a bash variable is empty?
- How to automate SSH login with password?
- ssh returns “Bad owner or permissions on ~/.ssh/config”
- What is the difference between double and single square brackets in bash?
- How do I change my private key passphrase?
- Create a public SSH key from the private key?
- How do diff over ssh?
- “Add correct host key in known_hosts” / multiple ssh host keys per hostname?
- SSH use only my password, Ignore my ssh key, don’t prompt me for a passphrase
- Non interactive git clone (ssh fingerprint prompt) [duplicate]
- How to check sshd log?
- What does “Warning: untrusted X11 forwarding setup failed: xauth key data not generated” mean when ssh’ing with -X?
- What’s the difference between authorized_keys and authorized_keys2?
- What is “-bash: !”: event not found”
- Keeping a linux process running after I logout
- Is my password compromised because I forgot to hit Enter after ssh username?
- How do I make ssh fail rather than prompt for a password if the public-key authentication fails?
- ssh-keygen does not create RSA private key
- OpenSSH: Difference between internal-sftp and sftp-server
- What significance does the user/host at the end of an SSH public key file hold?
- SSH Suddenly returning Invalid format
- How can I prevent the warning No xauth data; using fake authentication data for X11 forwarding?
- How to recover from “Too many Authentication Failures for user root”
- SFTP logging: is there a way?
- Why does my OpenSSH key fingerprint not match the AWS EC2 console keypair fingerprint?
- Can you have more than one ~/.ssh/config file?
- Login without running bash_profile or bashrc
- How to make bash scripts print out every command before it executes?
- How do I do Multihop SCP transfers between machines?
- SSH from A through B to C, using private key on B [closed]
- How can I edit the welcome message when ssh start?
- scp without known_hosts check
- How do I validate an RSA SSH public key file (id_rsa.pub)?
- What does passing the -xe parameters to /bin/bash do
- Add comment to existing SSH public key
- Log all commands run by admins on production servers
- ssh connection takes forever to initiate, stuck at “pledge: network”
- What is the benefit of not allocating a terminal in ssh?
- bad ownership or modes for chroot directory component
- Why Block Port 22 Outbound?
- Ansible stuck on gathering facts
- Is it possible to use rsync over sftp (without an ssh shell)?
- SSH Allow Password For One User, Rest Only Allow Public Keys [duplicate]
- “Couldn’t find a file descriptor referring to the console” on Ubuntu bash on Windows
- Could not open a connection to your authentication agent
- Bash script and /bin/bash^M: bad interpreter: No such file or directory [duplicate]
- Passing parameters to a Bash function
- Shell: How to call one shell script from another shell script?
- mvn command not found in OSX Mavrerick
- How can I match a string with a regex in Bash?
- How to trim whitespace from a Bash variable?
- How to define hash tables in Bash?