К основному контенту

Python remote commands


Выполнение команд/скриптов на удаленных серверах

https://rtfm.co.ua/python-modul-fabric/
http://www.fabfile.org

или использовать paramico
       

  import paramiko 
  res_list = list()
  i = 0  # counter
  while True:
    plpy.notice('Trying to connect to %s (%i from %i times)" % (host_ip, i, retry_time)')
    try:
      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect(hostname=host_ip, port=port, username=username, password=userpass)
      break
    except paramiko.AuthenticationException as e:
      plpy.error('Authentication failed when connecting to %s (%s)' % (host_ip, e.message))
      return None
    except Exception as e:
      plpy.error("Could not SSH to %s, waiting for it to start (%s)" % (host_ip, str(e)))
      i += 1
      time.sleep(2)

    # If we could not connect within time limit
    if i >= retry_time:
      plpy.error('Could not connect to %s. Giving up' % host_ip)
      return None

  # After connection is successful
  # Send the command
  for command in cmd_list:
    plpy.notice('> ' + command)
    # execute commands
    stdin, stdout, stderr = ssh.exec_command(command)
    while True:
      buf = stdout.readlines()
      if not buf:
        break
      res_list.extend(buf)
    if len(res_list) > 0:
      plpy.notice('Result: %i rows' % len(res_list))

  # Close SSH connection
  ssh.close()
  return res_list

       
 


Комментарии

Популярные сообщения из этого блога

Repeat Linux command every x seconds

watch -n 5 "ps -ef | grep COPY" watch -n 10 "psql -U postgres -c \"select clock_timestamp() - query_start as duration, substr(query,1,50) from pg_stat_activity where pid <> pg_backend_pid() and state='active' order by 1 desc\"" for i in {1..10}; do ps -ef | grep COPY; date ; sleep 5; done while true; do ps -ef | grep COPY ; date ; sleep 5; done