-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparamiko-ssh-yaml.py
More file actions
72 lines (52 loc) · 2.16 KB
/
Copy pathparamiko-ssh-yaml.py
File metadata and controls
72 lines (52 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import select
import yaml
import paramiko
def parse_args():
parser = argparse.ArgumentParser(description='Python paramiko script that uses a YAML file to run multiple commands\
on a set of hosts, takes the stdout of those commands & saves the output to a file.')
parser.add_argument('--config','-c', help='YAML Configuration File', required=True)
args = parser.parse_args()
return args
def main():
args = parse_args()
with open(args.config, "r") as f:
try:
data = yaml.load(f)
except yaml.YAMLError as e:
print(e)
username = data["global_config"]["username"]
password = data["global_config"]["password"]
port = data["global_config"]["port"]
for host in data["hosts"]:
hostname = host
print("[>] Attempting Connection to: {}".format(hostname))
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port=port, username=username, password=password, timeout=5)
channel = client.get_transport().open_session()
for command in data["commands"]:
try:
stdin, stdout, stderr = client.exec_command(command)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
if len(rl) > 0:
out = (stdout.channel.recv(1024))
with open("{}.log".format(hostname) ,"a") as fd:
fd.write("".join(out))
fd.close()
except Exception as e:
print(e)
except Exception as e:
print(e)
finally:
client.close()
print("[>] Client Session to : {} closed".format(hostname))
if __name__ == '__main__':
main()