2013-10-07 29 views
2

如果只能從server1訪問server2,我該如何ssh兩次?在主機內構建兩次ssh

fabfile.py:

from fabric.api import run 
from fabric.api import env 

env.hosts = ['[email protected]'] 

env.use_ssh_config = True 

def dothis(): 
    run('ssh [email protected]') 
    run('ls -al') # this should be done on [email protected] 

當我運行:

晶圓廠dothis,我得到:

[[email protected]] Executing task 'dothis' 
[[email protected]] run: ssh [email protected] 
[[email protected]] out: Permission denied (publickey). 
[[email protected]] out: 
[[email protected]] out: 

Fatal error: run() received nonzero return code 255 while executing! 

Requested: ssh [email protected] 
Executed: /bin/bash -l -c "ssh [email protected]" 

Aborting. 
Disconnecting from [email protected] done. 

我如何告訴面料env.user_ssh_config =真Server2上沒有在server1上保存另一個結構文件?

的方式,我通常訪問Server2上是這樣的:

ssh [email protected] ssh [email protected] 
+0

現在不好意思,想做一個答覆嗎?那需要2個任務嗎? – Neeran

+0

添加爲答案。只有一項任務需要完成你的問題。 – Johnsyweb

+0

或者,您可以嘗試使用'server1.com'中的[sshpass](http://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/)運行該命令以跳過主機密鑰檢查。你的命令應該是這樣的:'sshpass -p password ssh -o StrictHostKeyChecking = no -o CheckHostIP = no [email protected]「ls -al」' – Milo

回答

1

它看起來像你想使用「server1.com」爲gateway主機爲「server2.com」,想必其他人:

from fabric.api import run 
from fabric.api import env 

env.gateway = '[email protected]' 
env.hosts = ['[email protected]'] 

env.use_ssh_config = True 

def dothis(): 
    run('ls -al') # this should be done on [email protected] 
+0

我得到paramiko.ChannelException:管理上禁止。不知道那裏出了什麼問題。需要在server1上做些什麼來使它成爲這樣的網關? – Neeran

+0

@Jimmy:你有這個工作嗎?如果沒有,我懷疑你的'.ssh/config'中有一些爲'server2.com'配置的端口轉發。嘗試使用最低限度的.ssh/config並僅添加必要的行以獲得此示例的工作。這個例子你不需要在主機上做任何特殊的配置。祝你好運! – Johnsyweb