-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupcommand.py
More file actions
77 lines (63 loc) · 2.28 KB
/
groupcommand.py
File metadata and controls
77 lines (63 loc) · 2.28 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
73
74
75
76
77
from pexpect import pxssh
import optparse
import csv
class Client:
def __init__(self, host, user, password):
self.host = host
self.user = user
self.password = password
self.session = self.connect()
def connect(self):
try:
s = pxssh.pxssh()
s.login(self.host, self.user, self.password)
return s
except Exception, e:
print e
print 'Error Connecting'
def send_command(self, cmd):
self.session.sendline(cmd)
self.session.prompt()
return self.session.before
def runCommand(command):
for client in clientList:
output = client.send_command(command)
print 'Output from ' + client.host
print output + '\n'
def addClient(host, user, password):
client = Client(host, user, password)
clientList.append(client)
with open('clientList.txt', 'a') as writ:
with open('clientList.txt', 'rb') as readlist:
clientList=[]
for line in readlist:
if line:
addClient(line.split(",")[0],line.split(",")[1],line.split(",")[2])
# reader = csv.reader(readlist)
# clientList = list(reader)
inp = raw_input("Enter: \n1 if you want to manually submit the SSH credentials:\n2 if you want to specify a file with credentials:\n3 to run a command on your Client List:\n")
if inp == "1":
inp1 = raw_input("Enter details in the following order - separated by commas:\nIP Address of machine,SSH username,SSH Password\n")
ip = inp1.split(",")[0]
username = inp1.split(",")[1]
password = inp1.split(",")[2]
addClient(ip, username, password)
writ.write(ip+","+username+","+password)
writ.write('\n')
elif inp == "2":
inp2 = raw_input("Enter complete path to your file. Details must be in the following order - separated by commas:\nIP Address of machine,SSH username,SSH Password\nEach line in the file must have these 3 values separated by commas.\n")
with open(inp2, 'r') as user_file:
for line in user_file:
ip = inp2.split(",")[0]
username = inp2.split(",")[1]
password = inp2.split(",")[2]
addClient(ip, username, password)
writ.write(ip+","+username+","+password)
write.write('\n')
elif inp == "3":
inp3 = raw_input("Enter the command to run on your client list:\n")
runCommand(inp3)
# with open('clientList.txt', "a") as output:
# writer = csv.writer(output, lineterminator='\n')
# for val in clientList:
# writer.writerow([val])