python脚本实现scp上传下载功能

普通版本

1
# -*- coding:utf-8 -*- 2 import paramiko,os,sys,time 3 4 port = 22 5 user = 'root' 6 def ssh_scp_put(ip,port,user,password,local_file,remote_file): 7 ssh = paramiko.SSHClient() 8 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 9 ssh.connect(ip, 22, 'root', password) 10 a = ssh.exec_command('date') 11 stdin, stdout, stderr = a 12 print stdout.read() 13 sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) 14 sftp = ssh.open_sftp() 15 sftp.put(local_file, remote_file) 16 17 def ssh_scp_get(ip, port, user, password, remote_file, local_file): 18 ssh = paramiko.SSHClient() 19 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 20 ssh.connect(ip, 22, 'root', password) 21 a = ssh.exec_command('date') 22 stdin, stdout, stderr = a 23 print stdout.read() 24 sftp = paramiko.SFTPClient.from_transport(ssh.get_transport()) 25 sftp = ssh.open_sftp() 26 sftp.get(remote_file, local_file) 27 28 29 30 ip = raw_input("请输入远端主机的IP地址:") 31 password = raw_input("请输入远端主机的密码:") 32 33 while True: 34 print ''' 35 -------欢迎使用 scp software-------- 36 上传文件请输入 [ 1 ]: 37 下载文件请输入 [ 2 ]: 38 退出SCP请输入 [ q ]: 39 ------------------------------------ 40 ''' 41 choice = raw_input("请输入 [ ]") 42 if choice == "1": 43 local_file = raw_input("请输入本地文件的绝对路径:") 44 remote_file = raw_input("请输入文件上传的绝对路径:") 45 ssh_scp_put(ip,port,user,password,local_file,remote_file) 46 elif choice == "2": 47 remote_file = raw_input("请输入远端文件的绝对路径:") 48 local_file = raw_input("请输入要放到本地的绝对路径:") 49 ssh_scp_get(ip,port,user,password,remote_file,local_file) 50 elif choice == "q": 51 print "感谢使用,再见" 52 exit() 53 else: 54 print "输入错误,请重新输入:"

 装逼版本

 1 import paramiko
 2 import os
 3 import sys
 4 import getpass
 5 print("\033[32;1m****开始配置目标机器信息*****\033[0m")
 6 #ips = input("主机IP:")
 7 #user = input("主机账号:")
 8 #password = getpass.getpass("主机密码:")
 9 #port = 22
10 user = "root"
11 ips = "10.10.123.96"
12 password = "B^Dc%4LSBvhZZK3B"
13 port = 22
14 class Tools(object):
15     def __init__(self, user, password, port, ips):
16         self.user = user
17         self.password = password
18         self.port = port
19         self.ip = ips
20     def connect(self):
21         try:
22             self.ssh = paramiko.SSHClient()
23             self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
24             self.ssh.connect(self.ip, self.port, self.user, self.password)
25             print("连接已建立")
26         except Exception as e:
27             print("未能连接到主机")
28     def cmd(self):
29         cmd = input("请输入要执行的命令:>>")
30         stdout, stdin, stderr = self.ssh.exec_command(cmd)
31         #print(sys.stdout.read())
32     def input(self):
33         self.local_file_abs = input("本地文件的绝对路径:>>")
34         self.remote_file_abs = input("远程文件的绝对路径:>>")
35     def put(self):
36         sftp = paramiko.SFTPClient.from_transport(self.ssh.get_transport())
37         sftp = self.ssh.open_sftp()
38         self.input()
39         sftp.put(self.local_file_abs,self.remote_file_abs)
40     def get(self):
41         sftp = paramiko.SFTPClient.from_transport(self.ssh.get_transport())
42         sftp = self.ssh.open_sftp()
43         self.input()
44         sftp.get(self.remote_file_abs,self.local_file_abs)
45     def close(self):
46         self.ssh.close()
47         print("连接关闭")
48 obj = Tools(user, password, port, ips)
49 if __name__ == "__main__":
50     msg = '''\033[32;1m
51     执行命令 >>输入cmd
52     上传文件 >>输入put
53     下载文件 >>输入get
54     退出     >>输入q\033[0m
55     '''
56     getattr(obj, "connect")()
57     while True:
58         print(msg)
59         inp = input("action:>>")
60         if hasattr(obj,inp):
61             getattr(obj,inp)()
62         if inp == "q":
63             getattr(obj,"close")()
64             exit()
else:print("没有该选项,请重新输入:>>")

 

转载于:https://www.cnblogs.com/liruixin/p/5956868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值