python paramiko通过远程操作linux
python-paramiko通过远程操作linux
1. python-paramiko通过远程操作linux
python3 远程操作linux
使用第三方paramiko库,对于实现运维自动部署很重要
pip3 install paramiko
引用的cryptography输出有问题
pip install cryptography
案例1:通过paramiko使用用户密码远程操作linux
使用python编写,通过paramiko使用用户密码远程操作linux
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py import paramiko # 远程连接 ip和端口
transport = paramiko.Transport( '192.168.0.200', 22 ) # 连接私钥
# pkey = paramiko.RSAKey.from_private_key_file( '/root/.ssh/id_rsa' ) # 连接密码
pwd = '123456' # 连接用户和密码
transport.connect( username = 'root', password = pwd )
# 通过paramiko ssh连接
ssh = paramiko.SSHClient()
ssh._transport = transport # stdio 是输入, stdout 是输出,stderr错误输出
stdio, stdout, stderr = ssh.exec_command( "ifconfig eth0 | awk 'NR==2 {print $2}'" )
channel = stdout.channel
status = channel.recv_exit_status() # stdout标准输出读取数据
stdout = stdout.read().decode() # stderr标准错误数据输出读取数据
stderr = stderr.read().decode() ssh.close()
transport.close() print( stdout ) if status >= 1:
print(stderr)
else:
print("执行成功")
执行结果
# python3 test_paramiko.py
192.168.0.200
案例2:通过paramiko通过密钥远程操作linux
本机安装服务器密钥
[admin@shichaodeMacBook-Pro ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/admin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/admin/.ssh/id_rsa
Your public key has been saved in /Users/admin/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:pU9abOwMKntMChDm70ffna5DCRy2n83c9I71aE7Sb+0 admin@shichaodeMacBook-Pro.local
The key's randomart image is:
+---[RSA 3072]----+
| |
|.. o |
|o. o o . |
|.. + = . |
| .. S % o . |
| .. ... # + o o |
| ..o+o + = o *.o|
| ..+o. o o +oo+|
| o. .+. o.oE|
+----[SHA256]-----+
[admin@shichaodeMacBook-Pro ~]# cd .ssh
[admin@shichaodeMacBook-Pro .ssh]# ll
total 32
drwx------ 6 admin staff 192 Jun 30 11:01 ./
drwxr-xr-x+ 60 admin staff 1920 Jun 30 10:59 ../
-rw------- 1 admin staff 2622 Jun 30 11:01 id_rsa
-rw-r--r-- 1 admin staff 586 Jun 30 11:01 id_rsa.pub
-rw------- 1 admin staff 3616 Jun 10 13:59 known_hosts
-rw------- 1 admin staff 2950 May 10 16:50 known_hosts.old
拷贝公钥到服务器端
[admin@shichaodeMacBook-Pro .ssh]# ssh-copy-id root@192.168.0.200
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/admin/.ssh/id_rsa.pub"
The authenticity of host '192.168.0.200 (192.168.0.200)' can't be established.
ED25519 key fingerprint is SHA256:s/wgZLKfYoMCzTXN3RNfjSlFi6a68iT0wQDkh1CUUQg.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.0.200's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.0.200'"
and check to make sure that only the key(s) you wanted were added.使用key免密钥连接测试
[admin@shichaodeMacBook-Pro .ssh]# ssh root@192.168.0.200
Last login: Thu Jun 30 09:58:32 2022 from 192.168.0.160
[root@ansible ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:35:bb:34:55 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.200 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fe84:914d prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:84:91:4d txqueuelen 1000 (Ethernet)
RX packets 441188 bytes 281747902 (268.6 MiB)
RX errors 0 dropped 37 overruns 0 frame 0
TX packets 243722 bytes 118421451 (112.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 3 bytes 208 (208.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3 bytes 208 (208.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
使用python编写,通过paramiko使用免密钥的方式操作linux
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# Author:shichao
# File: .py import paramiko
import time # 远程连接 ip和端口
transport = paramiko.Transport( '192.168.0.200', 22 ) # 连接私钥
pkey = paramiko.RSAKey.from_private_key_file( '/Users/admin/.ssh/id_rsa' ) # 连接用户和密码
transport.connect( username = 'root', pkey = pkey )
# 通过paramiko ssh连接
ssh = paramiko.SSHClient()
ssh._transport = transport # stdio 是输入, stdout 是输出,stderr错误输出
stdio, stdout, stderr = ssh.exec_command( "ifconfig eth0 | awk 'NR==2 {print $2}'" )
time.sleep(2)
channel = stdout.channel
status = channel.recv_exit_status() # stdout标准输出读取数据
stdout = stdout.read().decode() # stderr标准错误数据输出读取数据
stderr = stderr.read().decode() ssh.close()
transport.close() if status >= 1:
print(stderr)
else:
print(stdout)
运行脚本,查看结果
# python3 test_paramiko.py
192.168.0.200
python paramiko通过远程操作linux的更多相关文章
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息
1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/2 ...
- paramiko模块(远程操作服务器)
paramiko模块(远程操作服务器) django+paramkio实现远程某些服务器执行命令+上传文件 用于帮助开发者通过代码远程连接服务器,并对服务器进行操作. pip3 install par ...
- 远程操作Linux主机
通过putty文件访问: 下载路径:https://the.earth.li/~sgtatham/putty/0.70/w32/putty-0.70-installer.msi 通过Python文件执 ...
- Notepad++【远程操作linux文件】
目录 目的 预期效果 操作步骤 1.打开插件 2.安装NppFTP 3.连接远程主机 注意 目的 通过Notepad++远程登录linux主机,修改配置文件 预期效果 在Notepad++上登录lin ...
- python的paramiko模块-远程登录linux主机并操作
paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作. 如果python服务器对被远程控制机器开启了免密验证,即在python服务器上可通过ssh 用户名@被控制机 ...
- 如何使用python远程操作linux
在云服务测试中,往往需要我们进入云服务内容进行相关内容的测试.这测试可以使用平台自身的noVNC.外部辅助xshell等工具连接到云服务内部进行测试.但是在如此反复的测试操作中,就需要用到自动化测试方 ...
- 使用paramiko模块进行封装,远程操作linux主机
import time import paramiko class HandleParamiko: ''' 定义一个linux处理类 ''' def __init__(self, hostname, ...
- 【转】python fabric实现远程操作和部署
fabric title是开发,但是同时要干开发测试还有运维的活……为毛 task*3 不是 salary * 3 (o(╯□╰)o) 近期接手越来越多的东西,发布和运维的工作相当机械,加上频率还蛮高 ...
- PYTHON FABRIC实现远程操作和部署
转载至:http://wklken.me/posts/2013/03/25/python-tool-fabric.html fabric title是开发,但是同时要干开发测试还有运维的活 (o(╯□ ...
- 利用Paramiko模块远程连接Linux
使用Paramiko模块模拟SSH远程连接到服务器,并执行命令.(支持Tab键补全) 1.安装相关模块: 1)安装 Crypto 模块: 下载源码包解压 安装: sudo python setup.p ...
随机推荐
- Educational Codeforces Round 130 (Rated for Div. 2) C. awoo's Favorite Problem
https://codeforc.es/contest/1697/problem/C 因为规则中,两种字符串变换都与'b'有关,所以我们根据b的位置来进行考虑: 先去掉所有的'b',如果两字符串不相等 ...
- 修改服务器ssh端口
最近,访问公司虚拟机都需要通过堡垒机才能访问了,觉得麻烦.要想不受该规则限制,也有办法,可以通过修改虚拟机ssh端口解决. 下面做个介绍. 1. 通过堡垒机登录虚拟机 2. 修改虚拟机ssh端口 编辑 ...
- SQLSever视图和存储过程
一.视图(View) 1. 为什么要学习视图? 在没有视图之前,我们都是写各种各样的SQL语句,有点,非常灵活.后面我们学习应用程序开发的时候,通过C#发送过来的SQL语句 到达数据库的时候,会执行什 ...
- i春秋Backdoor
点开是道没有任何窗口的题,右键查看源码也没上面东西,抓包试试,也没找到什么提示性的信息,根据提示去看看敏感文件泄露是什么吧 这里找到了篇敏感文件泄露的介绍及利用方法:https://www.cnblo ...
- # Android网络请求(4) 网络请求框架Volley
Android网络请求(4) 网络请求框架Volley Volley是Google在2013年5月15日到17日在旧金山Moscone中心举办网络开发者年会中推出的Android异步网络加载框架和图片 ...
- c++题目:数迷
c++题目:数迷 题目 [题目描述] 给出含有N×N个格子的正方形表格,要求每个格子都填上一个个位数(范围1-N),使得每行.每列以及同一斜线上的数字都不同.部分格子已经填好数字.求满足题意的方案数. ...
- 关于python实现html转word(docx)
安装 linux平台 sudo apt install pandoc pip3 install pypandoc 示例代码 import pypandoc output = pypandoc.conv ...
- flutter系列之:在flutter中使用流式布局
目录 简介 Flow和FlowDelegate Flow的应用 总结 简介 我们在开发web应用的时候,有时候为了适应浏览器大小的调整,需要动态对页面的组件进行位置的调整.这时候就会用到flow la ...
- 实践案例:同程艺龙网的 Dubbo 升级经验总结
本篇为同程艺龙旅行网 Apache Dubbo 的实践案例总结.感兴趣的朋友可以访问官网了解更多详情,或搜索关注官方微信公众号 Apache Dubbo 跟进最新动态. 作者信息: 严浩:同程艺龙高级 ...
- 【数据库】union和union all合并结果操作
一.含义 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的 ...