1

I am using paramiko python package to run kubectl commands on remote server. My target to send current file to kubernetes pod which runs on external server only. But exec_command not doing this for me. Is it I am trying in wrong way? :

import os
import paramiko
ssh = SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('server', port, userid, password)
client.exec_command("kubectl config use-context namespace")
client.exec_command("kubectl scale statefulsets rbatch --replicas=1")
client.exec_command("kubectl cp openings.csv namespace/rbatch-0:/srv/batch/source")

I appreciate if anyone can guide me

Alex W
  • 37,233
  • 13
  • 109
  • 109
Sanjay Chintha
  • 326
  • 1
  • 4
  • 21
  • Are you using the string `namespace` in your code or are you using the actual namespace you're trying to use? – Alex W Mar 02 '20 at 13:53
  • @AlexW I am using actual kubectl namespace and that command its working fine and able to scale rbatch pod, but ssh and scp exec_command not working for file transfer. – Sanjay Chintha Mar 02 '20 at 13:58
  • 1
    What did you to to debug the problem? Did you try reading the command (error) output? – Martin Prikryl Mar 02 '20 at 14:11
  • Do you know the working directory of SSH? You might want to try `./openings.csv`. The `exec_command` returns a 3-tuple with the stdin, stdout, stderr, are you getting any messages in stdout or stderr that would help debug? – Alex W Mar 02 '20 at 14:24
  • @AlexW When referring to data files, `openings.csv` and `./openings.csv` are the same. The `./` makes difference when executing files only. – Martin Prikryl Mar 02 '20 at 14:37
  • Really what I meant was that I would try to use a fully-qualified file name but yes if the working directory already has the openings.csv file it would make no difference – Alex W Mar 02 '20 at 14:45
  • 1
    does the file `openings.csv` live on the local server or the remote one? – Ohmen Mar 02 '20 at 15:38
  • @Ohmen its local one – Sanjay Chintha Mar 05 '20 at 07:19
  • @AlexW I have tried in "./openings" as well. – Sanjay Chintha Mar 05 '20 at 07:20
  • You have to copy your local file to the remote server first before you can use it on the remote server inside the SSH session. You could use something like `rsync` for this task. – Ohmen Mar 06 '20 at 17:02

0 Answers0