gpt4 book ai didi

python fabric,遍历IP列表以更新服务器

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:10 26 4
gpt4 key购买 nike

希望能帮到你。对织物完全陌生,了解一点 python。我正在尝试遍历 IP 的外部文件以更新 40 台奇怪的远程服务器。

这不起作用,在第一个 IP 后停止。

终端命令: fab -p 密码主机更新

from fabric.api import env, run, sudo


def hosts():
env.hosts = open('sat_ip_list', 'r').readlines()


def update():
sudo('apt-get update -y')

最佳答案

我尝试了以下 IP 列表 + Fabric 脚本并且运行没有问题 fab -p <password> hosts uname :

# ip_list.txt
192.168.xxx.x
127.0.0.1:xxxx
174.xxx.xxx.xxx:xxxx

# fabfile.py
from fabric.api import env, run, sudo

def hosts():
# Read ip list from ip_list.txt
env.hosts = open('ip_list.txt', 'r').readlines()

def uname():
sudo('uname -a')

你的sat_ip_list是什么文件看起来像 - 每行一个 IP 地址吗?

您是否仅使用极少数主机(例如 2-3 个 IP 地址)尝试过您的脚本?绝对没有理由你不能做你想完成的事情,你的脚本基本上对我有用。

作为完整性检查,您可能需要打印出 env.hosts 的值,像这样:

def hosts():
env.hosts = open('sat_ip_list', 'r').readlines()
print('Hosts:', env.hosts)

在我的例子中,结果如下:

me@machine:~$ fab hosts
('Hosts:', ['192.168.xxx.x\n', '127.0.0.1:xxxx\n', '174.xxx.xxx.xxx:xxxx\n'])

关于python fabric,遍历IP列表以更新服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com