2

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 ?

5
  • What is the operating system of the remote machine? Commented Sep 11, 2014 at 17:59
  • If this is linux, the screen utility can be helpful, depending on what you are trying to do. You can use screen on your local machine and then login to remote. Then, again on your local computer, use screen to attach to that same session again - now you have two. Or, you can use screen on the remote and log to a file. Now you can monitor by tailing the file. Commented Sep 11, 2014 at 17:59
  • Are you interested in syslog messages only, or do you also want to see the output of commands running on the console? Commented Sep 11, 2014 at 18:02
  • Operating system on both machines is Solaris. Interested in seeing all output to the console both system and commands run. Screen whilst I have used on Solaris requries some level of user interaction. I'd like to achieve this automatically using python. Commented Sep 12, 2014 at 7:06
  • I thought Paramiko's SSHClient.invoke_shell() would work, and then call this channel's recv() method to read stdout, but this does not connect to the console it simply creates an interactive shell on a pseudo terminal. Commented Sep 12, 2014 at 9:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.