0

I'm developing on a remote server which I login using ssh and develop using vi. I however need to send Terminal notification commands osascript -e "display notification {} {} {}" and such commands back to my local terminal so I can get sound/mac notifications on my system. How do I achieve this?

I know I can use import os; os.sytem('command') for the script on server to send terminal commands on the machine its running in i.e., the server itself, but is there a similar command to send commands back to my local ? Ideally, I need this to be done from the scripts itself- because I have multiple triggers for notifications to be done.

Deepak
  • 149
  • 1
  • 1
  • 11
  • How you are making connection between your local and server machine ? – DarkSuniuM Mar 10 '18 at 04:42
  • I use SSH to connect to the server – Deepak Mar 10 '18 at 04:47
  • I may not understood correctly, You are developing something on your server? and you need your script send some commands to your local machine? right? – DarkSuniuM Mar 10 '18 at 04:52
  • Yes, thats right – Deepak Mar 10 '18 at 05:07
  • Then u will need another script on your local machine to get the command, you need to use a little socket programming to communicate between server and local machine, you can send messages from your server to your local machine and then on your local machine decide about what you going to do if you received a message by "bla bla bla" content.. [ Sorry for bad English ] – DarkSuniuM Mar 10 '18 at 05:12
  • Ok, that seems fair. Can you make an answer with the steps required therein ? – Deepak Mar 10 '18 at 05:16

1 Answers1

0

You need to use some sockets

On your server machine you need something like this:

import socket


IP = "0.0.0.0"  # Your Local Machine IP
PORT = 5200  # Your Local Machine Listening Port

def send_message(msg):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((IP, PORT))
    s.send(bytes(msg, 'UTF-8'))
    data = s.recv(4096)
    s.close()
    print(data)

You can use the method where ever you need, the only argument it takes is msg, simply it's the command you need to send to your local machine

On your local machine this script should do what you need:

import socket
import os


IP = "0.0.0.0"  # 0.0.0.0 Means every available IP to assign, but you need to use your external IP on the script that u will use on server
PORT = 5200  # The port you want to listen on
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((IP, PORT))
s.listen(1)



while True:
    conn, addr = s.accept()
    print("Command from {}".format(addr))
    data = conn.recv(4096)
    if not data: continue
    if data == b'stop': break  # This line just defines a word that will make your local machine stop listening.
    print(data)
    command = data.decode('UTF-8')
    os.system(command)
    conn.send(data)

conn.close()

You need to run The local machine script first

DarkSuniuM
  • 2,523
  • 2
  • 26
  • 43
  • Hmm, its not working- I tried the exact same script. My local is a Mac- I went to Preferences > Network and found my IP there and for the server i did `ifconfig |grep inet`. My local script isn't able to catch anything and throws an error: `File "list.py", line 12, in s.bind((IP, PORT)) File "/Users/deepak/anaconda2/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 49] Can't assign requested address`. If I keep the IP as 0.0.0.0 in the local script, it just runs without anything happening. – Deepak Mar 10 '18 at 13:16
  • @Deepak You don't need to change IP on local machine script, you need to change it on server machine script – DarkSuniuM Mar 10 '18 at 13:39
  • No use mate, both script keep running- nothing happens. – Deepak Mar 12 '18 at 10:06