import paramiko
import configparser
remote_file = '/root/test.conf'
local_file = './test.conf'
t = paramiko.Transport(('192.168.0.104', 22))
t.banner_timeout = 10
t.connect(username='root', password='1a2b3c4d5e')
sftp = paramiko.SFTPClient.from_transport(t)
# 下载文件
sftp.get(remote_file, local_file)
# 读取文件
config = configparser.ConfigParser()
config.read(local_file)
print(config.sections())
# 修改文件
config.add_section("user")
config.set("user", "user_name", "xiaoming")
config.write(open(local_file, "w"))
# 上传文件
sftp.put(local_file, remote_file)
t.close()
【python】修改远程服务器上的配置文件
最新推荐文章于 2023-08-15 13:33:41 发布