<p>The bash script I'm trying to run on the K8S cluster node from a proxy server is as below:</p>
<pre><code>#!/usr/bin/bash
cd /home/ec2-user/PVs/clear-nginx-deployment
for file in $(ls)
do
    kubectl -n migration cp $file clear-nginx-deployment-d6f5bc55c-sc92s:/var/www/html
done
</code></pre>
<p>This script is not copying data which is therein path <code>/home/ec2-user/PVs/clear-nginx-deployment</code> of the master node.</p>
<p>But it works fine when I try the same script manually on the destination cluster.</p>
<p>I am using python's <code>paramiko.SSHClient()</code> for executing the script remotely:</p>
<pre><code>def ssh_connect(ip, user, password, command, port):
    try:
        client = paramiko.SSHClient()

        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip, username=user, password=password, port=port)

        stdin, stdout, stderr = client.exec_command(command)

        lines = stdout.readlines()

        for line in lines:
            print(line)

    except Exception as error:
        filename = os.path.basename(__file__)
        error_handler.print_exception_message(error, filename)

    return
</code></pre>
<p>To make sure the above function is working fine, I tried another script:</p>
<pre><code>#!/usr/bin/bash
cd /home/ec2-user/PVs/clear-nginx-deployment
mkdir kk
</code></pre>
<p>This one runs fine with the same python function, and creates the directory 'kk' in desired path.
If you could please suggest the reason behind, or suggest an alternative to carry out this.
Thank you in advance.</p>

kkpareek
<p>The issue is now solved.</p>
<p>Actually, the issue was related to permissions which I got to know later. So what I did to resolve is, first <code>scp</code> the script to remote machine with:</p>
<p><code>scp script.sh user@ip:/path/on/remote</code></p>
<p>And then run the following command from the local machine to run the script remotely:</p>
<p><code>sshpass -p &quot;passowrd&quot; ssh user@ip &quot;cd /path/on/remote ; sudo su -c './script.sh'&quot;</code></p>
<p>And as I mentioned in question, I am using python for this.</p>
<p>I used the <code>system</code> function in <code>os</code> module of python to run the above commands on my local to both:</p>
<ol>
<li>scp the script to remote:</li>
</ol>
<pre><code>import os

command = &quot;scp script.sh user@ip:/path/on/remote&quot;
os.system(command)
</code></pre>
<ol start="2">
<li>scp the script to remote:</li>
</ol>
<pre><code>import os

command = &quot;sshpass -p \&quot;passowrd\&quot; ssh user@ip \&quot;cd /path/on/remote ; sudo su -c './script.sh'\&quot;&quot;
os.system(command) 
</code></pre>
