0

Basiclly I'm trying to run sh script on linux server by running python script on my local machine but I have no idea how to pass the 1 or 0. I checked if it runs sh properly when it just has to create folder without any passing and it worked - also with sh script but I only needed to run it. SH

echo "Enter 1 to create a folder named 'Hello'"
echo "Enter 0 to exit"

read choice

if [ $choice -eq 1 ]
then
    mkdir Hello
    echo "Folder 'Hello' created!"
elif [ $choice -eq 0 ]
then
    exit
else
    echo "Invalid choice"
fi

I also checked above script by running it manual and it also worked fine. And the python function I'm trying to use:

from multiprocessing import Pool
import paramiko
import shlex


def log_to_machine(machines):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(machines, username = 'pluszczu', password = 'pluszczu1')
    script_run(ssh)
    ssh.close()
    
def mp(machines):

    p = Pool()
    result =p.map(log_to_machine, machines)
        
    p.close()
    p.join()
    
   

def script_run(ssh):
    path = '/home/pluszczu/logs'
    ssh.exec_command(f'cd {shlex.quote(path)} && ./zac.sh 1')


if __name__ == '__main__':
    machines = [
                r'192.168.56.102', 
                
                ]
    
    
    mp(machines)

Rest of the code is fine. I also tried like && echo 1, $1, echo $1 but with the same resut. Any ideas how can I pass that value?

  • `read` reads from stdin. You're passing the 1 on the argument vector, not in stdin; it's in `$1`, so `choice=$1` will assign it to the variable named `choice`. – Charles Duffy Mar 31 '23 at 21:08
  • BTW, when you generate a string to pass to `ssh.exec_command()` or anything else that invokes a shell, you should use `shlex.quote()` to ensure that values you substitute into that string are correctly escaped. That is to say, `f'cd {shlex.quote(path)} && ./zac.sh 1'`. – Charles Duffy Mar 31 '23 at 21:10
  • (the question text claims that you got "the same result" using `$1`, but you need to prove that to us by _showing_ us the exact code that didn't work and the exact process you used to determine that it doesn't work, since it _actually is_ the correct invocation). – Charles Duffy Mar 31 '23 at 21:11
  • ...one way to have it "not work", f/e, would be to never read the stdout of your executed command so it gets stuck trying to run the `echo` and never gets to the `mkdir`. If it _is_ the problem you're hitting, though, that's a problem with your Python and correct use of the paramiko library, not with your shell script or even with how you're passing arguments to that script. Assign the `ssh.exec_command()` returned channel to a variable, and actually read stdout/stderr and the exit status from that channel. – Charles Duffy Mar 31 '23 at 21:12
  • @CharlesDuffy I meant ssh.exec_command(f'cd {path} && ./zac.sh $1') I was trying to pass it like that, also checked (f'cd {shlex.quote(path)} && ./zac.sh $1') and without $. Still folder did not create but code did not return any errors etc – Mateusz Plsz Mar 31 '23 at 21:23
  • Well, yes, of course that won't work. How is the value you want going to get into the `$1` of the remote shell? Just `./zac.sh 1` is correct if the value is always supposed to be 1. If you want it to be set based on a Python variable... well, your Python code doesn't tell us anything about your Python variables, so how are we supposed to figure out how to transform it? – Charles Duffy Mar 31 '23 at 21:32
  • Anyhow -- right now, you're _assuming_ that the reason your code doesn't do the `mkdir` is because the argument wasn't passed, but you're doing absolutely nothing to validate that assumption. Try changing it to do a `mkdir Goodbye` when the argument isn't passed; if you don't see _either_ directory being created, then you know that the assumption that it was an argument-passing problem is wrong and should probably start investigating other avenues, like the one I told you to investigate where the remote process is hung trying to write to its stdout. – Charles Duffy Mar 31 '23 at 21:36
  • BTW, just adding more code does not make something a [mre]. Note the "minimal" part: We want the **shortest possible** code we can run with no changes whatsoever and see the same problem ourselves. – Charles Duffy Mar 31 '23 at 21:37
  • @CharlesDuffy I added the full code, I know that ./zac.sh 1 should work but for some reason folder isnt created – Mateusz Plsz Mar 31 '23 at 21:37
  • I already told you, _twice_, what I think the most likely reason that folder is not created might be. Try actually reading what I write. – Charles Duffy Mar 31 '23 at 21:37
  • As is said in th question I TRIED mkdir Hello in script and the code works totally fine. – Mateusz Plsz Mar 31 '23 at 21:38
  • (again, though, `$1` is completely wrong; you should be passing `1`, not `$1`) – Charles Duffy Mar 31 '23 at 21:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252909/discussion-between-charles-duffy-and-mateusz-plsz). – Charles Duffy Mar 31 '23 at 21:38

0 Answers0