forked from jamiecaesar/securecrt-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_sessions_from_csv.py
125 lines (107 loc) · 5.06 KB
/
import_sessions_from_csv.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# $language = "python"
# $interface = "1.0"
import os
import sys
import logging
import csv
# Add script directory to the PYTHONPATH so we can import our modules (only if run from SecureCRT)
if 'crt' in globals():
script_dir, script_name = os.path.split(crt.ScriptFullName)
if script_dir not in sys.path:
sys.path.insert(0, script_dir)
else:
script_dir, script_name = os.path.split(os.path.realpath(__file__))
# Now we can import our custom modules
from securecrt_tools import scripts
from securecrt_tools import utilities
# Create global logger so we can write debug messages from any function (if debug mode setting is enabled in settings).
logger = logging.getLogger("securecrt")
logger.debug("Starting execution of {0}".format(script_name))
# ################################################ SCRIPT LOGIC ###################################################
def script_main(script):
"""
Author: Michael Ethridge
Email: [email protected]
This script will import a list of sessions to create in SecureCRT from a CSV file. It does not connect to any
devices.
:param script: A subclass of the sessions.Session object that represents this particular script session (either
SecureCRTSession or DirectSession)
:type script: scripts.Script
"""
# Get input CSV, must contain Session Name and IP. Can also have
# Protocol and folder
sessions_csv = ""
sessions_csv = script.file_open_dialog("Please select your CSV Import file", "Open", sessions_csv,
"CSV Files (*.csv)|*.csv|")
# Check if got an input file name or not
if sessions_csv != "":
# Set couters
count = 0
skipped = 0
# Open our input file
with open(sessions_csv, 'rb') as csv_import_file:
# Read in CSV as DICT
import_reader = csv.DictReader(csv_import_file)
# Process each row and create the session
for row in import_reader:
# If we don't have a hostname / IP skip the row
if row['hostname'] == "":
skipped += 1
continue
# If session name is blank, set it to hostname / IP
if row['session_name'] == "":
row['session_name'] = row['hostname']
# If protocol is blank set to SSH2
if row['protocol'] == "":
row['protocol'] = "SSH2"
# If folder is blank set to '_imports'
if row['folder'] == "":
row['folder'] = "_imports"
# Create Session
script.create_new_saved_session(row['session_name'], row['hostname'], row['protocol'], row['folder'])
count += 1
# Display summary of created / skipped sessions
setting_msg = "{0} sessions created\n{1} sessions skipped (no Hostname / IP)".format(count, skipped)
script.message_box(setting_msg, "Sessions Created", scripts.ICON_INFO)
else:
# We didn't get an input file so ask to generate an example and exit.
result = script.message_box("Do you want to generate an example CSV file?", "Generate CSV",
scripts.ICON_QUESTION | scripts.BUTTON_YESNO)
if result == scripts.IDNO:
return
else:
# Create an example input filename by replacing .py in script
# name with .csv
example_file = os.path.normpath(os.path.join(script_dir, script_name.replace(".py", ".csv")))
# Write out example
with open(example_file, 'wb') as ex_file:
exWriter = csv.writer(ex_file)
exWriter.writerow(
['session_name', 'hostname', 'protocol', 'folder']
)
exWriter.writerow(
['switch1', '10.10.10.10', 'SSH2',
'Customer1/Site1/Building1/IDF1']
)
exWriter.writerow(
['switch2', '10.10.20.10', 'SSH2',
'Customer1/Site1/Building1/IDF2']
)
exWriter.writerow(
['router1', '10.10.10.1', 'SSH2',
'Customer1/Site1/Building1/IDF1']
)
# Show where example file was created
setting_msg = (
"No input file selected\n"
"Example Import file, {0}, created in directory:\n{1}\n\n").format(example_file, script_dir)
script.message_box(setting_msg, "Example Input Created", scripts.ICON_INFO)
# ################################################ SCRIPT LAUNCH ###################################################
# If this script is run from SecureCRT directly, use the SecureCRT specific class
if __name__ == "__builtin__":
crt_script = scripts.CRTScript(crt)
script_main(crt_script)
# If the script is being run directly, use the simulation class
elif __name__ == "__main__":
direct_script = scripts.DebugScript(os.path.realpath(__file__))
script_main(direct_script)