Get parameters in ssh file passed by Plink in batch [duplicate] - shell

I need to execute a shell script remotely inside the Linux box from Windows
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit
fi
echo "$1"
Here is the command I ran from Windows command prompt
cmd> plink.exe -ssh username#host -pw gbG32s4D/ -m C:\myscript.sh 5
I am getting output as
"Illegal number of parameters"
Is there any way I can pass command line parameter to shell script which will execute on remote server?

You misunderstand how the -m switch works.
It is just a way to make plink load the commands to send to the server from a local file.
The file is NOT uploaded and executed on the remote server (with arguments).
It's contents is read locally and sent to the server and executed there as if you typed it on a (remote) command line. You cannot give it arguments.
A workaround is to generate the file on the fly locally before running plink from a batch file (say run.bat):
echo echo %1 > script.tmp
plink.exe -ssh username#host -pw gbG32s4D/ -m script.tmp
Then run the batch file with the argument:
run.bat 5
The above will make the script execute echo 5 on the server.
If the script is complex, instead of assembling it locally, have it ready on the server (as #MarcelKuiper suggested) and execute just the script via Plink.
plink.exe -ssh username#host -pw gbG32s4D/ "./myscript.sh %1"
In this case, as we execute just one command, you can pass it on Plink command line, including the arguments. You do not have to use the -m switch with a (temporary) file.

I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:
You can define your script as an one liner using && in a file (I defined in one liner)
You need to run your command in <
Note: Use first EOF in quote like <<'EOF' but not the last one. Else you will see you code will behave weirdly.
Please see below.
Example:
sudo -i <<'EOF'
<your script here>
EOF
Then, finally run it using Plink:
plink -ssh username#hostname -pw password -m commands.txt

Have you tried putting the command and argument in quotes:
i.e. -m "C:\myscript.sh 5"

Related

Connecting to SSH using a bash script

I am trying to write a bash script to make things faster. Is it not possible to connect to the server with the code below in a bash script? I can't make it work, even though it works in the terminal.
#!/bin/bash -x
echo "Starting connection script"
sh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
What I get when I run is a not found output for each line in the pem file,
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: -----BEGIN: not found
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: adsnaleAFemasdsdsdnds: not foundMadfdasfdasfnda;vonraada
...
Some debug is needed.
Please change:
ssh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
to:
#!/bin/bash -x
echo "Starting connection script"
ssh -vi /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
does it produce an idea about the reason ?
you are calling sh which is kind of shell change it to ssh

Not able use lftp commands running from shellscript

I'm using set of commands from a shellscript. First command is running fine but it is moving to lftp command prompt and expecting manual input instead of running commands from shelscript. Following are the commands i'm using
lftp -e "$HOST"
lftp -u "$USER,$PWD"
lftp -e "cd /inbox"
put $file
bye
Please suggest me some solution
Using lower-case variable names to avoid conflicts with local environment variables or shell-builtins ($USER and $PWD are both builtins, so you shouldn't be setting them yourself):
lftp \
-e "cd /inbox; put $file" \
-u "$user,$pwd" \
"$host"
The point, here, is invoking lftp only once, and passing all the necessary commands to that single invocation.

Run shell script (with parameters) on Windows command line via Plink

I need to execute a shell script remotely inside the Linux box from Windows
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit
fi
echo "$1"
Here is the command I ran from Windows command prompt
cmd> plink.exe -ssh username#host -pw gbG32s4D/ -m C:\myscript.sh 5
I am getting output as
"Illegal number of parameters"
Is there any way I can pass command line parameter to shell script which will execute on remote server?
You misunderstand how the -m switch works.
It is just a way to make plink load the commands to send to the server from a local file.
The file is NOT uploaded and executed on the remote server (with arguments).
It's contents is read locally and sent to the server and executed there as if you typed it on a (remote) command line. You cannot give it arguments.
A workaround is to generate the file on the fly locally before running plink from a batch file (say run.bat):
echo echo %1 > script.tmp
plink.exe -ssh username#host -pw gbG32s4D/ -m script.tmp
Then run the batch file with the argument:
run.bat 5
The above will make the script execute echo 5 on the server.
If the script is complex, instead of assembling it locally, have it ready on the server (as #MarcelKuiper suggested) and execute just the script via Plink.
plink.exe -ssh username#host -pw gbG32s4D/ "./myscript.sh %1"
In this case, as we execute just one command, you can pass it on Plink command line, including the arguments. You do not have to use the -m switch with a (temporary) file.
I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:
You can define your script as an one liner using && in a file (I defined in one liner)
You need to run your command in <
Note: Use first EOF in quote like <<'EOF' but not the last one. Else you will see you code will behave weirdly.
Please see below.
Example:
sudo -i <<'EOF'
<your script here>
EOF
Then, finally run it using Plink:
plink -ssh username#hostname -pw password -m commands.txt
Have you tried putting the command and argument in quotes:
i.e. -m "C:\myscript.sh 5"

Escape whole find content to send it to a command over ssh

I'm trying to use a ssh command like :
ssh user#host command -m MYFILE
MYFILE is the content of a file on my local directory.
I'm using Bash. I've tried to use printf "%q", but i'd not working. MYFILE contains spaces, new lines, single and doublequotes...
Is there a way my command gets the file content ? I can't actually run anything else than command on the remote host.
How about first transferring the file to the remote machine
scp MYFILE user#host:myfile &&
ssh user#host 'command -m "$(< myfile)" && rm myfile'

sftp put command fails when in shell script

I am trying to make a shell script that creates a mysql dump and then puts it on another computer. I have already set up keyless ssh and sftp. They script below creates the mysql dump file on the local computer when it is run and doesn't throw any errors, however the file "dbdump.db" is never put on the remote computer. If I execute the sftp connection and put command by hand then it works.
contents of mysql_backup.sh
mysqldump --all-databases --master-data > dbdump.db
sftp -b /home/tim tim#100.10.10.1 <<EOF
put dbdump.db
exit
EOF
Try to use scp that should be easier in your case.
scp dbdump.db tim#100.10.10.1:/home/tim/dbdump.db
Both sftp and scp are using SSH.
Please write mput/put command into one file (file_contains_put_command) and try below command.
sftp2 -B file_contains_put_command /home/tim tim#100.10.10.1 >> log_file
Example:
echo binary > sample_file
echo mput dbdump.db >> sample_file
echo quit >> sample_file
sftp2 -B sample_file /home/tim tim#100.10.10.1 >> log_file
Your initial approach is a few characters off working though. You're telling sftp to read it's batch-commands from /home/tim -b /home/tim. So, if you change this to -b -, it should read it's batch commands from stdin.
Something along these lines, if -b /home/tim were intended to i.e. change directory remotely, you can add cd /home/tim to your here document.
mysqldump --all-databases --master-data > dbdump.db
sftp -b - tim#100.10.10.1 <<EOF
put dbdump.db
exit
EOF

Resources