python paramiko模块SSH自动登录linux系统进行操作
2023-09-27 14:24:05 时间
1). Linux系统首先要开启SSH服务:service ssh status
如果没安装的话,则要:apt-get install openssh-server
service ssh restart
2). pip install paramiko
example 1:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.80.139', username = 'allen', password = 'allen', timeout = 300)
cmd = 'cd'
stdin, stdout, stderr = ssh.exec_command(cmd)
cmd = 'ls python'
stdin, stdout, stderr = ssh.exec_command(cmd)
print stdout.read()
#for std in stdout.readlines():
# print std
ssh.close()
如果运行此脚本后“Multibackend cannot be initialized with no backends. If you are seeing this error when trying to use default_backend() please try uninstalling and reinstalling cryptography." 这个错误,那么就:
pip uninstall paramiko pip install paramiko==1.17
参考:http://stackoverflow.com/questions/37135521/paramiko-transport-throws-runtime-valueerror-while-connecting-to-remote-server-u
脚本二在远程服务器上执行相应命令
import sys
import paramiko
hostname = sys.argv[1]
command = " ".join(sys.argv[2:])
port=22
username="allen"
password="allen"
if __name__=="__main__":
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
stdin,stdout,sterr=s.exec_command(command)
print stdout.read()
s.close()
脚本三:管理多台服务器:批量查询ip列表中对应服务器的磁盘使用情况
import paramiko
port = 22
username = "allen"
file=open("ip.list")
for line in file:
hostname = str(line.split("\t")[1])
password = str(line.split("\t")[4]).strip()
print "##########################",hostname,"########################"
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname, port, username, password)
stdin,stdout,sterr = s.exec_command("df -Th")
print stdout.read()
s.close()
file.close()
ip.list内容:
dx 192.168.0.1 22 root loveyou
脚本四:类似于脚本二,在所有远程服务器上执行相应命令
import paramiko
import sys
port=22
username="root"
command = " ".join(sys.argv[1:])
file=open("ip.list")
for line in file:
hostname=str(line.split("\t")[1])
password=str(line.split("\t")[4]).strip()
print "##################",hostname,"######################"
s=paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,password)
stdin,stdout,sterr=s.exec_command(command)
print stdout.read()
s.close()
file.close()
下面是通过ssh的dsa或rsa公钥验证批量登录服务器执行命令:
import paramiko
import sys, os
port = 22
username = "root"
key_file = "~/.ssh/authorized_keys"
know_host = "/home/larry/.ssh/known_hosts"
command = " ".join(sys.argv[1:]) ####获取命令行参数
file = open("ip.list")
for line in file:
hostname = str(line.split(" ")[1]) ####截取ip字段
print "#####################################",hostname,"###############################################"
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.load_system_host_keys(know_host)
s.connect(hostname, port, username, key_file)
stdin, stdout, sterr = s.exec_command(command)
print stdout.read().strip()
s.close()
file.close()
相关文章
- windows python文件拷贝到linux上执行问题-换行符问题/r/n
- Linux下升级Python版本
- linux系统重装yum工具与python环境
- [ python ] 购物系统
- [ Python - 7 ] 简单的省份查询系统
- Python 字典的字符串格式转化为字典格式,txt文本导入转化为字典格式,txt文本导入转化为Dataframe格式,字符串转化为字典
- linux上源码安装python
- linux系统安装了python中,但是为什么执行有问题
- Linux平台下Python脚本编程入门(一)
- deepin linux下python安装mysqldb
- python - Linux C调用Python 函数
- Linux下使用python脚本获取本机CPU,内存,网络使用状态的方法
- Linux 下安装Python报错:zlib not available
- python get方法
- python中文分词工具——结巴分词
- Linux /python --- zipinfo命令
- python代理池的构建2——代理ip是否可用的处理和检查
- Linux 创建python虚拟环境
- Python OpenCV GrabCut进行前景分割和提取
- 【Python入门】Python搭建编程环境-安装Python3解释器(内含Windows版本、MacOS版本、Linux版本)
- Linux Python 及Sudo Python Flask 导包小技巧
- python辅助开发模块(非官方)如pil,mysqldb,openpyxl,xlrd,xlwd
- PyQt(Python+Qt)学习随笔:工具箱(QToolBox)的用途及标签部件项(tabbed widget item)作用介绍
- 第9.1节 Python的文件打开函数open简介
- 第4.3节 Python中与迭代相关的函数
- Python 中异步协程的使用方法介绍
- 必须收藏的python兼职网站大全
- 通用linux程序看门狗(watchdog)python版
- python - crontab模块设置linux定时任务
- 二、python学习之Linux系统命令汇总