一: 使用paramiko
#设置ssh连接的远程主机地址和端口
t=paramiko.Transport((ip,port))
#设置登录名和密码
t.connect(username=username,password=password)
#连接成功后打开一个channel
chan=t.open_session()
#设置会话超时时间
chan.settimeout(session_timeout)
#打开远程的terminal
chan.get_pty()
#激活terminal
chan.invoke_shell()
然后就可以通过chan.send('command')和chan.recv(recv_buffer)来远程执行命令以及本地获取反馈。
paramiko有两个模块SSHClient()和SFTPClient()
利用SSHClient()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #coding:utf-8 import paramiko #创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname = '192.168.2.103' , port = 22 , username = 'root' , password = '123456' ) # 执行命令 stdin, stdout, stderr = ssh.exec_command( 'ls' ) # 获取命令结果 result = stdout.read() print ( str (result,encoding = 'utf-8' )) # 关闭连接 ssh.close() |
SSHClient()里面有一个transport变量,这个是用于获取连接的,因此我们也可以单独的获取到transport变量,然后执行连接操作
1 2 3 4 5 6 7 8 9 10 11 12 13 | #coding:utf-8 import paramiko transport = paramiko.Transport(( '192.168.2.103' , 22 )) transport.connect(username = 'root' , password = '123456' ) ssh = paramiko.SSHClient() ssh._transport = transport stdin, stdout, stderr = ssh.exec_command( 'df' ) print ( str (stdout.read(),encoding = 'utf-8' )) transport.close() |
用transport实现上传下载以及命令的执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #coding:utf-8 import paramiko import uuid class SSHConnection( object ): def __init__( self , host = '192.168.2.103' , port = 22 , username = 'root' ,pwd = '123456' ): self .host = host self .port = port self .username = username self .pwd = pwd self .__k = None def connect( self ): transport = paramiko.Transport(( self .host, self .port)) transport.connect(username = self .username,password = self .pwd) self .__transport = transport def close( self ): self .__transport.close() def upload( self ,local_path,target_path): # 连接,上传 # file_name = self.create_file() sftp = paramiko.SFTPClient.from_transport( self .__transport) # 将location.py 上传至服务器 /tmp/test.py sftp.put(local_path, target_path) def download( self ,remote_path,local_path): sftp = paramiko.SFTPClient.from_transport( self .__transport) sftp.get(remote_path,local_path) def cmd( self , command): ssh = paramiko.SSHClient() ssh._transport = self .__transport # 执行命令 stdin, stdout, stderr = ssh.exec_command(command) # 获取命令结果 result = stdout.read() print ( str (result,encoding = 'utf-8' )) return result ssh = SSHConnection() ssh.connect() ssh.cmd( "ls" ) ssh.upload( 's1.py' , '/tmp/ks77.py' ) ssh.download( '/tmp/test.py' , 'kkkk' ,) ssh.cmd( "df" ) ssh.close() |
二,与linux连接
下面是两种使用paramiko连接到linux服务器的代码
方式一:
1 2 3 | 1 ssh = paramiko.SSHClient() 2 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 3 ssh.connect( "某IP地址" , 22 , "用户名" , "口令" ) |
方式二:
1 2 | 1 t = paramiko.Transport((“主机”,”端口”)) 2 t.connect(username = “用户名”, password = “口令”) |
SFTPClient()也是使用transport来实现的,因此如果有需求需要执行命令和上传文件糅合在一起的话,那么就需要使用transport的方式来实现。
如果连接远程主机需要提供密钥,上面第二行代码可改成:
1 | t.connect(username = “用户名”, password = “口令”, hostkey = ”密钥”) |
2.1 windows对linux运行任意命令,并将结果输出
如果linux服务器开放了22端口,在windows端,我们可以使用paramiko远程连接到该服务器,并执行任意命令,然后通过 print或其它方式得到该结果,
代码如下:
1 2 3 4 5 6 7 8 9 10 | #coding:Utf8 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( "某IP地址" , 22 , "用户名" , "口令" ) stdin, stdout, stderr = ssh.exec_command( "你的命令" ) print stdout.readlines() ssh.close() |
其中的”你的命令”可以任意linux支持的命令
2.2 从widnows端下载linux服务器上的文件:
1 2 3 4 5 6 7 8 9 10 11 | coding:utf8 import paramiko t = paramiko.Transport((“主机”,”端口”)) t.connect(username = “用户名”, password = “口令”) sftp = paramiko.SFTPClient.from_transport(t) remotepath = ’ / var / log / system.log’ localpath = ’ / tmp / system.log’ sftp.get(remotepath, localpath) t.close() |
2.3 从widnows端上传文件到linux服务器:
1 2 3 4 5 6 7 8 9 | import paramiko t = paramiko.Transport((“主机”,”端口”)) t.connect(username = “用户名”, password = “口令”) sftp = paramiko.SFTPClient.from_transport(t) remotepath = ’ / var / log / system.log’ localpath = ’ / tmp / system.log’ sftp.put(localpath,remotepath) t.close() |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库