yungcup
yungcup

Reputation: 13

Error during login per SSH from python script in Docker

I try to run my python script, which uses pexpect library to login to remote machine by ssh. The script is to be run from the Docker container.

Python script:

from pexpect import pxssh
import os


def main():
    username = os.environ.get("USERNAME")
    password = os.environ.get("PASSWORD")
    hostname = os.environ.get("HOSTNAME")
    
    client = pxssh.pxssh()
    client.login(hostname, username=username, password=password)
    print("Login successful")


if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print(e)

Dockerfile:

FROM python:3.10.12-slim-bookworm
RUN apt-get update
RUN apt-get install -y --no-install-recommends build-essential gcc

WORKDIR /app

RUN python3 -m venv /app/venv
ENV PATH="/app/venv/bin:$PATH"

RUN pip install --upgrade pip --no-cache

ADD requirements.txt requirements.txt
RUN pip3 install -r requirements.txt --no-cache

ADD ./src ./src


ENTRYPOINT ["python3", "src/connect.py"]

Build image: docker build -t connect .

Run container docker run --env-file .env connect

When I run my script locally I got success login, but when I try using docker I got error: The command was not found or was not executable: ssh.

Anyone know how to solve this problem?

Upvotes: 0

Views: 271

Answers (1)

0xdnL
0xdnL

Reputation: 393

I'm not familiar with pxssh, but looking at the docs it states

pxssh is a screen-scraping wrapper around the SSH command on your system.

Your base image python:3.10.12-slim-bookworm does not contain the ssh command. Therefore you should add ssh at the end of RUN apt-get install ... ssh and install it. That should solve:

The command was not found or was not executable: ssh

Upvotes: 0

Related Questions