how to execute an local script in remote server with parameters
Use the -s
option, which forces bash
(or any POSIX-compatible shell) to read its command from standard input, rather than from a file named by the first positional argument. All arguments are treated as parameters to the script instead.
ssh user@remote-addr 'bash -s arg' < test.sh
Pass arguments to a bash script stored locally and needs to be executed on a remote machine using Python Paramiko
Do the same, what you are doing in the bash
:
command = "/bin/bash -s {v1} {v2}".format(v1=var1, v2=var2)
stdin, stdout, stderr = c.exec_command(command)
stdin.write(mymodule)
stdin.close()
If you prefer the heredoc syntax, you need to use the single quotes, if you want the argument to be expanded:
command = "/bin/bash -s {v1} {v2} <<'EOF'\n{s}\nEOF".format(v1=var1,v2=var1,s=mymodule)
stdin, stdout, stderr = c.exec_command(command)
The same way as you would have to use the quotes in the bash:
ssh -i key user@IP bash -s "$var1" "$var2" <<'EOF'
echo $1
echo $2
EOF
Though as you have the script in a variable in your Python code, why don't you just modify the script itself? That would be way more straightforward, imo.
Obligatory warning: Do not use AutoAddPolicy
– You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
Execute local script on remote server using non-default shell with Python Paramiko
The #!/bin/bash
is a comment. Sending it to a remote shell as a command has no effect.
You have to execute /bin/bash
on the server and send your script to it:
stdin, stdout, stderr = ssh.exec_command("/bin/bash", timeout=15)
stdin.write(my_script)
Also, you have to exit
the shell at the end of your script, otherwise it will never end.
Related question:
Pass arguments to a bash script stored locally and needs to be executed on a remote machine using Python Paramiko
execute local script on remote server in expect
Try like this:
spawn bash -c "ssh $idNhost bash -s < a.sh"
Executing a local script on a remote Machine
You need to override the arguments:
echo 'set -- arg; cd /place/to/execute' | cat - test.sh | ssh -T user@hostname
The above will set the first argument to arg
.
Generally:
set -- arg1 arg2 arg3
will overwrite the $1
, $2
, $3
in bash.
This will basically make the result of cat - test.sh
a standalone script that doesn't need any arguments`.
Related Topics
How to Run Multiple Tor Processes at Once with Different Exit Ips
Svn Checkout the Contents of a Folder, Not the Folder Itself
How to Set Up Curl to Permanently Use a Proxy
How to Find Out Why My Storage Space on Amazon Ec2 Is Full
Installed Go Binary Not Found in Path on Alpine Linux Docker
Amazon Linux: "Apt-Get: Command Not Found"
Linux Command or Script Counting Duplicated Lines in a Text File
Turning Multiple Lines into One Comma Separated Line
Compile/Run Assembler in Linux
When to Use Pipes VS When to Use Shared Memory
How to Check in Bash Whether a File Was Created More Than X Time Ago
Unix - Count of Columns in File
Create a Dedicated Folder for Every Zip Files in a Directory and Extract Zip Files
How to Map a Hostname *And* a Port with /Etc/Hosts