Subha Bera
Subha Bera

Reputation: 11

By using Paramiko module in Python how can I edit a file on Linux server?

Suppose I made a file using Paramiko module in CentOS and accessing it from a Python script in Windows:

stdin,stdout,stderr=ssh.exec_command("touch Hello")

Then how can I write something and save the file?

print "hello,world"

Please help me I am stuck here.

Upvotes: 1

Views: 4577

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

Use the SFTP to upload the file contents. Do not try to hack this with shell commands.

transport = paramiko.Transport(("host", 22))
transport.connect(username = "username", password = "password")
sftp = paramiko.SFTPClient.from_transport(transport)

f = sftp.open("/path/to/remote/file", "wb")
f.write("hello,world")
f.close()

Upvotes: 1

Related Questions