linux使用Python上秘钥远程登录ssh执行shell命令

网友投稿 651 2022-09-21

linux使用Python上秘钥远程登录ssh执行shell命令

linux使用Python上秘钥远程登录ssh执行shell命令

单台登录:

#! /bin/bash # -*- coding: utf-8 -*- import paramiko ssh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ssh.set_missing_host_key_policy(key) pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') ssh.connect('103.11.30.2', 22, 'root', pkey ,timeout=5) stdin, stdout, stderr = ssh.exec_command('ls') print(stdout.read().decode()) print(stderr.read()) # 关闭连接 ssh.close()

逐行读取多个IP多台登陆,如下

#!/usr/bin/python # -*- coding: utf-8 -*- import time,shutil,os import paramiko appname = "root" ssh = paramiko.SSHClient() key = paramiko.AutoAddPolicy() ssh.set_missing_host_key_policy(key) pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') uptime = time.strftime("%Y%m%d") warpath="/var/tmp/cslc/aw/update/"+uptime def update_all_aw(): for line in open("awip.conf"): print line ssh.connect(line, 22, appname, pkey ,timeout=5) stdin, stdout, stderr = ssh.exec_command('python /var/tmp/awpy/createcopy.py') print(stdout.read().decode()) print(stderr.read()) ssh.close() print("+++++++下一个++++++") return

awip.conf文件防止多台IP地址即可。

参考:python密钥登录主机pythonparamiko通过密钥文件登陆ssh和听过sftp上传文件Python使用paramiko库远程安全连接SSH

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:linux中FTP服务搭建详解--2.家目录禁锢和黑白名单
下一篇:python逐行读取文件脚本并去掉空行
相关文章

 发表评论

暂时没有评论,来抢沙发吧~