Выполнение команд/скриптов на удаленных серверах
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
Комментарии
Отправить комментарий