Linuxサーバーからローカルコンピューターに定期的にデータをコピーし、パスワードやその他の操作を入力する必要をなくします

クラウドサーバーからローカルに情報を定期的に更新する必要がある場合があります。この効果を実現するには、pythonスクリプトを記述してローカルで実行できます。

#!/usr/bin/python  
# -*- coding: utf-8 -*- 

import os
import subprocess
import time
import pexpect
import psutil


# 杀进程函数
while True:
    child = pexpect.spawn('scp [email protected]:/home/test.txt /home/')  # 远程拷贝
    child.expect('password:')
    child.sendline('xxxx')  # 自动输入密码
    print("start sleep....")
    time.sleep(20)

スリープ時間は更新されたサイクルです。

これは、サーバー上のファイルのステータス変更を検出するために使用できます。たとえば、これは使用シナリオです。サーバー上のファイルが変更されると、他の操作がローカルで実行されます。

#!/usr/bin/python  
# -*- coding: utf-8 -*- 

import os
import subprocess
import time
import pexpect
import psutil

# 杀进程函数
def kill_process(name):
    for proc in psutil.process_iter():
        print("pid-%d,name:%s" % (proc.pid, proc.name()))
        if (proc.name() == name):
            os.system("kill -9 {}".format(proc.pid))
            print("杀死{}进程".format(name))


KMS_listen_port = 10000  # 初始化KMS监听端口
while True:
    child = pexpect.spawn('scp [email protected]:/home/test.txt /home/')  # 远程拷贝
    child.expect('password:')
    child.sendline('xxxxxx')  # 自动输入密码
    print("start sleep....")
    time.sleep(20)
    with open('/home/test.txt') as f:  # 读取文件,判断监听端口是否发生了变化,如果变换,杀死原来的fmpeg,重新开启ffmpeg
        new_port = int(f.read())
        print("new_port: ", new_port)
        if new_port != KMS_listen_port:
            KMS_listen_port = new_port
            kill_process("docker")  # 杀死正在运行的docker
            os.system("sudo sesrvice c start")

おすすめ

転載: blog.csdn.net/hongge_smile/article/details/108256624