如何使用Python将多个命令发送到远程服务器

时间:2017-10-05 16:15:25

标签: python pxssh

如何使用Python将多个命令发送到远程服务器?我不得不学习Python用于学习目的,我决定尝试自动收集Bacula控制台的信息。我正在尝试使用以下脚本,但我没有成功。你能告诉我哪里出错了吗?

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.")

输出:

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$

0 个答案:

没有答案