簡體   English   中英

如何從Windows 7使用python在遠程(linux)機器上創建文件

[英]How to create an file on remote (linux) machine using python from window 7

我想創建一個文件名(loginconfig.xml),其中包含遠程(Linux)計算機特定目錄中的某些內容,例如,在桌面目錄cd〜/ Desktop /下。

我正在使用導入telnetlib模塊來訪問cd〜/ Desktop /,甚至我也能夠創建文件,但是很遺憾在(loginconfig.xml文件)中插入以下內容

下面是我的代碼:

import telnetlib

tn = telnetlib.Telnet(HOST)

tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")


file_create1=tn.write(b"touch /opt/loginConfig.xml"+ b"\n")
#go to opt directory  
next_line_come = tn.read_until(b"#")
tn.write(b"chmod 777 /opt/loginConfig.xml"+ b"\n")

next_line_come = tn.read_until(b"#")

location=tn.write(b"/opt/loginConfig.xml")

write_under_file=open(b"location","w+")

write_under_file.write('<config\n')
write_under_file.write('user="name"\n')
write_under_file.write('password="admin!"\n')
write_under_file.write('autoLogin="true"\n'

正在創建的文件無法在文件下寫入內容,用內容寫文件的好方法是什么?

當您open(b"location","w+")您只是在打開一個名為“ location”的本地文件,而不是您在遠程系統上創建的文件。 如果要使用telnet寫入遠程文件,則必須發出與在終端提示符下使用的相同類型的命令: echo "Some line" >> /remote/path/to/somefile

現在,通常更容易在本地計算機上准備文件,然后使用文件傳輸協議(FTP或其他方式)移動到其他主機。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM