Posts Tagged “one-liner”

Recently had to dump a mysql database that was larger than the available space. Here’s a quick one-liner to compress the dump and pipe it over ssh to another destination.

# mysqldump DATABASE | gzip -c –fast |ssh ‘cat > /home/user/DATABASE.sql.gz’

Comments 1 Comment »

See a suspicious IP on your box and wonder what it’s doing? Here’s a one-liner that will list the files that IP is accessing:

# lsof -p $(netstat -npat | gawk ‘$5 ~ / {print $7}’ | cut -d”/” -f1 | tail -n1)

Comments No Comments »

Here’s a nifty little trick with vimdiff, say you want to compare the output of to commands. You can do so with a little redirections:

# vimdiff <(cat /etc/passwd) <(cat /etc/shadow)

I know this is silly since you can simply vimdiff the files directly without the redirects. But say you want to compare who is currently logged in with who was last 10 people logged in.

# vimdiff <(who) <(last|head)


Note: the space between th ‘)’ and the ‘<’ is required.

Comments No Comments »

Often times when being hit by a plethora of connections, it is good to tally them all up and see if there is a specific locale that may be of questionable origin.

netstat -an | grep “ESTABLISHED” |awk ‘{print $5}’ |cut -d “:” -f1 |sort |uniq -c |sort -n

Comments 2 Comments »

Nifty little one liners to find who’s running that problematic cronjob:

for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done

Comments 1 Comment »

The command for this week is SSH. There are a lot of neat things you can do with SSH. Here’s a nice one liner if you get in a pickle.

Background: You are working on remote server HOST_B when you realize that the file your trying to figure out how to configure properly is already working on remote server HOST_A. Unfortunately, you do not have direct access between HOST_A and HOST_B. Now you can easily SSH into HOST_A, SCP the file frpm HOST_A down to your local computer. SCP the file from your local computer to HOST_B and then SSH into HOST_B to put the file in the correct place. Well that takes a bit if time. You can pass information through SSH in order to transfer a file with a pipe.

Below is a one liner that not only transfers the file in this manner, but compresses in one format on HOST_A for faster downlaod, changes the compression type on the local computer so it can be decompressed on HOST_B.

ssh USER@HOST_A ‘cat FILENAME | bzip2′ | bunzip2 | gzip | ssh USER@HOST_B ‘gunzip | cat > FILENAME’

Try it out and let me know what you think.

Comments 1 Comment »