Commit 372b2573 authored by Simon Redman's avatar Simon Redman

Add helpers to annotate a netgraph with ssh sessions

parent 5bff8173
......@@ -88,6 +88,35 @@ def log_in_many_sessions(hostnames, usernames, passwords=None, ssh_options=DEFAU
return sessions, passwords
def network_graph_login(netgraph, username):
Log in to each host in the graph using the given username and annotate the graph with the new session
:param netgraph: networkx graph of the network topology
:param username: username to log in to each host as
:return: None
hostnames = [node for node in netgraph.nodes]
usernames = [username for i in range(len(hostnames))]
sessions, passwords = log_in_many_sessions(hostnames, usernames)
for host_idx in range(len(hostnames)):
netgraph._node[hostnames[host_idx]]['session'] = sessions[host_idx]
def network_graph_logout(netgraph):
Log out of every host in the network and remove the sessions from the netgraph
:param netgraph: networkx graph, annotated with logged-in SSH sessions
:return: None
for node in netgraph._node:
session = netgraph._node[node]['session']
del netgraph._node[node]['session']
def get_output(session, encoding=sys.stdout.encoding):
Decode the raw bytes written by the SSH session
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment