0

I am trying to write a shellscript to run on a linux server which will interact with a remote kubernetes cluster and get info about the certificates on the cluster. The linux server has connectivity to kubernetes nodes. I am able to SSH to the kubernetes node from the server and run the kubectl commands and get the proper output via terminal.

But, when I try the same via shellscript (ssh to kubernetes node and run kubectl get certs), the kubectl command gives an error "The connection to the server localhost:8080 was refused - did you specify the right host or port?"

Trying SSH from linux server terminal (working)

ssh user@server
sudo -i
kubectl get certs -o json

Trying the same in a shellscript(not working)

#!/bin/bash
ssh user@server <<"EOF"
sudo -i
Kubectl get certs -o json
EOF

The result of running the above shellscript is "The connection to the server localhost:8080 was refused - did you specify the right host or port? "

anky316
  • 21
  • 5
  • 1
    `localhost:8080` means that when you ran it on your own computer, it worked because you have opened that port on your own computer. We can't guess what this port was mapped to or why you expected it to work on a system where clearly this mapping had not been set up. Understand that `localhost` means "this computer". – tripleee Apr 27 '23 at 04:28
  • Your edit makes this (also) a duplicate of https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc which is weird because you managed to solve it properly for `ssh`, but not for `sudo -i`. (Using `sudo -i` to run a single command is silly anyway; just run `ssh user@server sudo kubectl get certs -o json`.) But the fact remains that `localhost` is clearly not listening on the port you specified; you need to connect to a different port somehow. – tripleee Apr 27 '23 at 05:12
  • @tripleee the localhost error occurs when kubectl is not able to connect with api server. There could be many reasons for that. The confusion here is that why it is working through terminal and not through shellscript. I am running kubectl after I ssh into the kubernetes node, so the kubectl is being ran on the kubernetes node in both cases. – anky316 Apr 27 '23 at 05:49
  • Do you have a `.ssh/config` which overrides the destination when you are logged in interactively? – tripleee Apr 27 '23 at 06:15
  • Please review the [help] and in particular [How to ask](/help/how-to-ask) as well as the guidance for providing a [mre]. Are all the layers required to reproduce the problem? If not, could you [edit] to peel off the parts which unnecessarily complicate the question, and add debugging information to show what you have done to investigate the problem? – tripleee Apr 27 '23 at 06:17
  • @tripleee Thank you for your suggestions. I will repost the questions so that it is more clear to the audience. – anky316 Apr 27 '23 at 14:22
  • Unfortunately, [your new question is not at all clearer](https://stackoverflow.com/questions/76123543/kubectl-command-giving-error-when-run-in-shellscript-on-a-remote-server) but has some additional feedback from other victims of your lack of clarity and focus. – tripleee Apr 28 '23 at 03:54
  • 1
    ... But [this comment](https://stackoverflow.com/questions/76123543/kubectl-command-giving-error-when-run-in-shellscript-on-a-remote-server#comment134249844_76123543) explains what's wrong; like I told you all along, this is a duplicate of https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc - you want `sudo kubectl`, not `sudo -i; kubectl` – tripleee Apr 28 '23 at 03:56
  • @tripleee, I have managed to resolve this issue. On the kubernetes node, I copied the admin.conf file from /etc/kubernetes to my user home directory. It allowed me to run kubectl as non root user. I removed sudo -i from the shellscript and it worked. Thank you for the help. I am an app developer and I don't have linux admin experience so I might have not explained by problem clearly – anky316 Apr 28 '23 at 18:35
  • So ultimately not a programming question really. You could solve this with `sudo` too; you just have to pass it the correct options to "really" log in to the root account (which IIRC the `-i` option does; but I have no way to reproduce your problem easily). – tripleee Apr 29 '23 at 09:33
  • Just to reiterate, the proposed duplicate explains the problem with your attempt in more detail. Did you read it? Did you try what I suggested? Anyway, going forward, this sort of question may be more suitable for our sibling site [unix.se] – tripleee Apr 29 '23 at 09:35
  • @tripleee yes, I read it and tried sudo kubectl as you suggested but it was not working, it must have something to do with how the node I am trying to run kubectl is configured. I will give it some more time and try to solve it with sudo. Thanks again for helping! – anky316 May 01 '23 at 14:20

0 Answers0