Run a local script on a remote host

Overview

Run bash and python scripts remotely, without modification, without copying files.

Details

I want to run such a script

sample.sh


#!/bin/bash

for arg in init ${@};do
    echo "$(date +%Y-%m-%dT%H:%M:%S) $(hostname) ${arg}"
    sleep 1
done
$ ./sample.sh a b c
2015-12-22T11:30:26 local-host init
2015-12-22T11:30:27 local-host a
2015-12-22T11:30:28 local-host b
2015-12-22T11:30:29 local-host c

Basic idea

1. With ssh, you can submit a script to stdin of a remote shell.

$ cat sample.sh | ssh user@remote-host
2015-12-22T11:31:43 remote-host init

2. bash can receive the contents of stdin as a file

$ cat sample.sh | bash <(cat -) a b c
2015-12-22T11:33:47 local-host init
2015-12-22T11:33:48 local-host a
2015-12-22T11:33:49 local-host b
2015-12-22T11:33:50 local-host c

3. With the here document, you can run the script without creating a file

$ cat << 'EOT' | bash <(cat -) a b c
> for arg in init ${@};do
>     echo "$(date +%Y-%m-%dT%H:%M:%S) $(hostname) ${arg}"
>     sleep 1
> done
> EOT
2015-12-22T11:40:17 local-host init
2015-12-22T11:40:18 local-host a
2015-12-22T11:40:19 local-host b
2015-12-22T11:40:20 local-host c

Implementation

Such a script

rdosh.sh


#!/bin/bash
echo -e "cat << 'EOT' | bash <(cat -) ${@:2}\n$(cat ${1}|sed -e 's/\\/\\\\/g')\nEOT"

Experiment

$ ./rdosh.sh sample.sh a b c | ssh user@remote-host
2015-12-22T11:42:48 remote-host init
2015-12-22T11:42:49 remote-host a
2015-12-22T11:42:50 remote-host b
2015-12-22T11:42:51 remote-host c

did it

python too

Such a script

sample.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import time
for arg in ["init"]+sys.argv[1:]:
    sys.stdout.write("{0} {1} {2}\n".format(time.strftime("%Y-%m-%dT%H:%M:%S"),os.uname()[1],arg))
    sys.stdout.flush()
    time.sleep(1)

rdopy.sh


#!/bin/bash
echo -e "cat << 'EOT' | python <(cat -) ${@:2}\n$(cat ${1}|sed -e 's/\\/\\\\/g')\nEOT"

python also worked

$ ./sample.py a b c
2015-12-22T13:20:29 local-host init
2015-12-22T13:20:30 local-host a
2015-12-22T13:20:31 local-host b
2015-12-22T13:20:32 local-host c
$ ./rdopy sample.py a b c | ssh user@remote-host
2015-12-22T13:20:46 remote-host init
2015-12-22T13:20:47 remote-host a
2015-12-22T13:20:48 remote-host b
2015-12-22T13:20:49 remote-host c

Recommended Posts

Run a local script on a remote host
Run Jupyter notebook on a remote server
Run a multi-line script in a PDB
Run a Linux server on GCP
Run Matplotlib on a Docker container
Run headless-chrome on a Debian-based image
Run TensorFlow2 on a VPS server
Run the Python interpreter in a script
Run Python code on A2019 Community Edition
Use Django from a local Python script
Let's run a Bash script in Java
Run matplotlib on a Windows Docker container
How to run a Maya Python script
Run a Python file inside a Docker container on a remote Raspbian via PyCharm
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
Run a Python script from a C # GUI application
Build a LAMP environment on your local Docker
Periodically run a python program on AWS Lambda
Run a python script from excel (using xlwings)
[Grasshopper] When creating a data tree on Python script
Build a local development environment for Laravel6.X on Mac
A memorandum to run a python script in a bat file
How to run Django on IIS on a Windows server
Run Django on PythonAnywhere
Run mysqlclient on Lambda
Run OpenMVG on Mac
Create a new csv with pandas based on the local csv
How to run a trained transformer model locally on CloudTPU
How to run a Python program from within a shell script
Run a limited number of image presentation programs on PsychoPy
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
Run the output code on the local web server as "A, pretending to be B" in python