普通版本
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("沒有該選項,請重新輸入:>>")