Write local line to remote file using python paramiko

I need to write a string to a file on a remote host using the python paramiko module. I tried various input redirection methods, but without success.

The local line in the code snippet below is populated with the result of the cat command

stdin, stdout, stderr = hydra.exec_command('cat /file.txt') localstring = stdout.read() manipulate(localstring) hydra.exec_command('cat > newfile.txt\n' + localstring + '\n') 

My script seems to freeze or get an EOF error or have no resulting line in the file at all. Note that the file has several lines.

+4
source share
2 answers

You can also use the ftp function:

 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('example.com', username='username', password='password') ftp = ssh.open_sftp() file=ftp.file('remote file name', "a", -1) file.write('Hello World!\n') file.flush() ftp.close() ssh.close() 
+4
source

cat reads from stdin . Use echo instead.

 'echo ' + localstring + ' > newfile.txt' 

If you need to repeat multiple lines, use the -e flag:

 echo -e "Line1\nLine2\n" > file.txt 
+1
source

Source: https://habr.com/ru/post/1484368/


All Articles