当前位置: 首页 > 后端技术 > Python

使用python连接ssh服务器

时间:2023-03-26 01:33:43 Python

使用pip通过paramiko包中的sshconnect方法安装环境依赖实现1.paramiko包中有依赖,需要安装pycrypto2。安装paramiko,在python脚本目录下执行pip命令安装豆瓣的paramiko源文件pip3installparamiko-ihttp://pypi.douban.com/simple/--trusted-hostpypi.douban.comsshConnect.py文件导入paramikossh=paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())#ssh.connect("hostname",port22,"username","password","timeout")ssh.connect("192.168.70.134",22,"root","ncayu@123",timeout=30)#执行多个命令,直接在exec_command()中使用;把它们分开。stdin,stdout,stderr=ssh.exec_command("cd/data/software;ls;ls-l;du-sh*")#循环打印stdout.readlines()中sysOutput的每一行输出:print(sysOutput)#print(stdout.readlines())ssh.关闭()