0

I can't understand. I successfully login on my remote host with ssh during job and i want to be sure that I can run commands at this host. So I have job like this:

Deliver image to host:
  stage: deploy
  before_script:
    # Setup SSH deploy keys
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - mkdir -p ~/.ssh
    - echo "$PRIVATE_SSH" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 700 ~/.ssh/id_rsa

  script:
    - ssh -o StrictHostKeyChecking=no <user>@<ip>
    - ls

And ls give me output of gitlab runner machine: i see my repository, not items of remote machine.
Why? How should I run commands on remote?

Archirk
  • 427
  • 7
  • 25

1 Answers1

1

I think your problem is here:

    - ssh -o StrictHostKeyChecking=no <user>@<ip>
    - ls

The first command runs ssh on the gitlab runner. ssh isn't interactive at this point (no keyboard is connected to the ssh session here). So ssh exits. Then you run ls on the gitlab runner, which gives you the strange output you are surprised by.

You want to tell the ssh command all of what you want it to do, by providing that extra thing as an argument. So try:

    - ssh -o StrictHostKeyChecking=no <user>@<ip> ls

See other answers like How to execute a remote command over ssh with arguments? and How do I pass arbitrary arguments to a command executed over SSH? for more information on how to exactly provide the commands and arguments to the ssh invocation.

omajid
  • 14,165
  • 4
  • 47
  • 64
  • thank you. Is this the only way: or there is a way to open kind of session? – Archirk Sep 01 '21 at 19:25
  • 1
    See the linked answers for some other techniques, but I find it sanity-preserving to stick to `ssh command` pattern. `command` can be a script, so you can put everything complex in a bash script, and then execute it via one `ssh` command. – omajid Sep 01 '21 at 19:30