python远程控制Linux

安装paramiko

pip install paramiko

操作代码

输出执行语句结果

# coding=utf8

import paramiko
#创建ssh对象
ssh = paramiko.SSHClient()
#连接方式
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#发起连接
ssh.connect('30.1.1.245',22,'root','Hytera@cs123456')
#stdin:标准输入文件 stdout:标准输出文件
stdin,stdout,stderr = ssh.exec_command("ifconfig")
#将执行结果打印出来
print(stdout.read().decode('utf-8'))
#关闭ssh链接
ssh.close()

 

上传下载文件

# coding=utf8
import paramiko
#创建ssh对象
ssh = paramiko.SSHClient()
#连接方式
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#发起连接
ssh.connect('30.1.1.245',22,'root','Hytera@cs123456')

#创建对象
sftp = ssh.open_sftp()
#从远程linux下载文件到本地 sftp.get("远程Linux下的文件路径","本地存储的路径")
sftp.get('/home/0506/VCS/InstallPackage/server/config.xml','D:/CommandCenter/config.xml')

#将本地文件传送到远程Linux sftp.put("本地存储的路径","远程Linux下的文件路径")
sftp.put('D:/CommandCenter/config.xml','/home/0506/VCS/InstallPackage/config.xml')
ssh.close()

 

posted @ 2020-05-18 19:24  linma  阅读(422)  评论(0编辑  收藏  举报