普通版本 1 # -*- coding:utf-8 -*-
import paramiko,os,sys,time port = 22
user = 'root'
def ssh_scp_put(ip,port,user,password,local_file,remote_file):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, 22, 'root', password)
a = ssh.exec_command('date')
stdin, stdout, stderr = a
print stdout.read()
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
sftp = ssh.open_sftp()
sftp.put(local_file, remote_file) def ssh_scp_get(ip, port, user, password, remote_file, local_file):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, 22, 'root', password)
a = ssh.exec_command('date')
stdin, stdout, stderr = a
print stdout.read()
sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
sftp = ssh.open_sftp()
sftp.get(remote_file, local_file) ip = raw_input("请输入远端主机的IP地址:")
password = raw_input("请输入远端主机的密码:") while True:
print '''
-------欢迎使用 scp software--------
上传文件请输入 [ 1 ]:
下载文件请输入 [ 2 ]:
退出SCP请输入 [ q ]:
------------------------------------
'''
choice = raw_input("请输入 [ ]")
if choice == "":
local_file = raw_input("请输入本地文件的绝对路径:")
remote_file = raw_input("请输入文件上传的绝对路径:")
ssh_scp_put(ip,port,user,password,local_file,remote_file)
elif choice == "":
remote_file = raw_input("请输入远端文件的绝对路径:")
local_file = raw_input("请输入要放到本地的绝对路径:")
ssh_scp_get(ip,port,user,password,remote_file,local_file)
elif choice == "q":
print "感谢使用,再见"
exit()
else:
print "输入错误,请重新输入:"
装逼版本
import paramiko
import os
import sys
import getpass
print("\033[32;1m****开始配置目标机器信息*****\033[0m")
#ips = input("主机IP:")
#user = input("主机账号:")
#password = getpass.getpass("主机密码:")
#port = 22
user = "root"
ips = "10.10.123.96"
password = "B^Dc%4LSBvhZZK3B"
port = 22
class Tools(object):
def __init__(self, user, password, port, ips):
self.user = user
self.password = password
self.port = port
self.ip = ips
def connect(self):
try:
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.ip, self.port, self.user, self.password)
print("连接已建立")
except Exception as e:
print("未能连接到主机")
def cmd(self):
cmd = input("请输入要执行的命令:>>")
stdout, stdin, stderr = self.ssh.exec_command(cmd)
#print(sys.stdout.read())
def input(self):
self.local_file_abs = input("本地文件的绝对路径:>>")
self.remote_file_abs = input("远程文件的绝对路径:>>")
def put(self):
sftp = paramiko.SFTPClient.from_transport(self.ssh.get_transport())
sftp = self.ssh.open_sftp()
self.input()
sftp.put(self.local_file_abs,self.remote_file_abs)
def get(self):
sftp = paramiko.SFTPClient.from_transport(self.ssh.get_transport())
sftp = self.ssh.open_sftp()
self.input()
sftp.get(self.remote_file_abs,self.local_file_abs)
def close(self):
self.ssh.close()
print("连接关闭")
obj = Tools(user, password, port, ips)
if __name__ == "__main__":
msg = '''\033[32;1m
执行命令 >>输入cmd
上传文件 >>输入put
下载文件 >>输入get
退出 >>输入q\033[0m
'''
getattr(obj, "connect")()
while True:
print(msg)
inp = input("action:>>")
if hasattr(obj,inp):
getattr(obj,inp)()
if inp == "q":
getattr(obj,"close")()
exit()
else:print("没有该选项,请重新输入:>>")