Passing Config Content to SSH Command - shell

Normally we pass config file via -F argument:
$ ssh -F my.conf
Is there any way that I can pass content of my.conf to ssh without creating config file?

You can write the config file content to a temporary file and delete it after using it for your ssh connection:
mktemp | xargs -t -I {} sh -c 'echo "your ssh config content" > {}; ssh -F {} HOST; rm {}'

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

Expect Script scp latest file in directoy

I am trying to scp latest file from remote machine however getting below error;
can't read "(ssh root#1.1..... "ls -t /test/*txt | head
-1")": no such variable
my expect script ;
spawn scp -r root#$remote_ip:/test/$(ssh root#$remote_ip "ls -t /test/*txt | head -1") /mypath
how should I get latest file from remote machine with expect script?
$(...) are shell syntax. To perform the same functionality in Tcl/expect, use the exec command.
spawn scp -r root#$remote_ip:/test/[exec ssh root#$remote_ip "ls -t /test/*txt | head -1"] /mypath
It doesn't have to be a single line, for maintainability, split it
set latest [exec ssh root#$remote_ip "ls -t /test/*txt | head -1"]
spawn scp -r root#$remote_ip:/test/$latest /mypath
However, I suspect you're using expect to send the passwords, either:
spawn ssh root#$remote_ip "ls -t /test/*txt | head -1"
expect "password"
send "$passwd\r"
expect eof
# parse $expect_out(buffer) to extract the file
But, your life will be much easier if
you set up ssh key authentication and avoid expect altogether:
ssh-keygen
ssh-copy-id root#$remote_ip
latest=$(ssh root#$remote_ip "ls -t /test/*txt | head -1")
scp -r root#$remote_ip:/test/$latest /mypath
scp -r root#$remote_ip:ssh root#$remote_ip ls /test/* -1td | head -1 /mypath/.

Environment variable overrides command

I set the EC2_IP_ADDRESS variable
$ export EC2_IP_ADDRESS="`docker run -it -v $PWD/infrastructure:/terraform -v $PWD/data:/data terraform sh -c "terraform init; terraform state show module.aws_ec2.aws_eip.aws_instance_eip" | grep public_ip | awk '{print $3}'`"
And then I'm trying to copy some files into the EC2 instance:
$ scp -i key.pem -r src/* ec2-user#$EC2_IP_ADDRESS:/home/ec2-user/src/
But the output is an error: : nodename nor servname provided, or not known
Output of $ echo "scp -i key.pem -r src/* ec2-user#$EC2_IP_ADDRESS:/home/ec2-user/src/"
:/home/ec2-user/src/c/* ec2-user#X.X.X.X
It seems that anything after the variable EC2_IP_ADDRESS goes to the beginning of the string, overriding the command.
Any ideas on how to fix this?
It seems the variable contains $'\r' at the end. Remove it with
EC2_IP_ADDRESS=${EC2_IP_ADDRESS%$'\r'}

Shell script to grep logs on different host and write the grepped output to a file on Host 1

Shell script needs to
ssh to Host2 from Host1
cd /test/test1/log
grep logs.txt for string error
write the grepped output to a file
and move that file to Host1
This can be accomplished by specifying the -f option to ssh:
ssh user#host -f 'echo "this is a logfile">logfile.txt'
ssh user#host -f 'grep logfile logfile.txt' > locallogfile.txt
cat locallogfile.txt
An example using a different directory and cd changing directories to it:
ssh user#host -f 'mkdir -p foo/bar'
ssh user#host -f 'cd foo/bar ; echo "this is a logfile">logfile.txt'
ssh user#host -f 'cd foo/bar ; echo "this is a logfile">logfile.txt'
ssh user#host -f 'cd foo/bar ; grep logfile logfile.txt' > locallogfile.txt
cat locallogfile.txt

How can I pass the result of a local `curl` in the command line to a remote server in one line?

I'm looking to make something like the following work in a one-line shell.
curl -s icanhazip.com | ssh user#host 'php /path/to/script.php "[PASS_IP_HERE]"'
I need to pass my local IP to a remote server, preferably in one line.
Today I do it like this:
curl -s icanhazip.com // manually copy result
ssh user#host 'php /path/to/script.php "[PASTE_RESULT_HERE]"'
Assuming local IP is 1.2.3.4, and remote IP is 5.6.7.8, then the desired result is closer to:
> curl -s icanhazip.com
1.2.3.4
> ssh user#5.6.7.8 'php /path/to/script.php "1.2.3.4"'
// How can I pass this dynamically? ---^
Probably something like:
curl -s icanhazip.com | xargs -I{} ssh user#host 'php /path/to/script.php {}'
You can use command substitution:
ssh -t user#host "php /path/to/script.php $(curl -s icanhazip.com)"

Resources