简体   繁体   中英

can't re-establish connection to remote server after server restart | #pythonScript

I am trying to Automate a unix based system. I have a python fabfile.py with the automation scripts, which first connects to a remote server and run the script. In between the script I have a command which calls for system reboot. At this point of time, I am loosing connection with the remote server and not able to re-establish connection.

--fabfile.py

//Import 

env.hosts = ['uname@host_ip']

def test() :

//here we run the commands

//calls reboot

//remaining commands

Can I re-establish the connection after calling 'reboot' command so that I can execute remaining commands?

Theres something else at work then here, not fabric.

Fabric run everything procedurally, meaning; if you have a list of things to run fabric will run them in order and when there is no more work to be done, THEN it disconnects not before. If you're having connection drops check either YOUR connection, the servers, or the servers ssh configuration. As an example:

from fabric.api import run, task
from fabric.state import env

@task
def dev():
    """
    I REALLY like the environments done in a task and not global, its easier to override
    """
    env.hosts = ['10.99.0.2']
    env.user = 'vagrant'
    env.password = 'vagrant'
    env.key_filename = '~/.vagrant.d/insecure_private_key'

@task
def whoami():
    run('whoami')

@task
def echo(msg):
    run('echo "{}"'.format(msg))

Simple test:

$ fab dev whoami echo:'hello world'
[10.99.0.2] Executing task 'whoami'
[10.99.0.2] run: whoami
[10.99.0.2] out: vagrant
[10.99.0.2] out: 

[10.99.0.2] Executing task 'echo'
[10.99.0.2] run: echo hello world
[10.99.0.2] out: hello world
[10.99.0.2] out: 


Done.
Disconnecting from 10.99.0.2... done.

or without you can still run it like this since you never hardcoded it:

$ fab whoami echo:'hello world' -H 'vagrant@10.99.0.2','ubuntu@*****' --password vagrant -i ~/.ssh/*****.pem

notice that the dev task was not ran here. Output:

[vagrant@10.99.0.2] Executing task 'whoami'
[vagrant@10.99.0.2] run: whoami
[vagrant@10.99.0.2] out: vagrant
[vagrant@10.99.0.2] out: 

[ubuntu@*****] Executing task 'whoami'
[ubuntu@*****] run: whoami
[ubuntu@*****] out: ubuntu
[ubuntu@*****] out: 

[vagrant@10.99.0.2] Executing task 'echo'
[vagrant@10.99.0.2] run: echo "hello world"
[vagrant@10.99.0.2] out: hello world
[vagrant@10.99.0.2] out: 

[ubuntu@*****] Executing task 'echo'
[ubuntu@*****] run: echo "hello world"
[ubuntu@*****] out: hello world
[ubuntu@*****] out: 


Done.
Disconnecting from ubuntu@*****... done.
Disconnecting from vagrant@10.99.0.2... done.

您可以使用time.sleep(t) (其中t是时间,以秒为单位)暂停脚本,直到重新引导的计算机备份为止。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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