Skip to main content

6 posts tagged with "bash"

View All Tags

· One min read
Hreniuc Cristian-Alexandru

Get lines that contain the same pattern twice or X times(source):

grep -E "(string.*){2}" file.log

· One min read
Hreniuc Cristian-Alexandru

Source: https://stackoverflow.com/a/19271368

You can achieve it in two steps:

1) You need to start nc with a named pipe (fifo) as its input:

mkfifo /tmp/fifoIn; cat /tmp/fifoIn | nc localhost 2222 &

2) Send your data from file input.txt, line by line with 2 sec delay:

cat input.txt | while read line; do echo $line; sleep 2; done > /tmp/fifoIn

I've tested it with this "server" (I'm using openbsd netcat syntax):

nc -l localhost 2222

If you don't want the new-line char after every line use echo -n instead of echo.

· One min read
Hreniuc Cristian-Alexandru

Install ucspi-tcp:

apt-get install ucspi-tcp

This pacakge contains this list of tools

Create tcp server from bash using tcpserver command from ucspi-tcp:

# script.sh content
#!/bin/bash
# read MESSAGE
echo "message: $MESSAGE"
sleep 2
echo " message 2"
sleep 3
echo " message 3"
sleep 2
# ---- end ------- #

# Run tcpserver:
tcpserver -v -H -R IP PORT script.sh

# Open a connection:
echo "" | nc IP PORT

This tcp server can open multiple connections in parallel. Verry usefull when you want to have a mock tcpserver.

Fixed issues:

  1. The read command doesn't work if I add it in the script(I had a client written in C++). But it did work when my client was nc....

The fix was to add \n at the end of the string that I was sending from that client. To reproduce this, you can run a client like this: echo -n "sa" | nc IP PORT and the script should contain a read instruction. You will notice that the server will stop at that read instruction.

· One min read
Hreniuc Cristian-Alexandru

Add this to ~/.basrc:

# Display date of the command
export HISTTIMEFORMAT="%d/%m/%y %T "

# Size(nr of lines) of the history loaded in memory?
export HISTFILESIZE=10000000
# Size(nr of lines) of the history saved in file?
export HISTFILESIZE=10000000
# Path to history file.
export HISTFILE=/home/chreniuc/data/configs/.bash_history