All Entries Tagged With: "command"
Use a decoy while scanning ports
From http://www.commandlinefu.com
Use a decoy while scanning ports to avoid getting caught by the sys admins.
$ sudo nmap -sS 192.168.0.10 -D 192.168.0.2
Scan for open ports on the target device/computer (192.168.0.10) while setting up a decoy address (192.168.0.2). This will show the decoy ip address instead of your ip in targets security logs. Decoy address needs to be alive. Check the targets security log at /var/log/secure to make sure it worked.
For my part, since I run I.T., I’m not concerned about being seen by the sys admins, but if I was in an internet cafe, or coffee shop, or using a hijacked wireless network this could come in handy – just to run a decoy to gain some time.
Control your hibernation state more easily from Terminal.app
From http://www.commandlinefu.com by thebillywayne
The command below is a function and should be placed in your bash environmental file.
The author often changed his MacBook’s sleep state. So he created a function for bash to make it a little easier.
Usage:
hibernate (on | off)
"hibernate on" will set your laptop to hibernate if the lid is closed.
"hibernate off" will set your laptop to sleep if the lid is closed.
### note : “proper” indentation isn’t preserved on the website
<START CODE>
function hibernate()
{
case "${1}" in
on)
sudo pmset -a hibernatemode 1
echo Hibernate mode on.
;;
off)
sudo pmset -a hibernatemode 0
echo Hiberate mode off.
;;
*)
echo "I'm sorry Dave, but I can't do that."
;;
esac
}
<END CODE>
To make things easier, add the proper line in your /etc/sudoers file so that your user may invoke pmset without a password. Here’s how it should look:
me mycomputer = NOPASSWD: /usr/bin/pmset
Don’t forget that you must edit sudoers with `sudo visudo` from Terminal.app, and not by another text editor.
Monitor Open TCP Connections
From SHELL FU: http://www.shellfu.org
The command below will monitor open TCP connections:
watch -n 1 "netstat -tpanl | grep ESTABLISHED"
This shows connections by processes for your user, or if done as root (“sudo netstat”) all processes.
Another option to perform the same task is:
sudo lsof -i -T -n
Again, ‘watch’ could be used with this, or adding -r as an option will make lsof refresh the output.
Some more command line shell fu for ya!
Remove Empty Directories:
Remove all empty directories within the current directory
find . -type d -empty -exec rmdir {} ;
Or another way to do it:
perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
Create File Dumps
This script displays the contents of files (or stdin) in ascii, hexadecimal, decimal, octal, and binary formats.
#!/usr/bin/perl
undef $/; # slurp files
while( $content = <> ) {
$offset = 0;
print "OFFSET ASC HEX DEC OCT BINn";
while( length $content ) {
$n = ord( substr( $content, 0, 1, '' ) );
printf "%08x %c %2x %3u %3o %sn"
, $offset,
, ( $n > 0x1f && $n < 0x7f ) ? $n : ord '.',
, $n,
, $n,
, $n,
, substr( unpack( "B*", pack( "n", $n ) ), -8 )
;
$offset++;
}
}
Moving Large Directories
Why not use cp or mv to move /usr/home to /storage/export/home? Weird things happen to hard and softlinks when you mv or cp. Try it and remember that a mv between different filesystems is actually a copy and delete.
Try this instead:
# tar -C /usr -cf - home | tar -C /storage/export -xvf -
Or, to copy to a remote machine:
# tar -C /usr -cf - home | ssh user@somemachine tar -C /storage/export -xvf -
You may want to add the -z switch to the tar commands. It will add compression but it depends on the type of data and your connection speed if it really improves transfer speeds.
Transfer Files With Netcat
On client machine
nc -lpvv port > file
On server machine
nc -vv clientip port < file
Example :
Client: nc -l -p 6868 > file.txt
Server: nc 31.41.59.26 6868 < file.txt
Diff Two Directories
A quick script to compare files from two directories (for example a backup and working directory).
#!/bin/bash
cr='*'
if [ -z $3 ]; then cr=$3; fi
for f in `find $1/$3 -type f | sed "s|$1/||"`
do
printf "===!%-76s" "$f!" | tr ' !' '= '; echo
diff $1/$f $2/$f | sed -e "s/^</$1: /" -e "s/^>/$2: /"
done
Usage: [script name] directory1 directory2 to check all files
[script name] directory1 directory2 *html to check files of type html.





