Unable to transfer file from master node to minion nodes using sftp in a python script

2/1/2022

I am trying to send a file from the master node to minion nodes using a python script but a single error OSError: Failure keeps on coming up.

I tried to code this file to send this file from one local machine to another local machine.

My code:

    #! /usr/bin/python
    #! /usr/bin/python3
    
    import  paramiko
    import os
    
    #Defining working connect
    def workon(host):
        #Making a connection
        ssh_client = paramiko.SSHClient()
        ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())  #To add the missing host key and auto add policy
        ssh_client.connect(hostname = host, username = 'username', password = 'password')
        
        ftp_client = ssh_client.open_sftp()
        ftp_client.put("/home/TrialFolder/HelloPython", "/home/")
        ftp_client.close()
    
        #stdin, stdout, stderr = ssh_client.exec_command("ls")
        #lines = stdout.readlines()
        #print(lines)
    
    def main():
        hosts = ['192.16.15.32', '192.16.15.33', '192.16.15.34']
        threads = []
        for h in hosts:
            workon(h)
    		
    main()

		

Error:

Traceback (most recent call last):
  File "PythonMultipleConnectionUsinhSSH.py", line 28, in <module>
    main()
  File "PythonMultipleConnectionUsinhSSH.py", line 26, in main
    workon(h)
  File "PythonMultipleConnectionUsinhSSH.py", line 15, in workon
    ftp_client.put("/home/Sahil/HelloPython", "/home/")
  File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 759, in put
    return self.putfo(fl, remotepath, file_size, callback, confirm)
  File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 714, in putfo
    with self.file(remotepath, "wb") as fr:
  File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 372, in open
    t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 813, in _request
    return self._read_response(num)
  File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 865, in _read_response
    self._convert_status(msg)
  File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 898, in _convert_status
    raise IOError(text)
OSError: Failure
-- Sahil Sharma
kubernetes
python
python-3.x

1 Answer

2/1/2022

First, you should make sure the target directory /home/ is writable for you. Then you should review documentation for the put method. It says this about the second argument (remotepath):

The destination path on the SFTP server. Note that the filename should be included. Only specifying a directory may result in an error.

Try including the filename in the path, like:

...
ftp_client.put("/home/TrialFolder/HelloPython", "/home/HelloPython")
...
-- rayt
Source: StackOverflow