|
| 1 | +# Copyright 2019 by Tuxedoar <tuxedoar@gmail.com> |
| 2 | + |
| 3 | +# LICENSE |
| 4 | + |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# any later version. |
| 9 | + |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | + |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import argparse |
| 19 | +import sys |
| 20 | +import re |
| 21 | +import getpass |
| 22 | +from time import sleep |
| 23 | +from socket import error |
| 24 | +from _version import __version__ |
| 25 | +import paramiko |
| 26 | + |
| 27 | +def main(): |
| 28 | + """ Setup CLI arguments and call rest of the functions """ |
| 29 | + parser = argparse.ArgumentParser( |
| 30 | + description='Excecute remote commands on several hosts, with SSH.') |
| 31 | + parser.add_argument('FILE', help='Plain text file with list of hosts') |
| 32 | + parser.add_argument('USER', help='User to login on remote hosts') |
| 33 | + parser.add_argument('COMMANDS', help='Comma separated commands to be \ |
| 34 | + executed on remote hosts') |
| 35 | + parser.add_argument('-p', '--port', required=False, action='store', |
| 36 | + help='Specify SSH port to connect to hosts') |
| 37 | + parser.add_argument('-v', '--version', action='version', |
| 38 | + version="%(prog)s {version}".format(version=__version__), |
| 39 | + help='Show current version') |
| 40 | + args = parser.parse_args() |
| 41 | + |
| 42 | + cmd = args.COMMANDS |
| 43 | + SSH_PORT = 22 |
| 44 | + |
| 45 | + if args.port: |
| 46 | + SSH_PORT = args.port |
| 47 | + pw = getpass.getpass('\n Please, enter your password to access hosts: ') |
| 48 | + target_hosts = read_hosts_file(args.FILE) |
| 49 | + |
| 50 | + # Start SSH session on each remote host. |
| 51 | + for target_host in target_hosts: |
| 52 | + try: |
| 53 | + remote_shell = setup_ssh_session(args.USER, pw, SSH_PORT, target_host) |
| 54 | + exec_remote_commands(remote_shell, cmd) |
| 55 | + except (KeyboardInterrupt, \ |
| 56 | + paramiko.ssh_exception.AuthenticationException, \ |
| 57 | + paramiko.SSHException, \ |
| 58 | + error) as e: |
| 59 | + print("%s" % (e)) |
| 60 | + |
| 61 | + |
| 62 | +def setup_ssh_session(user, pw, port, remote_host): |
| 63 | + """ Setup the SSH session with necessary arguments """ |
| 64 | + print("\n [+] Connecting to host %s with the user %s ... \n" % (remote_host, user)) |
| 65 | + my_session = paramiko.SSHClient() |
| 66 | + my_session.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 67 | + my_session.connect(remote_host, username=user, password=pw, port=port, \ |
| 68 | + look_for_keys=False, allow_agent=False, timeout=10) |
| 69 | + remote_shell = my_session.invoke_shell() |
| 70 | + return remote_shell |
| 71 | + |
| 72 | + |
| 73 | +def exec_remote_commands(remote_shell, commands): |
| 74 | + """ Execute provided commands on remote hosts! """ |
| 75 | + remote_commands = commands.split(',') |
| 76 | + |
| 77 | + for command in remote_commands: |
| 78 | + # Remove double quotes!. |
| 79 | + command = command.replace('\"', '') |
| 80 | + remote_shell.send(command+'\n') |
| 81 | + # Wait 1 second before sending commands provided by user!. |
| 82 | + sleep(1) |
| 83 | + # Ouput buffer size in bytes!. |
| 84 | + output = remote_shell.recv(8000) |
| 85 | + # Split each line for output. |
| 86 | + output = output.splitlines() |
| 87 | + for lines in output: |
| 88 | + print(lines.decode()) |
| 89 | + |
| 90 | + |
| 91 | +def validate_ip_addr(ip_addr): |
| 92 | + """ Validate an IP address """ |
| 93 | + validate_ip = re.search("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip_addr) |
| 94 | + return bool(validate_ip) |
| 95 | + |
| 96 | + |
| 97 | +def read_hosts_file(hosts_file): |
| 98 | + """ Read the file and get the IP adresses """ |
| 99 | + remote_hosts = [] |
| 100 | + try: |
| 101 | + with open(hosts_file, 'r') as file: |
| 102 | + for line in file.readlines(): |
| 103 | + ip = line.strip() |
| 104 | + if line and not line.startswith('#'): |
| 105 | + print("\n WARNING: The IP %s is NOT valid. Ignored!" % (ip)) \ |
| 106 | + if not validate_ip_addr(ip) else remote_hosts.append(ip) |
| 107 | + return remote_hosts |
| 108 | + |
| 109 | + except IOError: |
| 110 | + print("Can't read the specified file. Make sure it exist!.") |
| 111 | + sys.exit(2) |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments