标签:idt sub pat 目标 highlight pass transport container cell
ssh username@目标机器ip -p 22 -o ProxyCommand=‘ssh -p 22 username@跳板机ip -W %h:%p‘
1.1、基于用户名密码连接远程服务器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname = ‘c1.salt.com‘ , port = 22 , username = ‘shuaigaogao‘ , password = ‘0808‘ ) # 执行命令 stdin, stdout, stderr = ssh.exec_command( ‘df‘ ) #stdin => 标准输入,就是你输入的那个命令 stdout => 标准输出,你输入命令后执行的结果 stderr => 标准错误,名利执行的过程中,如果出错了,就把这个错误打到这里 # 获取命令结果 result = stdout.read() #输出的结果是bytes类型的 # 关闭连接 ssh.close() |
1.1.1、SSHClient 封装 Transport
1
2
3
4
5
6
7
8
9
10
11
12
|
import paramiko transport = paramiko.Transport(( ‘hostname‘ , 22 )) transport.connect(username = ‘zhangqigao‘ , password = ‘123‘ ) ssh = paramiko.SSHClient() ssh._transport = transport stdin, stdout, stderr = ssh.exec_command( ‘df‘ ) print stdout.read() transport.close() |
2.1、基于用户名密码上传下载
1
2
3
4
5
6
7
8
9
10
11
12
|
import paramiko transport = paramiko.Transport(( ‘hostname‘ , 22 )) transport.connect(username = ‘zhangqigao‘ ,password = ‘123‘ ) sftp = paramiko.SFTPClient.from_transport(transport) # 将location.py 上传至服务器 /tmp/test.py sftp.put( ‘/tmp/location.py‘ , ‘/tmp/test.py‘ ) # 将remove_path 下载到本地 local_path sftp.get( ‘remove_path‘ , ‘local_path‘ ) transport.close() |
标签:idt sub pat 目标 highlight pass transport container cell
原文地址:https://www.cnblogs.com/yaohu/p/11494377.html