简体   繁体   English

通过 ssh 远程运行 wget 命令但连接超时

[英]Run wget command via ssh remotely but connection timeout

I've been struggling with this for 2 days already.我已经为此苦苦挣扎了2天。

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):我有 2 台 linux 机器 A 和 B,并尝试从 A 远程运行以下命令(就像在本地 B 上完成的那样):

sshpass -p 'somePassword' ssh userName@machineB "wget http://someUrl.com/someFile.zip " 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. someFile.zip 应该下载并保存在 B 上,但我连接超时。 Running this command directy on macnine B works fine.在 macnine B 上直接运行此命令可以正常工作。 I presume there are some issues with SSH.我认为 SSH 存在一些问题。

This script is needed for TeamCity continous integration. TeamCity 持续集成需要此脚本。

ps sshpass is just an utility to run command via ssh without user interaction by specifying password. ps sshpass 只是一个通过 ssh 运行命令的实用程序,无需用户通过指定密码进行交互。

What the issue and how to fix it?问题是什么以及如何解决? Thank you.谢谢你。

UPDATE: Proxy settings should be specified in ~/.bashrc file for non-interactive sessions.更新:应在 ~/.bashrc 文件中为非交互式会话指定代理设置。 The reason is that proxy setting were in /etc/profile which works only for interactive sessions.原因是代理设置位于 /etc/profile 中,它仅适用于交互式会话。

I've not tried sshpass, but my guess is the connection timeout is A connecting to B, not B making the HTTP request. 我没有尝试过sshpass,但我的猜测是连接超时是A连接到B,而不是B发出HTTP请求。 I would suggest using SSH keys instead, which I'm pretty sure will work for you. 我建议使用SSH密钥,我非常肯定会为你工作。

To setup ssh keys, run this command on A: 要设置ssh密钥,请在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 '' ). 以上将创建一个长度为4096位且没有密码短语( -N '' )的SSH RSA密钥对。 It writes two files in ~/.ssh ; 它在~/.ssh写入两个文件; copy the public key to B: 将公钥复制到B:

scp ~/.ssh/id_rsa.pub B:

On server B do this: 在服务器B上执行此操作:

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: 在启用了SELinux的RedHat系统上,可能需要运行以下命令,以便系统使用授权密钥文件接受:

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. 以上允许包含authorized_keys文件中列出的任何公钥的私钥的服务器SSH进入机器。

Once you have that setup, you should be able to ssh from A to B without a password. 完成设置后,您应该能够在没有密码的情况下从A到B进行ssh。

The following command works on my system even via cronjob: 以下命令甚至可以通过cronjob在我的系统上运行:

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

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.当我打开 DHCP 时,超时消失了。

I use VMWare.我使用 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 Edit -> Virtual Network Editor -> 勾选Use local DHCP service to distribute IP address to VMs

And voila.瞧。 Problem solved.问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM