I'm baffled as to why I can't get rsyslog to log data sent to it via TCP whenever I send the data with a Python script, but works perfectly fine if I send the same string via netcat

Rsyslog is set up to receive syslog data on TCP/514: /etc/rsyslog.conf

$ModLoad imtcp
$InputTCPServerRun 514


$template LogByIP,"/tmp/%fromhost-ip%.log"

if ($fromhost-ip != '') then ?LogByIP

Restarted rsyslog. Executed this command from the client machine:

[root@localhost ~]# echo HELLO2 |nc logbox 514

Contents of /tmp/.log

Nov 18 15:36:16 HELLO2

So rsyslog is working as expected. Now when I try to do the same thing in Python, the message never gets written to file.

#! /usr/bin/python

import socket

message='hello world'


Running tcpdump on the logbox, I can see the session setup, the string transmitted and the session closed out in TCP, but "hello world" never shows up in the logfile. What am I missing here?

  • 3
    Just as a random guess, I noticed the shell script will append an enter (\n), and the python code won't. echo -n sends text without the trailing \n. – Some Linux Nerd Nov 19 '15 at 1:49
  • 3
    Adding a \n at the end of the string solved the problem. Thank you! – Sideshow Bob Nov 19 '15 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.