The netcat
tool is simply a network connection tool. It can listen on any port and can connect to any port. It can output that information or even have data piped to it. It is a very useful too. Unfortunately, it only listens and sends information; it does not execute any commands itself. Therefore, netcat
is not the tool for what you are wanting to do. The same is true of telnet.
In order to run commands on a remote server, you would need some software that is running on that remote server which would accept those commands. The tool to use for executing commands on a remote server is SSH. The listening server would be running the SSHD daemon. You could go through the trouble of trying to find a way to hack into the server some other way, but since you have access to that device already, that would really be more trouble than it would be worth since SSH already exists.
You say that your application cannot connect to SSH, but if it has access to netcat
, then I would recommend using whatever access that is to gain access to SSH, and execute your commands from there.
If the way that you are connecting to netcat
is that you have it listening on a port and the only thing your application can do is to connect to that port, then what you are wanting is to make some sort of an API, or some software which can handle requests over a port and use them to execute commands on the terminal through SSH. I found this article with some suggestions for doing so in BASH:
https://unix.stackexchange.com/questions/314550/how-to-set-a-script-to-execute-when-a-port-receives-a-message
Once you are able to get the request into a shell script, you can either make that script execute your commands directly, or you could write additional scripts which are triggered by the listening software. I would have this software listening on the client, rather than on the remote server, and you could access it over localhost (127.0.0.1). Also, remember to add an SSH key so that your connection to the remote server would not require any passwords to be used. This article may help with that:
https://docs.joyent.com/public-cloud/getting-started/ssh-keys/generating-an-ssh-key-manually/manually-generating-your-ssh-key-in-mac-os-x