Python自动连接ssh的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python自动连接ssh的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys, time, os
try:
  import pexpect
except ImportError:
  print """
    You must install pexpect module
  """
  sys.exit(1)
addr_map = {
  'v3' :('root@192.168.1.162', 'sina@2009'),
  'dev':('test016@192.168.1.136', 'test016'),
}
try:
  key = sys.argv[1]
  host = addr_map[key]
except:
  print """
    argv error, use it link
    jssh v3, v3 must defined in addr_map
  """
  sys.exit(1)
server = pexpect.spawn('/usr/bin/ssh %s' % host[0])
server.expect('.*ssword:')
server.sendline(host[1])
server.interact()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

解读Python中degrees()方法的使用

 degrees()方法从弧度转换到度角x 语法 以下是degrees()方法的语法: degrees(x) 注意:此函数是无法直接访问的,所以我们需要导入math模...

Python 3.6 中使用pdfminer解析pdf文件的实现

Python 3.6 中使用pdfminer解析pdf文件的实现

所使用python环境为最新的3.6版本 一、安装pdfminer模块 安装anaconda后,直接可以通过pip安装 pip install pdfminer3k  ...

举例详解Python中yield生成器的用法

yield是生成的意思,但是在python中则是作为生成器理解,生成器的用处主要可以迭代,这样简化了很多运算模型(还不是很了解是如何简化的)。 yield是一个表达式,是有返回值的. 当...

Python 存储字符串时节省空间的方法

从 Python 3 开始,str 类型代表着 Unicode 字符串。取决于编码的类型,一个 Unicode 字符可能会占 4 个字节,这个有些时候有点浪费内存。 出于内存占用以及性能...

Python中super()函数简介及用法分享

首先看一下super()函数的定义: super([type [,object-or-type]]) Return a **proxy object** that delegates m...