Я пытаюсь скопировать файл с удаленного компьютера в системе Linux.Для этого мне нужно использовать pxssh из pexpect.Я вхожу в систему, пишу и читаю с помощью этих функций:
def log_SC(host, user, password):
s = pxssh.pxssh()
hostname = host
username = user
password = password
test = 1
while test:
try:
s.login(hostname, username, password, sync_multiplier=10)
text = read_in_SC(s)
# print(text) # print everything before the prompt.
if text.find('Bienvenue sur') != -1:
test = 0
else:
s.logout()
except Exception as e:
print("pxssh failed on login. think about putting a higher number on sync_multiplier' (AS0002) :" + str(e))
#s.logout()
s.close()
time.sleep(3)
s = pxssh.pxssh()
return s
def write_in_SC(s, command):
text = 0
try:
s.sendline(command) # run a command
s.prompt() # match the prompt
text = read_in_SC(s)
except Exception as details:
print('Not able to write on super computer (AS0005) :' + str(details))
return text # returns text if everything went as supposed to, 0 if not
def read_in_SC(s):
text = 0
try:
text = translate_read(s.before)
except Exception as details:
print('Not able to read on super computer (AS0001) :' + str(details))
return text # returns a string if everything went all right, 0 if not
Чтобы сделать оттуда копию файла, я просто пишу команду cat
с my_txt = write_in_SC(s, 'cat the_file.txt')
и переставляю my_txt
в функционалеспособ написать это в локальном файле, но для меня это немного грязный код, и я чувствую, что есть лучший способ сделать все это.Как бы вы это сделали?