import paramiko
import os
import plistlib

# 设置SSH连接参数
# hostname = '172.30.104.18'
port = 22
username = 'root'
password = 'alpine'
# packagename = 'com.funny.prank.call'

def moveRemote(hostname:str, packagename:str,serverurl:str):
    # 创建SSH传输通道
    transport = paramiko.Transport((hostname, port))
    transport.set_keepalive(5)
    # 连接SSH服务端，以用户名和密码进行认证
    transport.connect(username=username, password=password)

    # 创建SFTP客户端
    sftp = paramiko.SFTPClient.from_transport(transport)

    # 上传本地文件到远程主机
    local_path = 'local_file.txt'
    remote_path = '/User/OhNoData/config001.plist'
    # sftp.put(local_path, remote_path)
    sftp.get(remote_path, local_path)

 
    with open(local_path, 'rb') as fp:
        configPlist = plistlib.load(fp)
        configPlist['IpDevName'] = packagename
        configPlist['ServerURL'] = serverurl
        configPlist['PackageName'] = 'com.cineLookStudio.cineLookStudio'
        configPlist['WashParam'] = False
        configPlist['MainServerURL'] = "http://192.168.40.8:8080"
    
    with open(local_path, 'wb') as fp:
        plistlib.dump(configPlist, fp)    

    print(configPlist)
    
    # 上传本地文件到远程主机
    sftp.put(local_path, remote_path)
    
    # 关闭SFTP连接
    sftp.close()

    # 关闭SSH传输通道
    transport.close()
    
if __name__ == "__main__":
    ips1 = []
    with open("./ips.txt", 'r') as f:
       ips1 = [i.strip() for i in f.readlines()]


    for it in ips1:
        try:
            print(it)
            moveRemote(it,"CineLookStudio", "https://nks-api.i.explore-sun.com")
        except Exception as e:
            print(e)