用python一键自动控制所有VPS 是有多爽!

 





Windows安装python

https://www.python.org/



安装依赖

cmd命令行输入下面的命令,到达目录安装依赖

cd \Users\kejilion\AppData\Local\Programs\Python\Python311

python -m pip install --upgrade pip

python -m pip install paramiko speedtest-cli 



下载工具

VS Code文本编辑工具下载

https://app.kejilion.pro/visual-studio-code



创建脚本

创建txt把后缀改成py,复制下面的代码

import paramiko


# 定义服务器列表,包括服务器名称、IP地址、端口号、用户名和密码

servers = [

    {"name": "美国", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456"},   

    {"name": "不丹", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456"},   

    {"name": "毛里求斯", "hostname": "1.1.1.1", "port": 22, "username": "root", "password": "123456"},   

    # 添加更多服务器

]


# 定义更新操作

def update_server(name, hostname, port, username, password):

    try:


        # 连接服务器

        client = paramiko.SSHClient()

        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        client.connect(hostname, port=port, username=username, password=password)


        # 执行步骤1: 更新操作

        print(f" {name} 更新")

        stdin, stdout, stderr = client.exec_command("apt update -y && apt install -y curl wget sudo socat htop")

        

        print(f"正在更新:")

        while not stdout.channel.exit_status_ready():

            if stdout.channel.recv_ready():

                print(stdout.channel.recv(1024).decode(), end="")


        # 检查执行状态

        if stderr.channel.recv_exit_status() == 0:

            print(f"更新成功")

        else:

            print(f"更新失败")

        

        print()



        print(f"{name} 安装 Docker")

        stdin, stdout, stderr = client.exec_command("wget -qO- https://get.docker.com/ | sh")


        print(f"正在安装 Docker:")

        while not stdout.channel.exit_status_ready():

            if stdout.channel.recv_ready():

                print(stdout.channel.recv(1024).decode(), end="")


        # 检查执行状态

        if stderr.channel.recv_exit_status() == 0:

            print(f"安装 Docker 成功")

        else:

            print(f"安装 Docker 失败")


        print()



        print(f"{name} 安装 Docker Compose")

        stdin, stdout, stderr = client.exec_command("wget https://github.com/docker/compose/releases/download/v2.17.3/docker-compose-$(uname -s)-$(uname -m) -O /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose")


        print(f"正在安装 Docker Compose:")

        while not stdout.channel.exit_status_ready():

            if stdout.channel.recv_ready():

                print(stdout.channel.recv(1024).decode(), end="")


        # 检查执行状态

        if stderr.channel.recv_exit_status() == 0:

            print(f"安装 Docker Compose 成功")

        else:

            print(f"安装 Docker Compose 失败")



        # 执行步骤1: 更新操作

        print(f"Docker Compose 版本")

        stdin, stdout, stderr = client.exec_command("docker-compose --version")

        

        while not stdout.channel.exit_status_ready():

            if stdout.channel.recv_ready():

                print(stdout.channel.recv(1024).decode(), end="")


        print()

        print()


        # 关闭 SSH 连接

        client.close()


    except Exception as e:

        print(f"连接 {name} 失败")



# 遍历服务器列表,逐一更新

for server in servers:

    name = server["name"]

    hostname = server["hostname"]

    port = server["port"]

    username = server["username"]

    password = server["password"]

    update_server(name, hostname, port, username, password)


# 等待用户按下任意键后关闭窗口

input("按任意键关闭窗口...")


运行

编辑好保存并退出,双击运行即可一键安装docker到所有小鸡了!




Linux安装python

安装py及依赖

apt install -y python3 python3-paramiko speedtest-cli


查看版本

python3 --version


创建py脚本

nano hello.py


运行py脚本

python3 hello.py





原作者GitHub网址

里面还有更多好用的一键脚本

https://github.com/kejilion/python-for-vps


















评论

发表评论

此博客中的热门博文

科技lion官方一键脚本上线!VPS监控测试建站维护于一身的全能工具箱

2023国外的接码平台分享

CloudCone付费VPS分享 挺便宜的