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()

python paramiko模块SSH自动登录linux系统进行操作的更多相关文章

  1. [转] windows 上用程序putty使用 ssh自动登录Linux(Ubuntu)

    需求: 在Win7电脑上使用putty(一种ssh客户端)连接Ubuntu 工具: puttygen.exe 和 putty.exe 第一步:生成密匙 运行puttygen.exe,选择需要的密匙类型 ...

  2. python paramiko模拟ssh登录,实现sftp上传或者下载文件

    Python Paramiko模块的安装与使用详解 paramiko是短链接,不是持续链接,只能执行你设定的shell命令,可以加分号执行两次命令. http://www.111cn.net/phpe ...

  3. linux expect详解(ssh自动登录)

    shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194 expect "*password:&quo ...

  4. Python Paramiko模块与MySQL数据库操作

    Paramiko模块批量管理:通过调用ssh协议进行远程机器的批量命令执行. 要使用paramiko模块那就必须先安装这个第三方模块,仅需要在本地上安装相应的软件(python以及PyCrypto), ...

  5. ssh自动登录的4种实现方法

    ssh自动登录的几种实现方法,记录在此.  1. 自动ssh/scp方法 A为本地主机(即用于控制其他主机的机器) ; B为远程主机(即被控制的机器Server), 假如ip为192.168.60.1 ...

  6. python paramiko模块学习分享

    python paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Sola ...

  7. 【浅层优化实战】ssh远程登录Linux卡慢的全过程排查及解决方案

    ssh远程登录Linux卡慢的全过程排查及解决方案 前言: 在linux操作系统使用过程中偶然一次感到使用ssh远程连接软件连接操作系统需要等待许久,第一次没在意,第二次也没在意,第三次有点忍受不住了 ...

  8. 使用密钥验证方式登录linux系统

    1.使用PuTTY工具PuTTYgen生成密钥对,打开PuTTYgen,点击Generate生成公钥(生成过程动动鼠标会提升进度哦). 2.将公钥(蓝色的是私钥)放到服务器上去: -在/root目录下 ...

  9. ssh远程登录linux服务器

    ssh远程登录linux服务器 用法: ssh -l user -p port server_ip 或者 ssh -p port user@server_ip 参数: -l 后接要登录的远程系统用户名 ...

随机推荐

  1. html 和 html5(一)(表格 | 列表 | 提交按钮 | 单选 |复选 | 框架 | 脚本 | html字符实体 )

    一.框架 使用iframe来显示目录链接页面 iframe可以显示一个目标链接的页面 目标链接的属性必须使用iframe的属性,如下实例: 实例 <iframe src="demo_i ...

  2. python正则表达式之元字符介绍

    python中元字符及其含义如下: 元字符 含义 . 匹配除换行符以外的任意一个字符 ^ 匹配行首 $ 匹配行尾 ? 重复匹配0次或1次 * 重复匹配0次或更多次 + 重复匹配1次或更多次 {n,} ...

  3. C/C++获取数组的长度

    C.C++中没有提供 直接获取数组长度的函数,对于存放字符串的字符数组提供了一个strlen函数获取长度,那么对于其他类型的数组如何获取他们的长度呢?其中一种方法是使 用sizeof(array) / ...

  4. C类地址

    C类地址第1字节.第2字节和第3个字节为网络地址,第4个字节为主机地址.另外第1个字节的前三位固定为110. C类地址范围:192.0.0.1到223.255.255.255.(第1个字节的二进制值前 ...

  5. php时间函数整理

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  6. River Hopscotch(二分POJ3258)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9263 Accepted: 3994 Descr ...

  7. Unity胶囊体的碰撞检测实现

    可选是否打开矩阵变换,支持xyz三种朝向 using UnityEngine; using System.Collections; using System.Collections.Generic; ...

  8. ReentrantLock的原理学习

    转载:https://my.oschina.net/andylucc/blog/651982 摘要 提到JAVA加锁,我们通常会想到synchronized关键字或者是Java Concurrent ...

  9. Educational Codeforces Round 1 A

    In this problem you are to calculate the sum of all integers from 1 to n, but you should take all po ...

  10. PHP redis负载均衡代码

    <?php /** * This is a Redis exntend class * jay.w */ class RedisClient { public static $instance ...