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

发布时间:2020-07-23 19:41:20 作者:wsl3511
来源:网络 阅读:1093

单台登录:

#! /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

推荐阅读:
  1. ssh基于秘钥的登陆机制
  2. xshell秘钥验证

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 秘钥 免秘钥

上一篇:一款破解 文件密码的工具,自己用过的

下一篇:VS2005 软件项目目录设置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》