-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathssh_connect.py
78 lines (73 loc) · 2.69 KB
/
ssh_connect.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
"""
This module provides ssh session for users.
"""
from lib.objects.basenode import BaseNode
from processdb import process_nodes
from processdb import process_templates
from search import node_element
from search import search_node
from search import search_template
from node_create import node_create
from multithread import multithread_engine
from get_property import get_port
import initialize
import subprocess
import os
def ssh_connect(args):
argument_node = args.name
auditcreeper = False
commands = initialize.configuration
username = os.environ.get('USERNAME')
"""
:param argument_node: Argument accepted as regular expression.
:type augument_node: str
:param auditcreeper: When auditcreeper is active/non-active.
:type auditcreeper: bool
:param commands: Referenced to global variable commands which keeps track of all commands per node.
:type commands: list
:param username: Pulled from environment variable.
:type ext: str
"""
node_object = process_nodes()
match_node = search_node(argument_node,node_object)
"""
:param node_object: All node(s) in the database with all attributes.
:type node_object: list
:param match_node: Nodes that matches the arguements passed in by user.
:type match_node: list
"""
try:
if len(match_node) == 0:
print('+ No matching nodes found in database.')
print('')
else:
node_element(match_node,node_object)
id = 1
ssh_id = 0
print('{} {: >27} {: >28} {: >26}'.format('id','name','address','platform'))
for index in initialize.element:
print('{id: {align}{space}} {name: {align}{space}} {mgmt_ip4: {align}{space}} {platform_name: {align}{space}}'.format(id = id,name = node_object[index]['name'],mgmt_ip4 = node_object[index]['mgmt_ip4'],platform_name = node_object[index]['hardware_vendor'],align = '<',space = 25))
id = id + 1
port = get_port(node_object,initialize.element,ssh_id)
try:
if(len(initialize.element) == 1):
try:
subprocess.call('ssh admin@{} -p {}'.format(node_object[initialize.element[ssh_id]]['mgmt_ip4'],port), shell=True)
except KeyboardInterrupt as error:
print('Terminating...')
else:
try:
ssh_id = int(input('Enter ID to SSH to: '))
ssh_id = ssh_id - 1
if ssh_id + 1 < 1:
print('IndexError: incorrect connection id')
except KeyboardInterrupt as error:
print('')
print('Terminating...')
else:
port = get_port(node_object,initialize.element,ssh_id)
subprocess.call('ssh admin@{} -p {}'.format(node_object[initialize.element[ssh_id]]['mgmt_ip4'],port), shell=True)
except IndexError:
print('IndexError: incorrect connection id')
except ValueError as error:
print('ValueError: expected an integer')