Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

python - fabric: run() detect if ssh connection is broken during command execution

I'm using fabric.api to run command in the remote machine. The snippet of my code looks like this:

import time
import sys

from fabric.api import *
from fabric.api import env, run, task, hide, execute

ssh_key = <something>
ssh_key_passphase = <something>
env.host_string = <something>
env.user = <something>
env.port = 22

def execute_command_in_remote(cmd):
    env.user = ssh_user
    env.key_filename = ssh_key
    env.password = ssh_key_passphase
    result = run(cmd)
    return result

result=execute_command_in_remote(<some commands>)
....
....

While the command is being executed in the remote, if ssh connection is broken(due to the host restart), the program gets stuck. How can I get a signal about the broken ssh connection?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...