scp multiple remote files while entering password once - linux

I'm trying to write a script to copy multiple files (in multiple directories) from a remote host to my local machine.
My script is (more or less) as follows:
path1="/home/db/primary/*.xml"
path2="/tmp/log_*"
copyto="/home/pathtodesktop/Desktop/temp"
mkdir $copyto
scpcommand="scp -o StrictHostKeyChecking=no root#$address:\"$path1 $path2\" $copyto"
echo $scpcommand
$scpcommand
When I run the script, I get the following output:
scp -o StrictHostKeyChecking=no root#SERVER:"/home/db/primary/*.xml /tmp/log_*" /home/pathtodesktop/Desktop/temp
sh: syntax error: unterminated quoted string
cp: cannot stat '/tmp/log_*"': No such file or directory
The output of the echo is as expected. But when I copy the output above and run the command manually in the terminal, it works as expected with no errors.
So the ultimate question is, what am I doing wrong? The command seems to work fine when run manually in the terminal. Where is my syntax error?
Thanks for your help!

Adding set -f will prevent the wildcards in your paths from being expanded locally (although you may run in to other issues with spaces/special characters).
(You can re-enable wildcards afterwards with set +f)

Related

string is not getting converted properly while setting it from ssh command

This is my actions file which is running the ssh command to ssh into my workstation with given parameters and calling deployer.sh file.
MOUNT_ECR_LOGIN="-v /usr/bin/docker-credential-ecr-login:/usr/bin/docker-credential-ecr-login"
ACTIONS="${WORKSTATION_EC2} MOUNT_ECR_LOGIN=$MOUNT_ECR_LOGIN ./deployer.sh"
which gets converted into the following string while running:
ssh -t -t -q ec2-user#networkba-bastion ssh -q -t ec2-user#workstation MOUNT_ECR_LOGIN=-v /usr/bin/docker-credential-ecr-login:/usr/bin/docker-credential-ecr-login ./deployer.sh
Below is the error:
bash: /usr/bin/docker-credential-ecr-login:/usr/bin/docker-credential-ecr-login: No such file or directory
I am setting up the variable for deployer.sh file which is running the docker run command.
unfortunately, the MOUNT_ECR_LOGIN is assigned as -v only and not the full string with file information. Am I supposed to escape the spaces here? or we need some other solution?
updated
The issue is due to argument splitting, because of the two ssh the arguments must be quoted twice
MOUNT_ECR_LOGIN=$'\'"-v /usr/bin/docker-credential-ecr-login:/usr/bin/docker-credential-ecr-login"\''
ACTIONS="${WORKSTATION_EC2} MOUNT_ECR_LOGIN=$MOUNT_ECR_LOGIN ./deployer.sh"
previous answer was
, a quick fix could be to use arrays
ACTIONS=("${WORKSTATION_EC2}" "SOURCE_REGISTRY=$SOURCE_REGISTRY" "DEPLOYMENT_NAME=$DEPLOYMENT_NAME" "MOUNT_ECR_LOGIN=$MOUNT_ECR_LOGIN" ./deployer.sh)
and then use "${ACTIONS[#]}"

Copy file from remote server through lftp

I'm new to linux scripting. I want to copy file from remote server to current server(executing or client server),required cert & key files are already installed on my server(client server). below commands work when I execute it individually in sequence but, after Integrating into a .sh script it doesnt!
--My Script--
lftp -u username,xxx -p 2121 remoteServer.net;
set ssl:cert-file /abc/def/etc/User_T.p12;
set ssl:key-file abc/def/etc/User_T.p12.pwd;
lftp -e 'set net:timeout 10; get /app/home/atm/feed.txt -o /com/data/';
man lftp:
-f script_file
Execute commands in the file and exit. This option must be used
alone without other arguments (except --norc).
-c commands
Execute the given commands and exit. Commands can be separated
with a semicolon, `&&' or `||'. Remember to quote the commands
argument properly in the shell. This option must be used alone
without other arguments (except --norc).
Use "here document" feature of the shell:
lftp <<EOF
set...
open...
get...
EOF
Thanks lav for your suggestion, I found that my script was not executing second line so added continuation like
<< SCRIPT
& ended script with SCRIPT
removed all semi colon... Its working

"cannot execute binary file" when trying to run a shell script on linux

I am very new to linux and shell scriprting.
I am trying to run a shellscript from secure shell (ssh) on linux using following commands:
chmod +x path/to/mynewshell.sh
sh path/to/mynewshell.sh
I get this error:
path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.
Tried using this command:
bash path/to/mynewshell.sh
I get the same error.
Tried with this command: su - myusername sh path/to/mynewshell.sh
It is asking for my password and giving me this error: no such file or directory.
1.The result of cat -v path/to/mynewshell.sh is:
^#^#^#^#^#^#^#^#Rscript "$dir"/diver_script.R
done
2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:
#!/bin/bash/Rscript^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
for dir in /path/to/* ; do
^#^#^#^#^#^#^#^#Rscript "$dir"/myRscript.R
done
3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"
Please give any advice on how I can try executing the shellscript.
chmod -x removes execution permission from a file. Do this:
chmod +x path/to/mynewshell.sh
And run it with
/path/to/mynewshell.sh
As the error report says, you script is not actually a script, it's a binary file.
I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.
From a proposed duplicate:
run_me.sh.xz: run_me.sh.xz: cannot execute binary file
This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.
xz -d ./run_me.sh.xz
chmod +x ./run_me.sh # probably not necessary if you already did that before
./run_me.sh
Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.
To anyone else having the problem i had.
i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.
open the text file click on "Format" at the top, go down to "Make Plain Text" click it.
open your terminal type chmod 777 /path/to/file.sh
put in terminal: /path/to/file.sh to run it
That script is simply not a shell script.
A shell script is usually readable and contains shell code.
The output your cat command shows looks indeed like it's a binary of some sort.
As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.
You can check what it is identified as with the file command so:
file path/to/mynewshell.sh
Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?
Make sure the R scripts point to the right R script executioner.
In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

Getting "Unknown command" when using "lftp -e"

I'm working on a bash script to automate FTP sessions, so I can run the same commands on multiple servers automatically)
lftp -u username,password ip_address -e **FILE_WITH_COMMANDS**
So the problem is that I somehow can't use a file with -f because I get an error like this:
Unknown command `commands'.
Does anybody know how to get around this problem?
Thank you very much!
To execute commands loaded from a file, use the -f switch:
-f execute commands from the file and exit
The -e switch is for executing a command specified on the command-line:
-e execute the command
So when you use -e commands, the lftp interprets it as a request to run the commands command. And there's no commands command, hence the error.
See also https://lftp.yar.ru/lftp-man.html

Why this Bash script runs on a machine but fails on another?

I have written a program which needs to access the I/O ports of my motherboard so it needs root permission. I wanted to run this program at system start up but since the Ubuntu system start up cannot run applications with root permission I wrote these two simple files:
askpass.sh
export SUDO_ASKPASS="/var/www/Bash/mits/getpass.sh";
sudo -A -E /home/mits/QtProjects/HandST/HandST // this is the address of my application
getpass.sh
echo 'P54_99**' //this is my password
as it is appear in the askpass.sh file, I used SUDO_ASKPASS as stated in sudo manual to run my application with sudo password located in anther file.
when I call askpass.sh in my laptop it works fine and the applications will start, but when I run this script in my PC server located in my office it gives the following error.(I use remote access to reach to my server )
./askpass.sh
**./askpass.sh: line 5: $'\r': command not found**
sudo: unable to run /var/www/Bash/mits/getpass.sh: Exec format error
Sorry, try again.
sudo: unable to run /var/www/Bash/mits/getpass.sh: Exec format error
Sorry, try again.
sudo: unable to run /var/www/Bash/mits/getpass.sh: Exec format error
Sorry, try again.
sudo: 3 incorrect password attempts
what is this $'\r' variable (I don't have anything like that in my code ! it seems like \n at the end of the files or something like that !!!) !! these two files are exactly the same with my laptop files only the path and the password is changed for the server but why it fails to run on my server ? I am sure the password is modified to be the server password and I tested that many times. but I don't know why it gives error on the password too :(
I also tried to put my application path in rc.local which is run with root permission but it also failed.
I only want to try this one as a solution so I would be happy to propose your solution for this type of start up handling.
thanks in advance.
You appear to have embedded windows line endings (\r\n) in your shell script, you can run the command dos2unix on the file and that should fix it. If you don't have dos2unix you can use tr and something like,
tr -d '\r' < askpass.sh > out.sh
Then
mv out.sh askpass.sh
Try add this as the first line of Your script
#!/bin/bash
Maybe you have on the other machine set different shell. Try check it by running
env | grep SHELL

Resources