How to send multiple commands to a remote server using python

Asked

Viewed 197 times

0

How can I send multiple commands to a remote server using python?

I had to learn python for reasons of study, so I decided to try to automate the console information collection.

I’m trying to use the following script, but I haven’t been successful.

Could you point me in the wrong direction?

HOST= 'server.name.com.br'

try:
    s = pxssh.pxssh()
    hostname = HOST 
    username = 'root'
    password = 'xxxxxxx' 
    s.login(hostname, username, password)
    s.sendline('bconsole')   # run a command
    s.prompt()               # match the prompt
    print(s.before)          # print everything before the prompt.
    s.prompt()               # match the prompt
    s.sendline('st')   # run a command
    s.prompt()               # match the prompt
    print(s.before)          # print everything before the prompt.
    s.prompt()               # match the prompt
    s.sendline('1')   # run a command
    s.prompt()               # match the prompt
    print(s.before)          # print everything before the prompt.
    s.prompt()               # match the prompt

    s.logout()
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")

Thank you very much for your attention.

  • What’s the problem? some error msg? the question is a bit obscure, why is using pxssh? ssh wouldn’t be enough and simpler? Do you know the plubum? and the Fabric?

  • Thanks for the answer. What happens is that it executes only the first command. it opens Bconsole, but later commands do not execute. Getting stuck in that part of the script. da these bugs here

  • About Fabric, I read about it, but I thought it would be more useful pxssh for not relying on anything external to python... maybe I was wrong.

  • Yes, Fabric is done in python, and you won’t need to install anything on the remote. I do depoloy Django, bank synchronization postrgres, and even cleaning logs on a google server, all via Fabric. Ball show. Plumbum tb is another to consider.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.