0

I wrote this script to send a file using netcat. My intention is to open the netcat port in the destination server from the origin, this way I do everything with just one script instead of having one on the dest and one on the source.

#!/bin/bash

FILE=$1
SERVDEST=$2
SIZE=`du -h --apparent-size $FILE | cut -f1`
DATE=`date +%Y%m%d%H%M`

echo "Sending file $FILE of $SIZE to $SERVDEST" >> NCtransf_$DATE.log
echo "" >> NCtransf_$DATE.log

ssh $SERVDEST "nc -l 6969 > /backup/backupSO/nc_$FILE.tgz" &

#sleep 5       #tried sleeping for some secs but it didn't work either

(/usr/bin/time --format='Transfer took %e seconds' tar czf - $FILE | nc $SERVDEST 6969) &>> NCtransf_$DATE.log

echo "" >> NCtransf_$DATE.log

For some reason the file is created in the destination but its size is 0. The information never goes through and the netcat port stays open on the other side. I also tried changing this line ssh $SERVDEST "nc -l 6969 > /backup/backupSO/nc_$FILE.tgz" & for this ssh $SERVDEST "nohup nc -l 6969 > /backup/backupSO/nc_$FILE.tgz" & but same results happened.

If I try the same lines separated (opening the port manually on the destination) it works like a charm. Just for the record I have passwordless ssh connection and the port is open in the firewall.

Nocturn
  • 133
  • 1
  • 10

2 Answers2

0

What you're doing is useless.

You should use use scp.

It's basicaly the command to send files over ssh.

example:

FILE=$1
SERVDEST=$2
SIZE=`du -h --apparent-size $FILE | cut -f1`
DATE=`date +%Y%m%d%H%M`
tar czv /tmp/tmpfile.tar.gz $FILE
scp $FILE $SERVDEST:/backup/backupSO/nc_$FILE.tgz
rm /tmp/tmpfile.tar.gz
moebius_eye
  • 1,103
  • 7
  • 21
  • I'm actually doing this to compare the performance of SCP (enabling compression of course), SSH, NETCAT and RSYNC. Believe or not right now Netcat has given the best times yet I don't know why the transfer fails when I do it with this script. – Nocturn Apr 15 '15 at 15:44
  • That's because your file is sent unencrypted and you're piping the tar output directly to netcat. That's good if you want performance. Obviously, you're scarifying on security. – moebius_eye Apr 15 '15 at 15:52
  • Also, I think you may have made a mistake with your use of `time`. The way you use it, it will tell you the time tar has taken to compress your file, not the time it took to send it on the network. – moebius_eye Apr 15 '15 at 15:55
  • I've made a second response. – moebius_eye Apr 15 '15 at 15:56
0

Here's another better answer which is a good compromise between performance and security.

Feel free to test it.

FILE=$1
SERVDEST=$2
SIZE=`du -h --apparent-size $FILE | cut -f1`
DATE=`date +%Y%m%d%H%M`
tar czv - $FILE | ssh $SERVDEST "cat - > /backup/backupSO/nc_$FILE.tgz"

For even more performance, you can set theCompressionLevel (with -o) and use a different Cipher; that is if you don't care about the NSA. ;)

moebius_eye
  • 1,103
  • 7
  • 21
  • Thank you very much, as I said in your other answer I'm testing several options and this is one of them. However my objective is to find why setting the netcat receiver from the origin makes the transfer fail. I'm using the BSD version btw. – Nocturn Apr 15 '15 at 16:16