(一)基于用户名密码的远程登录加命令执行
- import paramiko
- #创建ssh链接实例
- ssh = paramiko.SSHClient()
- #创建白名单,默认不询问, 即允许连接不在known_hosts文件中的主机
- known_hosts = paramiko.AutoAddPolicy()
- ssh.set_missing_host_key_policy(known_hosts)
- #链接服务器
- ssh.connect(
- hostname = "10.10.65.65", # inux主机
- port = 22, #端口,默认22
- username = "chen", #用户名 ,也可登录到root
- password = "123" #密码
- )
- #执行命令
- stdin,stdout,stderr = ssh.exec_command("ls /")
- #ssh.exec_command() 返回三个类文件对象
- #stdin是远程主机接收我们写入到数据,stdout是远程主机向我们输出信息
- #stdin 是写权限的文件对象,标准输入
- #stdout 是读权限的文件对象,标准输出
- #stderr 是读权限的文件对象,标准错误,特殊的输出
- #查看执行结果
- result = stdout.read().decode() # 得到远程主机的输出
- print(result)
- #关闭链接,释放内存
- ssh.close()
(二)基于多线程的远程登录
- import threading
- def Exec(command,ip,user,passwd,port = 22):
- """
- 编写执行命令的模块
- """
- ssh = paramiko.SSHClient()
- know_hosts = paramiko.AutoAddPolicy()
- ssh.set_missing_host_key_policy(know_hosts)
- ssh.connect(
- hostname = ip,
- port = port,
- username = user,
- password = passwd
- )
- stdin, stdout, stderr = ssh.exec_command(command)
- result = stdout.read().decode()
- print("+++++++++++++++++%s+++++++++++++++++++++" % ip)
- print(result)
- print("+++++++++++++++++%s+++++++++++++++++++++" % ip)
- ssh.close()
- def main(command,pool):
- lenth = len(pool)
- thread_pool = []
- for i in range(lenth):
- t = threading.Thread(target = Exec,args = (command,)+pool[i])
- thread_pool.append(t)
- for t in thread_pool:
- t.start()
- for t in thread_pool:
- t.join() # join见讲解
- #https://blog.csdn.net/sinat_38068807/article/details/88742964
- if __name__ == "__main__":
- command = input(">>>")
- pool = [
- ("10.10.65.95", "root", "123", 22),
- ("10.10.65.71", "root", "123",)
- ]
- main(command,pool)
(三)基于面向对象的多线程
Pyhton 多线程模块threading通过Thread类构建新的线程
Thread类当做提供了run方法用于子类继承重写
当run方法被修改,在线程调用start的时候,会调用run的功能作为线程的功能。
- import random
- import threading
- from time import sleep
- class ExecThread(threading.Thread):
- def __init__(self):
- super(ExecThread,self).__init__()
- def run(self):
- time = random.randint(1,3)
- sleep(time)
- print("hello world,I will sleep %s second"%time)
- pool = []
- for i in range(10):
- t = ExecThread()
- pool.append(t)
- for i in pool:
- i.start()
- for i in pool:
- i.join()
(四)多线程面向对象
- import threading
- class ExecThread(threading.Thread):
- def __init__(self,command,ip,user,passwd,port = 22):
- self.command = command
- self.ip = ip
- self.user = user
- self.password = passwd
- self.port = port
- super(ExecThread,self).__init__()
- def run(self):
- ssh = paramiko.SSHClient()
- know_hosts = paramiko.AutoAddPolicy()
- ssh.set_missing_host_key_policy(know_hosts)
- ssh.connect(
- hostname = self.ip,
- port = self.port,
- username = self.user,
- password = self.password
- )
- stdin, stdout, stderr = ssh.exec_command(self.command)
- result = stdout.read().decode()
- print("+++++++++++++++++%s+++++++++++++++++++++" % self.ip)
- print(result)
- print("+++++++++++++++++%s+++++++++++++++++++++" % self.ip)
- ssh.close()
- def main(command,pool):
- lenth = len(pool)
- thread_pool = []
- for i in range(lenth):
- ip = pool[i][0]
- user = pool[i][1]
- passwd = pool[i][2]
- if len(pool[i]) > 3:
- port = pool[i][0]
- t = ExecThread(command, ip, user, passwd, port)
- else:
- t = ExecThread(command, ip, user, passwd)
- thread_pool.append(t)
- for t in thread_pool:
- t.start()
- for t in thread_pool:
- t.join()
- if __name__ == "__main__":
- command = input(">>>")
- pool = [
- ("10.10.65.95", "root", "123"),
- ("10.10.65.71", "root", "123")
- ]
- main(command,pool)
(五)交互式编程
- import paramiko
- ssh = paramiko.SSHClient()
- know_hosts = paramiko.AutoAddPolicy()
- ssh.set_missing_host_key_policy(know_hosts)
- ssh.connect(
- hostname = "10.10.65.95", #主机
- port = 22, #端口,默认22
- username = "root", #用户名
- password = "123" #密码
- )
- shell = ssh.invoke_shell() #实例化一个terminal
- shell.settimeout(1) #命令执行的等待时间
- command = input(">>>")+"\n" #\n回车
- shell.send(command) #发送命令
- while True:
- try:
- recv = shell.recv(512).decode() #接受返回
- if recv:
- print(recv)
- else:
- continue
- except:
- command = input(">>>")+"\n"
- shell.send(command)
- ssh.close()
(六)sftp文件上传下载
- import paramiko
- trans = paramiko.Transport(
- sock=(
- "10.10.65.65", 22
- )
- )
- trans.connect(
- username="root",
- password="123"
- )
- sftp = paramiko.SFTPClient.from_transport(trans)
- sftp.put("ssh2.py","/opt/test.py") # 将本机ssh2.py放到远程机的/opt下并命名test.py
- # sftp.get("/opt/main.py", "hello.py")
- sftp.close()