Day9
paramiko
实现ssh远程连接
1、简单的ssh
1 #Author: Zachary 2 import paramiko 3 4 # 创建SSH对象 5 ssh = paramiko.SSHClient() 6 # 允许连接不在know_hosts文件中的主机 7 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 8 9 # 连接服务器 10 ssh.connect(hostname='ip', port=22, username='root', password='password') 11 # 执行命令 12 stdin, stdout, stderr = ssh.exec_command('ls') 13 14 # 获取命令结果 15 res,err = stdout.read(),stderr.read() 16 result = res if res else err 17 print(result.decode()) 18 19 # 关闭连接 20 ssh.close()
2、封装ssh
1 #Author: Zachary 2 import paramiko 3 4 transport = paramiko.Transport(('ip', 22)) 5 transport.connect(username='root', password='password') 6 ssh = paramiko.SSHClient() 7 ssh._transport = transport 8 9 stdin, stdout, stderr = ssh.exec_command('df') 10 print(stdout.read().decode()) 11 transport.close()
3、ssh的ftp
1 #Author: Zachary 2 import paramiko 3 4 transport = paramiko.Transport(('ip', 22)) 5 transport.connect(username='root', password='password') 6 sftp = paramiko.SFTPClient.from_transport(transport) 7 8 # 将本地的笔记 上传至服务器 /tmp/test_from_win 9 sftp.put('笔记', '/tmp/test_from_win') 10 # 将服务器端/tmp/test_from_win 下载到本地 from_linux.txt 11 sftp.get('/tmp/test_from_linux', 'from_linux.txt') 12 transport.close()
ssh公钥密钥连接
RSA-非对称密钥验证
公钥-public key
私钥-private key
在linux上
.png)
[root@localhost ~]# more /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAuYPDMdZfuiXNB4imiJPfBHp5IdFkSKGzUr08OqTi0DCkK6qkH2IafqReC04B13hxNBMovHt4e2Z78SY
dVeLhnNpN9mVKfm135MLgkUvwlYwFJVFv/nAoW6Wle7Z4F49gNfHC2cl7f838a+pWZ1U1XZJcCBxPSenCyFij4S/PsWqniQw1ntBc2dg6Y+cEp+sAAL
RS3Wh6MqxJA3RDYObvbAQtia4BP9AkoWv3+w3xxZvKOww1MqNg1ycfdTEtEjAw9U2ogBIOII7pT/gYM3sMPlutMcfLaPm9mvAELVQP2fuUkQgCaQfiy
DRSTfW27KC7bARtOuM8rWdb5cDM94Nhaw== root@localhost.localdomain
1 #Author: Zachary 2 import paramiko 3 private_key = paramiko.RSAKey.from_private_key_file('id_rsa') 4 # 创建SSH对象 5 ssh = paramiko.SSHClient() 6 # 允许连接不在know_hosts文件中的主机 7 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 8 # 连接服务器 9 ssh.connect(hostname='ip', port=22, username='root', pkey=private_key) 10 # 执行命令 11 stdin, stdout, stderr = ssh.exec_command('df') 12 # 获取命令结果 13 result = stdout.read() 14 # 关闭连接 15 ssh.close()
进程与线程
进程processes:以一个整体的形式暴露给操作系统管理,里面包含对各种资源的调用,内存的管理、网络接口的调用等。对各种资源管理的集合 就可以成为 进程
线程thread:是操作系统能够进行运算调度的最小单位,是一串指令的集合
进程要操作cpu,必须要先创建一个线程
什么是线程
A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.
Suppose you're reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.
If you have a roommate, and she's using the same technique, she can take the book while you're not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.
Threads work in the same way. A CPU is giving you the illusion幻觉 that it's doing multiple computations at the same time. It does that by spending a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.
On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers.
Last: threads are different from processes. A thread is a context of execution, while a process is a bunch of resources associated with a computation. A process can have one or many threads.
Clarification: the resources associated with a process include memory pages (all the threads in a process have the same view of the memory), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).
什么是进程
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
进程与线程的区别?
1. Threads share the address space of the process that created it; processes have their own address space.
线程共享内存空间,进程的内存是独立的
2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
4. New threads are easily created; new processes require duplication克隆 of the parent process.
5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.
下面这个例子,总共sleep 2
1 #Author: Zachary 2 import threading,time 3 4 def run(n): 5 print("task",n) 6 time.sleep(2) 7 8 t1= threading.Thread(target=run,args=("t1",)) 9 t2= threading.Thread(target=run,args=("t2",)) 10 t1.start() 11 t2.start()
.png)
1 #Author: Zachary 2 import threading,time 3 class MyThread(threading.Thread): 4 def __init__(self,n): 5 super(MyThread,self).__init__() 6 self.n = n 7 def run(self): 8 print("running task",self.n) 9 t1 = MyThread("t1") 10 t2 = MyThread("t2") 11 t1.start() 12 t2.start()
.png)
当需要执行多次
1 #Author: Zachary 2 import threading,time 3 def run(n): 4 print("task",n) 5 time.sleep(2) 6 start_time = time.time() 7 for i in range(50): 8 t= threading.Thread(target=run,args=("t-%s"%i ,)) 9 t.start() 10 11 print("cost:",time.time() - start_time)
.png)
多线程的效果
1 #Author: Zachary 2 import threading,time 3 class MyThread(threading.Thread): 4 def __init__(self,n,sleep_time): 5 super(MyThread,self).__init__() 6 self.n = n 7 self.sleep_time = sleep_time 8 def run(self): 9 print("running task",self.n) 10 time.sleep(self.sleep_time) 11 print("task done,",self.n) 12 t1 = MyThread("t1",2) 13 t2 = MyThread("t2",4) 14 t1.start() 15 t2.start() 16 t1.join() # wait() 17 print("main thread")
结果会分别打印
.png)
可以打印出总运行时间的,多线程
1 #Author: Zachary 2 import threading,time 3 def run(n): 4 print("task",n) 5 time.sleep(2) 6 start_time = time.time() 7 t_objs = [] 8 for i in range(50): 9 t= threading.Thread(target=run,args=("t-%s"%i ,)) 10 t.start() 11 t_objs.append(t) 12 for t in t_objs: 13 t.join() 14 print("-----------------all threads has finished") 15 print("cost:",time.time() - start_time)
.png)