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. 20150618_Andriod_设置TextView垂直滚动

    布局文件 android:scrollbars="vertical" android:singleLine="false" 代码文件 ctl_tv_conten ...

  2. Unity中实现List类型的自定义GUI(ReorderableList)

    感谢韩同学提供的资源 Unity本身提供了float,int,vector3..等类型字段的gui接口,而对于集合类型一般要自己硬写. 官方提供了一个List的自定义GUI,但使用起来非常复杂 Uni ...

  3. Entity Framework 第六篇 分页查询

    目前分页支持单表 , ) where TEntity : class { ) * size; var _reset = Get(filter, orderBy); total = _reset.Cou ...

  4. HTML 5 Web Sockets应用初探

    转载不错的http://developer.51cto.com/art/201008/217874.htm

  5. 第一课 android环境搭建

    android环境搭建需要的工具: 1.JDK 2.eclipse 3.SDK 4.ADT

  6. percona-toolkit系列之介绍和安装(mysql复制工具)

    percona-toolkit使用教程(一) 一.percona-toolkit简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统 ...

  7. Changing Icon File Of Push Button At Runtime In Oracle Forms 6i

    Set Icon_File property in When-Mouse-Enter trigger Suppose you are creating icon based menu system i ...

  8. TI公司Tina-ti和FilterProDesktop下载地址

    http://www.ti.com/tool/tina-ti http://focus.ti.com/en/download/aap/DesignEnv/FilterPro-DT/FilterProD ...

  9. zoj 1081 判断点在多边形内

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81Points Within Time Limit: 2 Second ...

  10. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...