Giter VIP home page Giter VIP logo

ssh-command-action's Introduction

What is this? / How to use?

License Latest Release

This is a GitHub Action designed to run commands on a remote server using SSH. It allows you to securely execute commands on a remote server from your GitHub workflow, making it ideal for deployment, server management, and other remote tasks.

Table of Contents

Features

  • Secure: Uses SSH with key-based authentication to securely execute commands on remote servers.
  • Flexible: Run any command supported by the shell on the remote server.
  • Easy Integration: Simple to include in your GitHub Actions workflow.

Usage

Action Example

Here's an action example of how to use this ssh-command-action.

Single Command Example:

    - name: Run remote command via SSH
      uses: D3rHase/ssh-command-action@latest
      with:
        host: ${{ secrets.HOST }}
        port: ${{ secrets.PORT }}
        user: ${{ secrets.USER }}
        private_key: ${{ secrets.PRIVATE_KEY }}
        host_fingerprint: ${{ secrets.HOST_FINGERPRINT }}
        command: echo 'Hello, World!'

Multiline Command Example:

    - name: Run multiple remote commands via SSH
      uses: D3rHase/ssh-command-action@latest
      with:
        host: ${{ secrets.HOST }}
        port: ${{ secrets.PORT }}
        user: ${{ secrets.USER }}
        private_key: ${{ secrets.PRIVATE_KEY }}
        host_fingerprint: ${{ secrets.HOST_FINGERPRINT }}
        command: |
          cd /path/to/your/directory
          git pull origin main
          npm install
          npm run build

Parameters

You can use plain text instead of the secrets for these values directly in your action, but it is highly recommended to use GitHub Secrets for sensitive information to ensure privacy and security. See Secrets Configuration.

  • host: The remote server address (IP or domain) - Required.
  • port: The port to connect to on the remote server - Default: 22.
  • user: The username for SSH access - Required.
  • private_key: The private SSH key to authenticate with the remote server - Required.
  • host_fingerprint: The public SSH key fingerprint of the remote server for verification - Optional.
  • command: The command to execute on the remote server - Required.

Secrets Configuration

To keep your credentials secure, store sensitive information like host, port, user, and private_key as GitHub Secrets. You can add these secrets in your repository's settings under Secrets and variables > Actions > Repository secrets.

Adding an SSH Key to Your Server

To use this action, you'll need to set up an SSH key on your server. Here's how to do it:

  1. Generate an SSH Key Pair on your local machine (if you don't have one already):

    ssh-keygen -t rsa -b 4096

    This command creates a new SSH key using the RSA algorithm with a 4096-bit key length.

  2. Add the SSH Key to the Server:

    Copy the public key (~/.ssh/id_rsa.pub) to your server using the ssh-copy-id command:

    ssh-copy-id user@your-server-ip

    Replace user with your server's username and your-server-ip with the IP address of your server. This command adds your public key to the ~/.ssh/authorized_keys file on the server.

  3. Test the SSH Connection:

    Verify that you can connect to your server using the SSH key:

    ssh user@your-server-ip
  4. Store the SSH Key in GitHub Secrets:

    Go to your repository on GitHub, navigate to Settings in your repository > Secrets and variables > Actions, and add a new repository secret named PRIVATE_KEY. Paste the contents of your private key (~/.ssh/id_rsa) into this secret.

    Note: Ensure your private key remains confidential. Do not share it publicly.

Getting the Host Fingerprint

To ensure you're connecting to the correct server and to prevent man-in-the-middle attacks, you can verify the server's host fingerprint. Here's how to obtain it:

  1. Connect to your server using SSH from your local machine:

    ssh user@your-server-ip
  2. Get the SSH host key fingerprint:

    After connecting, run the following command on your server:

    ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub

    Replace /etc/ssh/ssh_host_rsa_key.pub with the path to your server's SSH public key file if it's different.

  3. Copy the fingerprint displayed by the command. It should look something like this:

    2048 SHA256:ABC123def456ghi789... (RSA)
    
  4. Store the Host Fingerprint in GitHub Secrets:

    Go to your repository on GitHub, navigate to Settings in your repository > Secrets and variables > Actions, and add a new repository secret named HOST_FINGERPRINT. Paste the fingerprint into this secret.

How to implement in your workflow

This is an example of how you could use it in your GitHub workflow YAML file.

name: Example workflow file

on:
  push:
    branches:
      - main

jobs:
  remote-command:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Run remote command via SSH
      uses: D3rHase/ssh-command-action@latest
      with:
        host: ${{ secrets.HOST }}
        port: ${{ secrets.PORT }}
        user: ${{ secrets.USER }}
        private_key: ${{ secrets.PRIVATE_KEY }}
        host_fingerprint: ${{ secrets.HOST_FINGERPRINT }}
        command: echo 'Hello, World!'

    - name: Notify Command Success
      run: echo "Command executed on ${{ secrets.HOST }} successfully!"

License

This project is licensed under the MIT License. See the LICENSE file for more details.

ssh-command-action's People

Contributors

d3rhase avatar dependabot[bot] avatar emibergo02 avatar lingepumpe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ssh-command-action's Issues

Load key "/root/.ssh/id_rsa": invalid format

Hi, i just got this error.

here is my github actions

      - name: SSH and Deploy
        uses: D3rHase/[email protected]
        with:
          host: myhost.com
          user: myuser
          private_key: ${{ secrets.AWS_PEM }}
          command: ls -lah

and here is my secret key.

-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----

No ED25519 host key is known for *** and you have requested strict checking. Host key verification failed.

Full log:

Run D3rHase/[email protected]
  with:
    host: ***
    user: ***
    host_fingerprint: ***
    private_key: ***
    command: ***;
  
    port: 22
/usr/bin/docker run --name f820883cd8951e4cb454c9d620ee15663213a_6f254c --label 4f8208 --workdir /github/workspace --rm -e "INPUT_HOST" -e "INPUT_USER" -e "INPUT_HOST_FINGERPRINT" -e "INPUT_PRIVATE_KEY" -e "INPUT_COMMAND" -e "INPUT_PORT" -e "HOST" -e "PORT" -e "USER" -e "PRIVATE_KEY" -e "HOST_FINGERPRINT" -e "COMMAND" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY"
>> Public ssh fingerprint found, man-in-the-middle protection enabled.
No ED2551[9](https://github.com/Waterfall-IT/wit-site/actions/runs/8764218441/job/24053987315#step:4:9) host key is known for *** and you have requested strict checking.
Host key verification failed.

Appears to be connecting to incorrect host

Hi there,

Recently this action has stopped working for me with a Remote Host Identification warning. The fingerprint doesn't seem to have changed, and the error message reports a different fingerprint to the host that also seems to be the new GitHub fingerprint:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s.
Please contact your system administrator.
Add correct host key in /home/***/.ssh/known_hosts to get rid of this message.
Offending RSA key in /home/***/.ssh/known_hosts:1
RSA host key for github.com has changed and you have requested strict checking.
Host key verification failed.

Googling uNiVztksCsDhcc0u9e8BujQXVUpKZIDTMczCvj3tD2s. matches the fingerprint in quoted errors from when Github changed their RSA SSH host key earlier this year.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.