paramiko的SF@TP对象实现Linux的scp功能



 

   1 #!/usr/bin/env python

2 # -*- coding: UTF-8 -*-

3

4 import paramiko

5 import os

6 import re

7

8

9 class SSHConnection(object):

10     def __init__(self, host, port, username, password):

11         self.host = host

12         self.port = port

13         self.username = username

14         self.password = password

15         self.__k = None

16

17     def run(self):

18         self.connect()

19         self.sftp_put_dir("/opt/app/appops/sbin/collectd/var", "/opt/app/")

20         # self.cmd('ls /opt/app')

21         self.close()

22

23     def connect(self):

24         transport = paramiko.Transport((self.host, self.port))

25         transport.connect(username=self.username, password=self.password)

26         self.__transport = transport

27

28     def close(self):

29         self.__transport.close()

30

31     def upload(self, loacal_path, target_path):

32         sftp = paramiko.SFTPClient.from_transport(self.__transport)

33         sftp.put(loacal_path, target_path)

34

35     def cmd(self, command):

36         print "commadn in cmd:", command

37         ssh = paramiko.SSHClient()

38

39         ssh._transport = self.__transport

40         stdin, stdout, stderr = ssh.exec_command(command)

41

42         result = stdout.read()

43         print "result:\n", result

44         return result

45

46     def getList(self, path):

47         files_list = []

48         files = sorted(filter(lambda x: re.match(r'^\w', x), os.listdir(path)))

49         for tmp_file in files:

50             path_file = os.path.join(path, tmp_file)

51             if os.path.isdir(path_file) and not os.path.islink(path_file):

52                 files_list.append(path_file)

53                 files_list += self.getList(path_file)

54             else:

55                 files_list.append(path_file)

56         return files_list

57

58     def sftp_put_dir(self, local_dir, remote_dir):

59         sftp = paramiko.SFTPClient.from_transport(self.__transport)

60         if remote_dir[-1] == '/':

61             remote_dir = remote_dir[0:-1]

62         all_files = self.getList(local_dir)

63         for x in all_files:

64             f_path = x.replace(local_dir, "").lstrip('/')

65             if os.path.isdir(x):

66                 r_path = os.path.join(remote_dir, f_path)

67                 try:

68                     sftp.mkdir(r_path)

69                 except Exception,e:

70                     pass

71             else:

72                 remote_filename = os.path.join(remote_dir, f_path)

73                 print u'Put文件%s传输中...' % f_path

74                 sftp.put(x, remote_filename)

75

76

77 def main(host, port, username, password):

78     print "in main:"

79     obj = SSHConnection(host, port, username, password)

80     obj.run()

81

82

83 if __name__ == '__main__':

84     host = ''

85     port = ''

86     username = ''

87     password = ''

88     main(host, port, username, password)

 

 

猜你喜欢

转载自right-left.iteye.com/blog/2405823