From 8bd5aaad2c3bf6c0282f6358cb62c34073699508 Mon Sep 17 00:00:00 2001 From: Grant Bremer Date: Mon, 12 Aug 2013 21:59:33 -0400 Subject: [PATCH 1/9] Adding support for CHEATPATH variable and multiple cheat locations --- cheat | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cheat b/cheat index ad69b44..a4d0117 100755 --- a/cheat +++ b/cheat @@ -2,31 +2,44 @@ import os import sys +def cheat_directories(): + default = [ default_dir for default_dir in [os.path.expanduser('~/.cheat')] if os.path.isdir(default_dir) ] + if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']: + return [ path for path in os.environ['CHEATPATH'].split(os.pathsep) if os.path.isdir(path) ] + default + else: + return default + +def cheat_files(cheat_directories): + cheats = {} + for cheat_dir in reversed(cheat_directories): + cheats.update(dict([ (cheat, cheat_dir) for cheat in os.listdir(cheat_dir) ])) + return cheats + # assemble a keyphrase out of all params passed to the script keyphrase = ' '.join(sys.argv[1:]) -cheat_dir = os.path.expanduser('~') + '/.cheat/' +cheat_dirs = cheat_directories() -# verify that the ~/.cheat directory exists -if not os.path.isdir(cheat_dir): - print >> sys.stderr, 'The ~/.cheat directory does not exist.' +# verify that we have at least one cheat directory +if not cheat_dirs: + print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.' exit() # list the files in the ~/.cheat directory -cheatsheets = os.listdir(cheat_dir) -cheatsheets.sort() +cheatsheets = cheat_files(cheat_dirs) # print help if requested -if keyphrase in ['', 'help', '--help', '-h']: +if keyphrase.lower() in ['', 'help', '--help', '-h']: print "Usage: cheat [keyphrase]\n" print "Available keyphrases:" - print "\n".join(cheatsheets) + max_command = max([ len(x) for x in cheatsheets.keys() ]) + 3 + print '\n'.join(sorted([ '%s [%s]' % (key.ljust(max_command), value) for key, value in cheatsheets.items() ])) exit() # print the cheatsheet if it exists if keyphrase in cheatsheets: - with open (cheat_dir + keyphrase, 'r') as cheatsheet: + with open (os.path.join(cheatsheets[keyphrase], keyphrase), 'r') as cheatsheet: print cheatsheet.read() # if it does not, say so else: - print 'No cheatsheet found.' + print 'No cheatsheet found for %s.' % keyphrase From b6b416e7ceda798dec4e95e5476f62e4a0522ae3 Mon Sep 17 00:00:00 2001 From: John Shanahan Date: Wed, 14 Aug 2013 01:41:17 -0400 Subject: [PATCH 2/9] Added nmap support --- .cheat/nmap | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .cheat/nmap diff --git a/.cheat/nmap b/.cheat/nmap new file mode 100644 index 0000000..61c58c6 --- /dev/null +++ b/.cheat/nmap @@ -0,0 +1,33 @@ +Single target scan: +nmap [target] + +Scan from a list of targets: +nmap -iL [list.txt] + +iPv6: +nmap -6 [target] + +OS detection: +nmap -O [target] + +Save output to text file: +nmap -oN [output.txt] [target] + +Save output to xml file: +nmap -oX [output.xml] [target] + +Scan a specific port: +nmap -source-port [port] [target] + +Do an aggressive scan: +nmap -A [target] + +Traceroute: +nmap -traceroute [target] + +Ping scan only: -sP +Don't ping: -PN +TCP SYN ping: -PS +TCP ACK ping: -PA +UDP ping: -PU +ARP ping: -PR From 7cf6291c8adedd4065441c1adcb32b1fa882f0e5 Mon Sep 17 00:00:00 2001 From: John Shanahan Date: Wed, 14 Aug 2013 10:36:00 -0400 Subject: [PATCH 3/9] Changed from 2-spaces per indentation level to 4, per PEP-8 standard. --- cheat | 18 +++++++++--------- install | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cheat b/cheat index ad69b44..856a419 100755 --- a/cheat +++ b/cheat @@ -8,8 +8,8 @@ cheat_dir = os.path.expanduser('~') + '/.cheat/' # verify that the ~/.cheat directory exists if not os.path.isdir(cheat_dir): - print >> sys.stderr, 'The ~/.cheat directory does not exist.' - exit() + print >> sys.stderr, 'The ~/.cheat directory does not exist.' + exit() # list the files in the ~/.cheat directory cheatsheets = os.listdir(cheat_dir) @@ -17,16 +17,16 @@ cheatsheets.sort() # print help if requested if keyphrase in ['', 'help', '--help', '-h']: - print "Usage: cheat [keyphrase]\n" - print "Available keyphrases:" - print "\n".join(cheatsheets) - exit() + print "Usage: cheat [keyphrase]\n" + print "Available keyphrases:" + print "\n".join(cheatsheets) + exit() # print the cheatsheet if it exists if keyphrase in cheatsheets: - with open (cheat_dir + keyphrase, 'r') as cheatsheet: - print cheatsheet.read() + with open (cheat_dir + keyphrase, 'r') as cheatsheet: + print cheatsheet.read() # if it does not, say so else: - print 'No cheatsheet found.' + print 'No cheatsheet found.' diff --git a/install b/install index cf64c1f..4d55ab8 100755 --- a/install +++ b/install @@ -4,9 +4,9 @@ import shutil import sys try: - shutil.copy('./cheat', '/usr/local/bin/') - shutil.copytree('./.cheat', expanduser('~') + '/.cheat') - print "cheat has been installed successfully." + shutil.copy('./cheat', '/usr/local/bin/') + shutil.copytree('./.cheat', expanduser('~') + '/.cheat') + print "cheat has been installed successfully." except IOError as e: - print >> sys.stderr, "This installer must be run as root." - sys.exit(1) + print >> sys.stderr, "This installer must be run as root." + sys.exit(1) From cc47515f41b4526ccc2da9304b65397399bb1ecf Mon Sep 17 00:00:00 2001 From: John Shanahan Date: Thu, 15 Aug 2013 17:49:30 -0400 Subject: [PATCH 4/9] Added '-help' and 'cheat' to the list of cheat usage arguments. --- cheat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cheat b/cheat index 856a419..0469ddf 100755 --- a/cheat +++ b/cheat @@ -16,7 +16,7 @@ cheatsheets = os.listdir(cheat_dir) cheatsheets.sort() # print help if requested -if keyphrase in ['', 'help', '--help', '-h']: +if keyphrase in ['', '-h', 'help', '-help', '--help', 'cheat']: print "Usage: cheat [keyphrase]\n" print "Available keyphrases:" print "\n".join(cheatsheets) From 10cb62fee97acd92a0369e0a8f8ea1729410ddf2 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 15 Aug 2013 18:03:58 -0400 Subject: [PATCH 5/9] Continuing to merge gbremer's work into master. --- cheat | 22 +++++++++++----------- install | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cheat b/cheat index a4d0117..3a1bc81 100755 --- a/cheat +++ b/cheat @@ -16,30 +16,30 @@ def cheat_files(cheat_directories): return cheats # assemble a keyphrase out of all params passed to the script -keyphrase = ' '.join(sys.argv[1:]) +keyphrase = ' '.join(sys.argv[1:]) cheat_dirs = cheat_directories() # verify that we have at least one cheat directory if not cheat_dirs: - print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.' - exit() + print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.' + exit() # list the files in the ~/.cheat directory cheatsheets = cheat_files(cheat_dirs) # print help if requested if keyphrase.lower() in ['', 'help', '--help', '-h']: - print "Usage: cheat [keyphrase]\n" - print "Available keyphrases:" - max_command = max([ len(x) for x in cheatsheets.keys() ]) + 3 - print '\n'.join(sorted([ '%s [%s]' % (key.ljust(max_command), value) for key, value in cheatsheets.items() ])) - exit() + print "Usage: cheat [keyphrase]\n" + print "Available keyphrases:" + max_command = max([ len(x) for x in cheatsheets.keys() ]) + 3 + print '\n'.join(sorted([ '%s [%s]' % (key.ljust(max_command), value) for key, value in cheatsheets.items() ])) + exit() # print the cheatsheet if it exists if keyphrase in cheatsheets: - with open (os.path.join(cheatsheets[keyphrase], keyphrase), 'r') as cheatsheet: - print cheatsheet.read() + with open (os.path.join(cheatsheets[keyphrase], keyphrase), 'r') as cheatsheet: + print cheatsheet.read() # if it does not, say so else: - print 'No cheatsheet found for %s.' % keyphrase + print 'No cheatsheet found for %s.' % keyphrase diff --git a/install b/install index 4d55ab8..b179e26 100755 --- a/install +++ b/install @@ -8,5 +8,5 @@ try: shutil.copytree('./.cheat', expanduser('~') + '/.cheat') print "cheat has been installed successfully." except IOError as e: - print >> sys.stderr, "This installer must be run as root." + print >> sys.stderr, e sys.exit(1) From b8c42a1b640b18bb9791c42eddff39b206ec4822 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 15 Aug 2013 18:38:13 -0400 Subject: [PATCH 6/9] Updated the README. --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4799788..8d75aac 100644 --- a/README.md +++ b/README.md @@ -83,4 +83,18 @@ If you would like to contribute additional cheatsheets for basic \*nix commands, please modify the `.cheat` file and send me a pull request. -[dotfiles]: http://dotfiles.github.io/ +Related Projects +---------------- +- [lucaswerkmeister/cheats][1]: An implementation of this concept in pure bash + that also allows not only for numerical indexing of subcomands but also + supports running commands interactively. + +- [jahendrie/cheat][2]: A bash-only implmentation that additionally allows for + cheatsheets to be created and `grep` searched from the command-line. + ([jahendrie][] contributed key ideas to this project as well.) + + +[dotfiles]: http://dotfiles.github.io/ +[jahendrie]: https://github.com/jahendrie +[1]: https://github.com/lucaswerkmeister/cheats +[2]: https://github.com/jahendrie/cheat From 40d18e01dcf286cb10a74d38f481cbe49c232a03 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 15 Aug 2013 18:52:32 -0400 Subject: [PATCH 7/9] Updated the README to document the usage of the $CHEATPATH environment variable. --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 8d75aac..c44d027 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,23 @@ After you've customized your cheatsheets, I urge you to track `~/.cheat/` along with your [dotfiles][]. +Setting a `$CHEATPATH` +---------------------- +By default, all cheatsheets are installed to `~/.cheat/`, but you can instruct +`cheat` to look for cheatsheets in other directories by exporting a `CHEATPATH` +environment variable: + +```bash +export CHEATPATH=/path/to/my/cheats +``` + +You may, of course, append multiple directories to your `CHEATPATH`: + +```bash +export CHEATPATH=$CHEATPATH:/path/to/more/cheats +``` + + Contributing ------------ If you would like to contribute additional cheatsheets for basic \*nix From 07bd1953e7ca361bc3f19ac5227adadbe4731ce8 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 15 Aug 2013 18:55:22 -0400 Subject: [PATCH 8/9] More changes to the README. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c44d027..ac39e3f 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ After you've customized your cheatsheets, I urge you to track `~/.cheat/` along with your [dotfiles][]. -Setting a `$CHEATPATH` +Setting a CHEATPATH ---------------------- By default, all cheatsheets are installed to `~/.cheat/`, but you can instruct `cheat` to look for cheatsheets in other directories by exporting a `CHEATPATH` @@ -96,8 +96,8 @@ export CHEATPATH=$CHEATPATH:/path/to/more/cheats Contributing ------------ -If you would like to contribute additional cheatsheets for basic \*nix -commands, please modify the `.cheat` file and send me a pull request. +If you would like to contribute cheetsheets for program functionality, please +fork this repository, make your chanages, and send me a pull request. Related Projects From 6b8c516599e1a446a3208b3751497f624d8f430d Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 15 Aug 2013 18:55:59 -0400 Subject: [PATCH 9/9] Fixing a minor typo in the REAMDE. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ac39e3f..387d6a3 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ export CHEATPATH=$CHEATPATH:/path/to/more/cheats Contributing ------------ -If you would like to contribute cheetsheets for program functionality, please +If you would like to contribute cheetsheets or program functionality, please fork this repository, make your chanages, and send me a pull request.