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 ...
随机推荐
- nordic——nrf52系列SWD设置回读保护
在开发时可能需要回读保护功能,在产品出厂后这个功能可以让你的代码更加安全,无法用SEGGER或者其余方式读取你的代码HEX文件,也就是禁用SWD下载接口.但是SWD锁住了,还想使用(从新下载代码)也是 ...
- 在Rocky8中安装VMware Workstation 的方法
在Rocky8中安装VMware Workstation 的方法 1.Rocky必须是图形界面 2.下载wmware workstation(下载地址:https://www.vmware.com/i ...
- 并发编程之 ThreadLocal
前言 了解过 SimpleDateFormat 时间工具类的朋友都知道,该工具类非常好用,可以利用该类可以将日期转换成文本,或者将文本转换成日期,时间戳同样也可以. 以下代码,我们采用通用的 Simp ...
- Linux--多线程(三)
生产者消费者模型 概念: 生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题.生产者和消费者彼此之间不直接通讯,而通过一个来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给 ...
- zk系列二:zookeeper实战之分布式统一配置获取
前面介绍了zk的一些基础知识,这篇文章主要介绍下如何在java环境下获取zk的配置信息:主要基于zk的监听器以及回调函数通过响应式编程的思想将核心代码糅合成一个工具类,几乎做到了拿来即用: 在分布式集 ...
- AIR32F103(五) FreeRTOSv202112核心库的集成和示例代码
目录 AIR32F103(一) 合宙AIR32F103CBT6开发板上手报告 AIR32F103(二) Linux环境和LibOpenCM3项目模板 AIR32F103(三) Linux环境基于标准外 ...
- C#在Xp系统执行.exe程序的报错怎么查看原因
我的电脑---->管理---->事件查看器----->应用程序,查看错误来源
- 修改api-server支持的NodePort端口映射范围
创建svc资源报错显示:provided port is not in the valid range. The range of valid ports is 30000-32767 k8s集群默认 ...
- 2022-11-14 Acwing每日一题
本系列所有题目均为Acwing课的内容,发表博客既是为了学习总结,加深自己的印象,同时也是为了以后回过头来看时,不会感叹虚度光阴罢了,因此如果出现错误,欢迎大家能够指出错误,我会认真改正的.同时也希望 ...
- 云原生之旅 - 13)基于 Github Action 的自动化流水线
前言 GItHub Actions是一个持续集成和持续交付的平台,能够让你自动化你的编译.测试和部署流程.GitHub 提供 Linux.Windows 和 macOS 虚拟机来运行您的工作流程,或者 ...