How can I send multiple commands to a remote server using Python? I had to learn Python for study purposes, I decided to try to automate the collection of information from the Bacula consoles. I'm trying with the following script, but I'm not having success. Could you show me where I went wrong?
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.")
Output:
xxxx@xxx:~/scripts$ python automatizador.py
bconsole Connecting to Director backup:9101 1000
OK: backup-dir Version: 5.2.12 (12 September 2012)
Enter a period to cancel a command.
*
bconsole Connecting to Director backup:9101 1000
OK: backup-dir Version: 5.2.12 (12 September 2012)
Enter a period to cancel a command.
*
bconsole Connecting to Director backup:9101 1000
OK: backup-dir Version: 5.2.12 (12 September 2012)
Enter a period to cancel a command.
*
Traceback (most recent call last): File "automatizador.py", line 31, in <module> s.logout()
File "/usr/lib/python2.7/dist-packages/pexpect/pxssh.py", line 353, in logout index = self.expect([EOF, "(?i)there are stopped jobs"])
File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 321, in expect timeout, searchwindowsize, async)
File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 345, in expect_list return exp.expect_loop(timeout)
File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 107, in expect_loop return self.timeout(e)
File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 70, in timeout raise TIMEOUT(msg) pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pxssh.pxssh object at 0x7fd642e4e8d0> command: /usr/bin/ssh args: ['/usr/bin/ssh', '-q', '-l', 'root', 'backup.adm.lojasobino.com.br'] buffer (last 100 chars): 'r Version: 5.2.12 (12 September 2012)\r\n
Enter a period to cancel a command.\r\n
st\x08\x08\x1b[K1\x08\x1b[Kexit\x08\x08\x08\x08\x1b[K'
before (last 100 chars): 'r Version: 5.2.12 (12 September 2012)\r\n
Enter a period to cancel a command.\r\n
st\x08\x08\x1b[K1\x08\x1b[Kexit\x08\x08\x08\x08\x1b[K'
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 47488
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
0: EOF
1: re.compile("(?i)there are stopped jobs")
xxxx@xxxxx:~/scripts$