Reputation: 93
I'm trying to find a means of monitoring the output to the console of a remove server over ssh, and from within Python.
Paramiko and Fabric python modules provide a good means of getting an ssh connection and executing specific commands on the remote server and getting the output from these commands.
However I don't want to execute anything I just want to "screen scrape" so to speak all the output being spitted out to the console on that machine.
Can Paramiko be used for this purpose, or does anyone know of another Python utility that can achieve this ?
Upvotes: 2
Views: 3906
Reputation: 93
Ok so I've managed to get this working using SSHClient.invoke_shell(), and monitoring it's output. Solaris hardware all come with ILOM (Integrated Lights Out Manager) configured, which is very useful getting a serial console on a machine.
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("9.9.9.9", 22, "username", "password")
channel = client.invoke_shell()
channel.settimeout(0.0)
while True:
r, w, e = select.select([channel], [], [])
try:
console_data = ""
while channel.recv_ready():
console_data += channel.recv(1024)
if len(console_data) == 0:
print "\n*** EOF\n"
break
# Search console_data for console prompt
# If found, start a serial console
if re.search("->", console_data):
channel.send("start -script SP/Console")
elif re.search("y/n", console_data):
channel.send("y\n")
elif re.search("SOME STRING ON CONSOLE", console_data):
print "Action completed"
break
except socket.timeout:
pass
channel.close()
client.close()
Above code connects to Service port on ILOM and waits for "->" prompt, once received it starts the serial console via "start -script SP/Console", and then anwsers "y" to continue prompt.
Now we have serial console and can monitor all output to this serial console, when some predefined string is output to the console I can exit.
Upvotes: 1