DLL Download
sftp example
import os
import sshnet as ssh
import config as cfg
# Callback Example
def GetDownloadHandle(sftp,remote):
f = sftp.Get(remote)
filesize = f.Attributes.Size
def Progress(size):
print('Get :', 100*size/filesize, size, filesize)
return Progress
def GetUploadHandle(local):
filesize = os.path.getsize(local)
def Progress(size):
print('Put :', 100*size/filesize, size, filesize)
return Progress
def GetFile(srv,remote,local):
conn = ssh.Sftp(cfg.host[srv],cfg.user[srv],cfg.pw[srv])
conn.Download(remote,local,GetDownloadHandle(conn.sftp,remote))
conn.Close()
def PutFile(srv,local,remote):
conn = ssh.Sftp(cfg.host[srv],cfg.user[srv],cfg.pw[srv])
conn.Upload(local,remote,GetUploadHandle(local))
conn.Close()
# Simple Example
def SimpleProgress(size):
print(size)
def Get(srv,remote,local):
ssh.SftpGet( cfg.host[srv],cfg.user[srv],cfg.pw[srv],remote,local,SimpleProgress)
def Put(srv,remote,local):
ssh.SftpPut( cfg.host[srv],cfg.user[srv],cfg.pw[srv],remote,local,SimpleProgress)
sshnet.py
import clr
clr.AddReferenceToFileAndPath("Renci.SshNet")
import System
from System.IO.File import OpenRead
from System.IO.File import OpenWrite
from System.IO import FileStream
from System.IO import FileMode
import Renci.SshNet
from Renci.SshNet import ConnectionInfo
from Renci.SshNet import PasswordAuthenticationMethod
from Renci.SshNet import PrivateKeyAuthenticationMethod
from Renci.SshNet import SshClient
from Renci.SshNet import SftpClient
'''
new AuthenticationMethod[] {
new PasswordAuthenticationMethod("username","password"),
new PrivateKeyAuthenticationMethod("username",new PrivateKeyFile[]{
new PrivateKeyFile(@"..\openssh.key","passphrase")}}
'''
class Ssh():
def __init__(self,host,user,pw):
self.conn = ConnectionInfo( host, 22, user, PasswordAuthenticationMethod( user, pw ))
self.ssh = SshClient(self.conn)
self.ssh.Connect()
def Close(self):
self.ssh.Disconnect()
def Command(self,command):
self.cmd = self.ssh.CreateCommand(command)
self.cmd.Execute()
return self.cmd.ExitStatus, self.cmd.Result
class Sftp():
def __init__(self,host,user,pw):
self.conn = ConnectionInfo( host, 22, user, PasswordAuthenticationMethod( user, pw ))
self.sftp = SftpClient(self.conn)
self.sftp.Connect()
def Close(self):
self.sftp.Disconnect()
def Exists(self,filename):
return self.sftp.Exists(filename)
def Delete(self,filename):
return self.sftp.Delete(filename)
def Chdir(self,folder):
return self.sftp.ChangeDirectory(folder)
def ListDir(self,folder):
return self.sftp.ListDirectory(folder)
def Upload(self,local,remote=None,handler=None):
if not remote: remote = local
if not handler:
with OpenRead(local) as f:
self.sftp.UploadFile(f, remote, True)
else:
with OpenRead(local) as f:
self.sftp.UploadFile(f, remote, handler)
def Upload2(self,local,remote=None):
if not remote: remote = local
return self.sftp.UploadFile(FileStream(local,FileMode.Open), remote, True)
def Download(self,remote,local=None,handler=None):
if not local: local = remote
if not handler:
with OpenWrite(local) as f:
self.sftp.DownloadFile(remote, f)
else:
with OpenWrite(local) as f:
self.sftp.DownloadFile(remote, f, handler)
def SftpGet(host,user,passwd,remote,local,handler=None):
sftp = Sftp(host,user,passwd)
sftp.Download(remote,local,handler)
sftp.Close()
def SftpPut(host,user,passwd,local,remote,handler=None):
sftp = Sftp(host,user,passwd)
sftp.Upload(local,remote,handler)
sftp.Close()
def SshDemo():
ssh = Ssh('localhost', 'username', 'password')
rv,text = ssh.Command("ls -l")
print(rv,text)
rv,text = ssh.Command("ls")
print(rv,text)
ssh.Close()
def SftpDemo():
sftp = Sftp('localhost', 'username', 'password')
sftp.Chdir("work")
sftp.Upload("a.py","c.py")
sftp.Download("a.py","b.py")
files = sftp.ListDir("./")
for f in files:
print(f.Name)
sftp.Close()
if __name__ == "__main__":
SshDemo()
SftpDemo()
댓글 없음:
댓글 쓰기