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

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)"

Related

Run a script on remote server with ssh password or key

I'm trying to run a script on a remote server with either password credentials or .pem key access and I'm getting errors no matter which solution I've found etc.
bash script content:
#!/bin/bash
sudo fdisk -l
ssh -T -i "~/.ssh/keys/key.pem" ubuntu#host "sudo bash <(wget -qO- http://host.com/do.sh)"
Error: bash: /dev/fd/63: No such file or director
ssh user#host.com 'echo "password" | sudo bash <(wget -qO- http://www.host.io/do.sh)'
Error: sudo: a password is required
ssh -t user#host.com "echo password | sudo fdisk -l"
Works but still gives me the password propmt
echo -t pass | ssh user#host "sudo bash <(wget -qO- http://host.com/do.sh)"
echo -tt pass | ssh user#host "sudo bash <(wget -qO- http://host.com/do.sh)"
Error: bash: /dev/fd/63: No such file or directory
// And I also get the password prompt
echo -tT pass | ssh user#host "sudo bash <(wget -qO- http://host.com/do.sh)"
Error: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
// And I also get the password prompt
// This works but I still get the password propmt
ssh user#host 'echo "password" | sudo -S sudo fdisk -l'
These are different variations of the supposed solutions from other places.
What I'm trying to do:
Is to run a script from a URL on the remote server while echoing the password to the cmd so I don't get propmt to input the password manually.
To be able to do the same thing above with using the .pem key variant also
For an explanation for commands except the first one, You can't do stdin-redirect a password to ssh if ssh requires interactively. ssh only allows manual typing if you use a password.
Your first error said that bash can't read a file descriptor. So ssh via ~/.ssh/keys/key.pem works. To run the shell command on the fly,
ssh -T -i "~/.ssh/keys/key.pem" ubuntu#host "curl -fsSL http://host.com/do.sh | sudo bash"
Does your script really need to run with sudo??
If not, then try this:
ssh user#host "curl -s -o do.sh 'http://host.com/do.sh'; source do.sh"

i need execute command after ssh connection, but have command sustitution

but needed execute $() in server, it is running before ssh
ssh -t -o StrictHostKeyChecking=no -i "${SSH_KEY}" "${HOST}" "$(command)"
this is a script that I need to run that code on the server
If you run that, it will execute whats inside of the $() before connecting to the server. Then it will send the result to be executed.
I think what you want is do this :
ssh -t -o StrictHostKeyChecking=no -i "${SSH_KEY}" "${HOST}" 'command'
Where command might be something like :
ls -lh | grep someword
Let me know if I am correct.

nested ssh -t -t not providing $PS1

I am trying to run a nested ssh -t -t but it won't provide me the environment variables when working with cat and echo.
#!/bin/bash
pass="password\n"
bla="cat <(echo -e '$pass') - | sudo -S su -"
ssh -t -t -t -t jumpserver "ssh -t -t -t -t server \"$bla\" "
I get an output without any variables taken into consideration. (e.g. PS1 does not get shown but commands work fine) The problem is related to cat <(echo -e '$pass') - but this was the way to keep echo alive after providing the password for sudo.
How can i achieve this and get environment variables to get a proper output?
Thanks.
The -tt is enough. Using more -t does not add any more effect and just makes an impression that you have no idea what are you doing.
What is the point of cat <(echo -e) construction? Writing just echo would result in the same, isn't it?
Why to use sudo su? sudo already does all you need, isn't it?
So how can it look in some fashionable manner?
pass="password\n"
bla="echo '$pass' | sudo -Si"
ssh -tt jumpserver "ssh -tt server \"$bla\""
And does it work? Try to debug the commands with -vvv switches to the ssh. It will show you what is actually executed and passed to each other shell.

How to copy echo 'x' to file during an ssh connection

I have a script which starts an ssh-connection.
so the variable $ssh start the ssh connection.
so $SSH hostname gives the hostname of the host where I ssh to.
Now I try to echo something and copy the output of the echo to a file.
SSH="ssh -tt -i key.pem user#ec2-instance"
When I perform a manual ssh to the host and perform:
sudo sh -c "echo 'DEVS=/dev/xvdbb' >> /etc/sysconfig/docker-storage-setup"
it works.
But when I perform
${SSH} sudo sh -c "echo 'DEVS=/dev/xvdb' > /etc/sysconfig/docker-storage-setup"
it does not seem to work.
EDIT:
Also using tee is working fine after performing an ssh manually but does not seem to work after the ssh in the script.sh
The echo command after an ssh of the script is happening on my real host (from where I'm running the script, not the host where I'm performing an ssh to). So the file on my real host is being changed and not the file on my host where I've performed an ssh to.
The command passed to ssh will be executed by the remote shell, so you need to add one level of quoting:
${SSH} "sudo sh -c \"echo 'DEVS=/dev/xvdb' > /etc/sysconfig/docker-storage-setup\""
The only thing you really need on the server is the writing though, so if you don't have password prompts and such you can get rid of some of this nesting:
echo 'DEVS=/dev/xvdb' | $SSH 'sudo tee /etc/sysconfig/docker-storage-setup'

Can I open mysql tunnel from with a iPython notebook cell (osx)

Tried using "Magics" and can successfully run:
%%bash
echo "hello from $BASH"
which returns:
hello from /bin/bash
But running this:
%%bash
ssh -C -N <foo user>#<bar server> -L 3306:127.0.0.1:3306
does not permit running vanilla mysql query code against the tunnel connection. Am I trying to do the impossible?
Yes. So I have two options:
I can use the magic method of -
%%bash
ssh -C -N # -L 3306:127.0.0.1:3306
I can simply start the command with '!'
!ssh -C -N # -L 3306:127.0.0.1:3306
BUT - be sure the end the tunnel command with a ';' so that output is suppressed and IPython Notebook isn't waiting for the tunnel command to return something before executing your SQL. Example:
!ssh -C -N <foo user>#<bar server> -L 3306:127.0.0.1:3306;

Resources