-
Notifications
You must be signed in to change notification settings - Fork 1
/
cp.py
108 lines (96 loc) · 3.51 KB
/
cp.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import re
import shutil
import os
from os import listdir
import glob
import xlrd
import xlwt
import time
import sys
import winpexpect
import paramiko
import paramiko_expect
import traceback
from paramiko_expect import SSHClientInteraction
# time check - start time
start_time = time.time()
iprb = xlrd.open_workbook('c:/python/checkpoint/ip.xls')
sheet = iprb.sheet_by_name('IPs')
num_rows = sheet.nrows
num_cols = sheet.ncols
cmrb = xlrd.open_workbook('c:/python/checkpoint/commands.xls')
sheet1 = cmrb.sheet_by_name('commands')
num_rows1 = sheet1.nrows
num_cols1 = sheet1.ncols
ip_addr_list = [sheet.row_values(rawnum)[0] for rawnum in range(sheet.nrows)]
commands_list = [sheet1.row_values(rawnum)[0] for rawnum in range(sheet1.nrows)]
num_ips = 0
#host = '10.10.0.254'
user = 'admin'
passwd = '1q2w3e'
expert_passwd = '1q2w3e'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
prompt = '\n.*> '
expert_prompt = '\n.*# '
not_found = '\n.*Invalid command:.*'
no_expert_pass = '\n.*"set expert-password".*'
wrong_expert_pass = '\n.*Wrong password.*'
enter_expert_pass = '\n.*expert password:'
#def ssh_connection():
# """ssh connection process"""
# try:
# # Connect to the host
# ssh.connect(hostname=host, username=user, password=passwd)
# interact = SSHClientInteraction(ssh, timeout=10, display=True)
# print('ok')
# except TimeoutError:
# print('surprise motherfucker')
#
#
# return interact
def commands():
"""ssh write commands"""
try:
ssh.connect(hostname=host, username=user, password=passwd)
except TimeoutError:
print('surprise motherfucker, host is down')
interact = SSHClientInteraction(ssh, timeout=10, display=True)\
print('connected')
interact.send(item)
found_index = interact.expect([not_found, prompt])
if found_index == 0:
print('command not found, trying expert mode')
interact.send('expert')
entering_expert_index = interact.expect([no_expert_pass, enter_expert_pass])
if entering_expert_index == 0:
print('no expert password, need to set it')
no_expert_output = interact.current_output_clean
# something here to write to file
elif entering_expert_index == 1:
print('cp asked for expert password, entering it')
interact.send(expert_passwd)
expert_mode_index([wrong_expert_pass, expert_prompt])
if expert_mode_index == 0:
print('wrong expert pass')
#something here to write to file
elif expert_mode_index == 1:
print('password ok, continuing')
interact.send(i)
interact.expect(expert_prompt)
interact.send('exit')
interact.expect(prompt)
for host in ip_addr_list:
print(host)
file = open('c:/python/checkpoint/{0}.txt'.format(host),"a")
# ssh_connection()
commands()
file.close()
#for item in ip_addr_list:
# host = ip_addr_list[0]
# p=pexpect.fdpexpect.fdspawn('ssh [email protected]')
# print(host)
# re.sub("^\s+|\n|\r|\s+$", '', str_all.decode())
# file = open('{0}'.format(ip_addr_list[num_ips]),"wb")
# file.write(str_all)
# num_ips += 1