'Run wget command via ssh remotely but connection timeout

I've been struggling with this for 2 days already.

I have 2 linux machines A and B and trying to run the following command from A remotely (as it would be done on B locally):

sshpass -p 'somePassword' ssh userName@machineB "wget http://someUrl.com/someFile.zip"

someFile.zip should be downloaded and kept on B but I get connection timeout. Running this command directy on macnine B works fine. I presume there are some issues with SSH.

This script is needed for TeamCity continous integration.

p.s. sshpass is just an utility to run command via ssh without user interaction by specifying password.

What the issue and how to fix it? Thank you.

UPDATE: Proxy settings should be specified in ~/.bashrc file for non-interactive sessions. The reason is that proxy setting were in /etc/profile which works only for interactive sessions.



Solution 1:[1]

I've not tried sshpass, but my guess is the connection timeout is A connecting to B, not B making the HTTP request. I would suggest using SSH keys instead, which I'm pretty sure will work for you.

To setup ssh keys, run this command on A:

ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N ''

The above will create an SSH RSA keypair that is 4096 bits in length with no passphrase (-N ''). It writes two files in ~/.ssh; copy the public key to B:

scp ~/.ssh/id_rsa.pub B:

On server B do this:

mkdir ~/.ssh chmod 0700 ~/.ssh cat id_rsa.pub >> ~/.ssh/authorized_keys chmod 0600 ~/.ssh/authorized_keys

On RedHat systems that have SELinux enabled, it may be necessary to run the following command in order for the system to accept using the authorized keys file:

restorecon -R -v ~/.ssh

The above allows the server containing the private key of any public key listed in the authorized_keys file to SSH into the machine.

Once you have that setup, you should be able to ssh from A to B without a password.

The following command works on my system even via cronjob:

ssh -i ~/.ssh/id_rsa foobar.local 'curl -O https://www.google.com/logos/doodles/2014/rubiks-cube-5658880499515392-res.png'

Solution 2:[2]

I have had the same problem and tried all the ways to solve it.

My me the timeout has disappeared when I turned on the DHCP.

I use VMWare. So there the solution was the following:

Edit -> Virtual Network Editor -> Check the box Use local DHCP service to distribute IP address to VMs

And voila. Problem solved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Kateridzhe