python通过paramiko实现,ssh功能 import paramiko ssh =paramiko.SSHClient()#创建一个SSH连接对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#允许连接不在KNOV_HOSTs文件中的主机 自动添加 ssh.connect(hostname='192.168.11.51',port=22,username='yjj',password='yjj')#连接,主机 端口 用户名 密码 stdin,stdout,stderr=ssh.exec_command('df')#.exec_command 为执行命令,返回结果 ,标准输入,标准输出,标准错误,错误与输出只会返回其一 result=stdout.read()#获取结果 #result2=stdin.read()#获取结果 #result3=stderr.read()#获取结果 #print(result,result2,result3) result=result.decode() print(result) ssh.close()#关闭连接
ftp function
import paramiko #创建一个传输通道对象 transport=paramiko.Transport(('192.168.11.50',22))#传输模块 Transport 服务器地址 端口 transport.connect(username='root',password='yjj')#用户名,,密码 sftp=paramiko.SFTPClient.from_transport(transport)#调用传输方法 sftp.put('test2','/home/yjj/test2')#上传文件 ,本地路径文件 ,服务器的路径文件 sftp.get('/home/yjj/test1','test1')#下载文件 ,服务器的路径文件 ,本地路径文件
For security reasons, plaintext passwords are not used and RSA asymmetric keys are used to log in automatically
Under linux: Generate key
Transmit to the server where you want to log in:
If the transmission is successful, you can successfully log in to the corresponding user on the server
If you log in to linux from windows
You can copy the private key to windows
Specify the private key through paramiko.RSAKey for access
ssh function:
import paramiko priv_key=paramiko.RSAKey.from_private_key_file('id_rsa')#指定私钥文件 ssh=paramiko.SSHClient()#生成ssh对象 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#允许连接不在KNOV_HOSTs文件中的主机 自动添加 ssh.connect(hostname='192.168.11.50',port=22,username='root',pkey=priv_key)#连接,主机 端口 用户名 私钥 stdin,stdout,stderr=ssh.exec_command('df')#.exec_command 为执行命令,返回结果 ,标准输入,标准输出,标准错误,错误与输出只会返回其一 result=stdout.read()#获取结果 result=result.decode() print(result) ssh.close()#关闭连接
ftp function:
import paramiko priv_key=paramiko.RSAKey.from_private_key_file('id_rsa')#指定私钥文件 #创建一个传输通道对象 transport=paramiko.Transport(('192.168.11.50',22))#传输模块 Transport 服务器地址 端口 transport.connect(username='root',pkey=priv_key)#用户名,,私钥 sftp=paramiko.SFTPClient.from_transport(transport)#调用传输方法 sftp.put('test2','/home/yjj/test2-2')#上传文件 ,本地路径文件 ,服务器的路径文件 sftp.get('/home/yjj/test1','test1-2')#下载文件 ,服务器的路径文件 ,本地路径文件 with open('test1-2','r',encoding='utf-8') as f: s=f.readlines() print(s)
The above is the detailed content of python learning diary (50)--paramiko. For more information, please follow other related articles on the PHP Chinese website!

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
