LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   tail file over tcp (https://www.linuxquestions.org/questions/linux-networking-3/tail-file-over-tcp-553615/)

lloydie-t 05-13-2007 06:45 PM

tail file over tcp
 
I am trying to send changes of a file using 'tail' over a tcp port, but I can not find any references to do this. I have seen that you can do it to a serial port using

Code:

tail -f /path/to/cdr/file > /dev/ttyS0
Can you help?

jschiwal 05-13-2007 07:31 PM

I think it is something like this:
On the target computer enter: 5<>/dev/tcp/<host2>/port

On the remote computer enter: tail -f file >/dev/tcp/<host1>/port

--
You could pipe the output of tail to netcat:
netcat -l <port>
and on the other computer:
tail -f file | /dev/tcp/<host1>/port

See the "Advanced Bash Scripting Guide" on the www.tldb.org website and the manpage and readme for netcat.

lloydie-t 05-13-2007 08:05 PM

Thanks Jschiwal. The machine I am trying to send to is a windows machine listening on a port 4515. Am I correct is assuming that /dev/tcp/<host1>/port is the machine I am trying to send to?

jschiwal 05-14-2007 07:10 AM

Quote:

Originally Posted by lloydie-t
Thanks Jschiwal. The machine I am trying to send to is a windows machine listening on a port 4515. Am I correct is assuming that /dev/tcp/<host1>/port is the machine I am trying to send to?

Yes. The abs-guide has this example:
Code:

#!/bin/bash
# dev−tcp.sh: /dev/tcp redirection to check Internet connection.
# Script by Troy Engel.
# Used with permission.
TCP_HOST=www.dns−diy.com  # A known spam−friendly ISP.
TCP_PORT=80                # Port 80 is http.
# Try to connect. (Somewhat similar to a 'ping' . . .)
echo "HEAD / HTTP/1.0" >/dev/tcp/${TCP_HOST}/${TCP_PORT}
MYEXIT=$?
: <<EXPLANATION
If bash was compiled with −−enable−net−redirections, it has the capability of
using a special character device for both TCP and UDP redirections. These
redirections are used identically as STDIN/STDOUT/STDERR. The device entries
are 30,36 for /dev/tcp:
  mknod /dev/tcp c 30 36

I did this between two Linux machines:
A laptop and a desktop. I'm at the laptop and ssh'd into the desktop because I was too lazy to walk over there!

desktop:
netcat -l -p 8080

laptop:
sudo tail -f /var/log/messages >/dev/tcp/hpmedia/8080
root's password:

Result at Desktop:

May 14 12:35:10 hpamd64 SuSEfirewall2: batch committing...
May 14 12:35:10 hpamd64 SuSEfirewall2: Firewall rules successfully set
May 14 12:42:33 hpamd64 syslog-ng[2762]: STATS: dropped 0
May 14 12:46:08 hpamd64 sudo: jschiwal : TTY=pts/2 ; PWD=/home/jschiwal ; USER=root ; COMMAND=/usr/bin/tail -f /var/log/messages
May 14 12:49:06 hpamd64 sudo: jschiwal : TTY=pts/2 ; PWD=/home/jschiwal ; USER=root ; COMMAND=/sbin/rcSuSEfirewall2 status

You need the target to be listening or you will get:
echo "This is a test" >/dev/tcp/hpamd64/8080
-bash: connect: Connection refused
-bash: /dev/tcp/hpamd64/8080: Connection refused

So Connection refused could just mean that there isn't a listener to complete the handshake, and isn't necessarily a firewall problem.

lloydie-t 05-14-2007 02:31 PM

Nearly there. The damn windoze machine is looking for either carriage return or Line feed and is therefore not seeing any thing until it gets one. Can we send this along with the tail?

jschiwal 05-15-2007 02:18 AM

Pipe the tail command through unix2dos to convert \n to \n\r.

Code:

sudo tail -f /var/log/messages | unix2dos >/dev/tcp/hpmedia/8080


All times are GMT -5. The time now is 04:11 AM.