SCP with -J option gives invalid argument error - bash

I'm trying to send files using shell script like below
#!/bin/bash
scp -J "JumpHostUser#JumpHostIp -i KeyFile" -i "$HOME/KeyFile2" \
-r ~/FolderToSend "DestinationUser#DestinationIP:~/"
First problem is when I enter command in shell directly, it works.
But when I execute shell script using sh command it gives
Warning: Identity file KeyFile not accessible: No such file or
directory.
So I specified the path like below
#!/bin/bash
scp -J "JumpHostUser#JumpHostIp -i /home/JumpHostUser/KeyFile" \
-i "$HOME/KeyFile2" -r ~/FolderToSend "DestinationUser#DestinationIP:~/"
Second problem is it gives
Invalid -J argument error
Wrote a shell script using command that does work in shell.
Specified the path result in Invalid -J argument error

Related

sshpass want to use parameter of sftp

Hi i created following script to initialize my storage box to use rsync without password later. Last year it works if i remember correct...
cat .ssh/id_rsa.pub >> .ssh/storagebox_authorized_keys
echo -e "mkdir .ssh \n chmod 700 .ssh \n put $.ssh/storagebox_authorized_keys" \
".ssh/authorized_keys \n chmod 600 .ssh/authorized_keys" | sshpass -p ${storage_password} \
sftp -P ${storage_port} -i .ssh/id_rsa ${storage_user}#${storage_address}
today I get following error:
sshpass: invalid option -- 'i'
but the parameter -i belongs to sftp and not sshpass - is there an possibility to parse the parameters in the correct way?
edit: i switched the position of
-i .ssh/id_rsa ${storage_user}#${storage_address}
and get this error
sshpass: Failed to run command: No such file or directory
edit: it seems like an sftp problem...
after discussion, updating answer to properly support automation
step 1:
create an sftp "batch file" e.g: ~/.ssh/storage-box_setup.sftp
mkdir .ssh
chmod 700 .ssh
put /path/to/authorized_keys_file ".ssh/authorized_keys
chmod 600 .ssh/authorized_keys
/path/to/authorized_keys_file is a file containing public keys of ONLY the keys that should have access to your storage box (.ssh/storagebox_authorized_keys)
step 2:
update automation script command to
sshpass -p <password> -- sftp -P <port> -b ~/.ssh/storage-box_setup.sftp user#host
the -b flag was the answer you needed.
refer: man sftp
-b batchfile
Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it should be used in conjunction with non-interactive authentication.
--
sshpass -p ${storage_password} -- \
sftp -P ${storage_port} -i .ssh/id_rsa \
${storage_user}#${storage_address}
the -- before sftp is a way to tell sshpass (and most other programs) to stop parsing arguments.
everything after -- is assumed as the last argument, which in the case of sshpass is the command to be executed ssh -i ~/.id_rsa ...
in case you're wondering switching the position of -i tells sshpass to execute -i as a program and hence fails with command not found

psql return value / error killing the shell script that called it?

I'm running several psql commands inside a bash shell script. One of the commands imports a csv file to a table. The problem is, the CSV file is occasionally corrupt, it has invalid characters at the end and the import fails. When that happens, and I have the ON_ERROR_STOP=on flag set, my entire shell script stops at that point as well.
Here's the relevant bits of my bash script:
$(psql \
-X \
$POSTGRES_CONNECTION_STRING \
-w \
-b \
-L ./output.txt
-A \
-q \
--set ON_ERROR_STOP=on \
-t \
-c "\copy mytable(...) from '$input_file' csv HEADER"\
)
echo "import is done"
The above works fine as long as the csv file isn't corrupt. If it is however, psql spits out a message to the console that begins ERROR: invalid byte sequence for encoding "UTF8": 0xb1 and my bash script apparently stops cold at that point-- my echo statement above doesn't execute, and neither do any other subsequent commands.
Per the psql documentation, a hard stop in psql should return an error code of 3:
psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g. out of >memory, file not found), 2 if the connection to the server went bad and the session was not >interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set
That's fine and good, but is there a reason returning a value of 3 should terminate my calling bash script? And can I prevent that? I'd like to keep ON_ERROR_STOP set to on because I actually have other commands I'd like to run in that psql statement if the intial import succeeds, but not if it doesn't.
ON_ERROR_STOP will not work with the -c option.
Also, the $(...) surronding the psql look wrong — do you want to execute the output as a command?
Finally, you forgot a backslash after the -L option
Try using a “here document”:
psql \
-X \
$POSTGRES_CONNECTION_STRING \
-w \
-b \
-L ./output.txt \
-A \
-q \
--set ON_ERROR_STOP=on \
-t <<EOF
\copy mytable(...) from '$input_file' csv HEADER
EOF
echo "import is done"

Unable to fetch line from crontab -l via pssh command

I have a file /tmp/hostlist containing hosts 1000+ . I am trying to fetch line which has particular word from crontab -l on 1000+ hosts via pssh command to expedite the task, but unable to fetch the line
I am trying below command. The reason for using echo \$(echo ) is to display the output in same line when triggering more than one OS command
pssh -h /tmp/hostlist -i "echo \$(echo ), \$(crontab -l|grep root)"
The above command works fine when i replaced "crontab -l|grep root" with any OS Command , Having an issue with crontab -l -> It doesn't display the expected output.
Just to add-
The above command works when using only one Command as below.
pssh -h /tmp/hostlist -i 'crontab -l|grep -i root' --> Works fine
Is there any possibility to use more than one command and display output in one line with crontab -l command like below.
pssh -h /tmp/hostlist -i "echo \$(echo ), \$(crontab -l|grep root), \$(uptime)" --> doesn't work or doesn't give expected output of crontab -l|grep root
and when using below without crontab command works fine.
pssh -h /tmp/hostlist -i "echo \$(echo ), \$(uname -a), \$(uptime)" --> Works fine

Syntax error when calling variable in bash

Here is my code:
#!bin/bash
id=$(sshpass -p password ssh -tt username#ipaddress -p PORT "grep --include=\*.cr -rlw '/usr/local/bin/' -e '$1' | cut -c16-")
echo $id
sshpass -p password rsync -avHPe 'ssh -p PORT' username#ipaddress:/usr/local/bin/"$id" /usr/local/bin/
id echos correctly, but I get an rsync error when trying to call the variable.
If I manually populate and run rsync, the command works, so I'm not sure what is going on.
Rsync gives me the following output on error.
rsync: link_stat "/usr/local/bin/match.cr\#015" failed: No such file or directory (2)
It seems to be grabbing extra characters? Any help is appreciated :)
Looks like your file contains Windows specific "CR LF" characters. You need to convert these to Linux specific "LF" characters in your script. You can use a tool like dos2unix or Notepad++.

Current directory appended to windows cmd passed in "parameter" causing invalid argument

I am trying to run a gimp batch command in windows cmd.
"C:\Program Files (x86)\GIMP-2.0\bin\gimp-console-2.6.exe" -i -b --verbose "(maketemplates \"C:\\autocovers\\sample\")" -b "(gimp-quit 0)"
I get the following:
GIMP-Error: Opening 'C:\Users\IOANNIS(maketemlpates "C:\autocovers\sample")' failed: Invalid argument
The argument does indeed seem invalid as it has the current directory appended to it! Any idea why this is happening?
I don't have GIMP installed on my Windows test boxes, so I can't test this, but I suspect that the --verbose option might be causing the issue.
The option -b expects an argument, but in your command-line you placed the option --verbose between -b and its argument, which would make the string --verbose the argument of -b. That leaves the intended argument "(maketemplates \"C:\\autocovers\\sample\")" as a non-option parameter. As documented, gimp-console considers all non-option parameters as file names.
Change your command-line from
"C:\Program Files (x86)\GIMP-2.0\bin\gimp-console-2.6.exe" -i -b --verbose "(maketemplates \"C:\\autocovers\\sample\")" -b "(gimp-quit 0)"</code>
to
"C:\Program Files (x86)\GIMP-2.0\bin\gimp-console-2.6.exe" --verbose -i -b "(maketemplates \"C:\\autocovers\\sample\")" -b "(gimp-quit 0)"</code>
and your problem should go away.

Resources