|
Project Information
Featured
|
DescriptionThe SSH Power Tool (sshpt) enables you to execute commands and upload files to many servers simultaneously via SSH without using pre-shared keys. Uploaded files and commands can be executed directly or via sudo. Connection and command execution results are output in standard CSV format for easy importing into spreadsheets, databases, or data mining applications. AdvantagesSince sshpt does not utilize pre-shared SSH keys it will use provided credentials for all outgoing SSH connections. This has many advantages:
RequirementsThe SSH Power Tool requires Python 2.5+ (not 3.0+ yet) and the following Python modules:
Also A Modulesshpt is designed to be easily imported as a module so that any Python application can quickly and easily be given the ability to SSH into a large number of hosts to gather information or perform administrative tasks. Report Format (CSV)Execution results are reported in RFC 4180-compliant CSV format... "host","connection result","datetime","command","command output"
In the case that multiple commands were passed to sshpt the report will prepend 'index: ' before each command and each result respectively. Example: ./sshpt -f ../testhosts.txt "echo foo" "echo bar" Username: myuser Password: "devhost","SUCCESS","2009-02-20 16:20:10.997818","0: echo foo 1: echo bar","0: foo 1: bar" "prodhost","SUCCESS","2009-02-20 16:20:11.990142","0: echo foo 1: echo bar","0: foo 1: bar" Use CasesThe following are examples of how sshpt might be used. Run CommandsYour boss has asked you to provide a list of Linux servers that have a certain package installed (e.g. wget). You don't have root access to all systems but you do have the ability to log in... IMPORTANT: Always make sure to surround your commands in quotes! $ ./sshpt.py -f hostlist.txt -o wget_report.csv "rpm -q wget" Username: myuser Password: "devhost","SUCCESS","2009-02-19 15:07:57.078446","rpm -q wget","wget-1.9.1-17" "prodhost","SUCCESS","2009-02-19 15:07:58.663611","rpm -q wget","Package wget is not installed" "blah","FAILED","2009-02-19 15:07:59.134068","rpm -q wget","(-5, 'No address associated with hostname')" ... In this example the results were output to stdout and to an outfile (-o), wget_report.csv. Upload FilesYou've just got some new DNS servers in and you need to configure all of your servers to use them. Your task is to replace the existing resolv.conf on all of your servers with a modified one... $ ./sshpt.py -f hostlist.txt -u myuser -s --copy-file=resolv.conf --dest=/etc/resolv.conf Password: "blah","FAILED","2009-02-19 15:20:14.134068","sudo -u root sshpt: sftp.put /etc/resolv.conf blah:/tmp/resolv.conf)","(-5, 'No address associated with hostname')" "devhost","SUCCESS","2009-02-19 15:20:15.401703","sudo -u root sshpt: sftp.put /etc/resolv.conf devhost:/tmp/resolv.conf)","-rw-r--r-- 1 root root 72 2009-02-19 15:20 /tmp/resolv.conf" "prodhost","SUCCESS","2009-02-19 15:20:15.691474","sudo -u root sshpt: sftp.put /etc/resolv.conf prodhost:/tmp/resolv.conf)","-rw-r--r-- 1 root root 72 2009-02-19 15:20 /tmp/resolv.conf" ... This example is kind of complicated so here's a detailed rundown of what happened:
Note: If the sudo switch (-s) was not set the file would have been copied directly to the supplied destination (and owned by the username that was used to connect). In this case the command output would have reported "Permission Denied" since /etc/resolv.conf can only be edited/replaced by root. Command Line HelpSSHPT Help Text./sshpt.py --help
Usage: sshpt.py [options] [command] [arguments...]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-f <file>, --file=<file>
Location of the file containing the host list.
-o <file>, --outfile=<file>
Location of the file where the results will be saved.
-a <file>, --authfile=<file>
Location of the file containing the credentials to be
used for connections (format is "username:password").
-t <int>, --threads=<int>
Number of threads to spawn for simultaneous connection
attempts [default: 10].
-p <port>, --port=<port>
The port to be used when connecting. Defaults to 22.
-u <username>, --username=<username>
The username to be used when connecting.
-P <password>, --password=<password>
The password to be used when connecting (not
recommended--use an authfile unless the username and
password are transient
-q, --quiet Don't print status messages to stdout (only print
errors).
-c <file>, --copy-file=<file>
Location of the file to copy to and optionally execute
(-x) on hosts.
-D <path>, --dest=<path>
Path where the file should be copied on the remote
host (default: /tmp/).
-x, --execute Execute the copied file (just like executing a given
command).
-r, --remove Remove (clean up) the SFTP'd file after execution.
-T <seconds>, --timeout=<seconds>
Timeout (in seconds) before giving up on an SSH
connection (default: 30)
-s, --sudo Use sudo to execute the command (default: as root).
-U <username>, --sudouser=<username>
Run the command (via sudo) as this user.Notes On UsageAlways make sure to put your commands in quotes like so: ./sshpt.py -f hostlist.txt "ls -l /tmp/foo" "cat /tmp/foo" Otherwise the shell will think that '-l' is just another command line argument being passed to sshpt.py and this has the potential to cause serious problems (e.g. running 'rm -rf' on your local machine by accident) |