Linux/BASH

From CodeMonkeyWiki

Jump to: navigation, search

Contents

Resume failed scp

If you've run an SCP thats cancelled and want to resume, where the scp command was..

  $ scp -r some_dir user@host:/target/path

replace the scp -r with rsync --partial --progress --rsh=ssh to make the resume command:

rsync -r --partial --progress --rsh=ssh some_dir user@host:/target/path

Find & grep

To find all files called "something.txt" containing the text "find me" run:

find . -name "something.txt" -exec grep "find me" {} /dev/null \;

Port scanner script

#!/usr/bin/python

import socket
import sys

if ( len(sys.argv) != 2 ):
    print "Usage: " + sys.argv[0] + " you must enter IP or FQDN as argument"
    sys.exit(1)

remote_host = sys.argv[1]

for remote_port in [21,22,23,80,139,139,389,443,445,3128,3306,3389]:
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.settimeout(20)
  try:
    sock.connect((remote_host, remote_port))
  except Exception,e:
    print "%d closed " % remote_port
  else:
    print "%d open" % remote_port
sock.close()

Tab Complete

To ignore certain files/directories when tab completing (in the example "CVS" and ".svn", open "~/.bashrc"..

export FIGNORE=CVS:.svn

Trap Ctrl-C

Example loops forever until ctrl-c is pressed

#! /bin/sh

exit_loop() {
  echo "death!"
  exit
}

trap exit_loop INT
 
while [ 1 == 1 ]
do
  echo "."
done
Personal tools