Instant message from console in jabber

    xmpp logoOften, system administrators have the task of notifying themselves and colleagues about any events on the server, whether it is a report on successful logins via ssh, a sharply increased load, a drop in service, a message about switching to backup power or a boiled kettle.
    Most often this problem is solved, for example, by sending a mail message. But it cannot be guaranteed that the message, firstly, will arrive on time, and secondly, that it will be read right away. Then, the administrator will think, we will use IM. But how? To keep, for example, centerim constantly open in screen? Agree, not the most rainbow option.
    A chip and a dale hurry to our rescueopen XMPP protocol. Many extensions to popular languages ​​and code examples have been written, allowing you to send a message to anyone you want, and send it quickly.
    I will give an example of such code.

    When I needed such functionality, I went over the existing solutions in such popular languages ​​as C, php, perl, python. Some suggested using pre-built classes, some using module loading. The code was monstrously bulky and unreadable, and I was looking for an elegant solution. And it was found in Python.

    So, let's agree that we work in Linux. In fact, the script will probably be able to work on Windows, but I didn’t ask myself such a goal. We will need, in fact, python itself, as well as two modules - any dns and xmpppywhich is introduced in Debian by the python-xmpp package .

    The script code is as follows:

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-

    import xmpp,sys

    xmpp_jid = 'noreply@some.jabber.server'
    xmpp_pwd = 'noreplypass'

    to = sys.argv[1]
    msg = sys.argv[2]

    jid = xmpp.protocol.JID(xmpp_jid)
    client = xmpp.Client(jid.getDomain(),debug=[])

    The script, using the JID and password passed to it, is authorized on the server, sends a message and closes the connection. Nowhere is easier. You will need to make a separate account for the script (but you can not, jabber allows multiple simultaneous connections with different resources) and enter the credentials in the variables xmpp_jid and xmpp_pwd .

    Save the code to a file, make it executable, run it. The launch is as follows:

    /home/username/scripts/send_xmpp_message "привет, это твой любимый сервер, не забудь вынуть пиво из серверного шкафа"

    The flight of fantasy is unlimited: after a light modification, for example, it will be possible to redirect data to a script instead of passing arguments, and using crontab, send messages according to a schedule.

    Also popular now: