Python网络编程:使用pexpect实现快速ssh连接

简介

本文介绍了使用pexpect模块实现快速ssh登录的功能

实现

下面的例子,在Python 2.6.5的开发环境和Ubuntu系统下,实现ssh的快速连接的代码实现:

#! /usr/bin/env python

import pexpect
import sys

ip = "ssh root@192.168." + sys.argv[1]

try:
        child = pexpect.spawn(ip)
        res = child.expect(['Are you sure you want to continue connecting (yes/no)?', 'password:'])
        if res == 0:
                child.sendline('yes')
                child.expect('password:')

        child.sendline('liuqi80')

        print child.before
        child.interact()

except pexpect.EOF, pexpect.TIMEOUT:
        print "ssh 192.168." + sys.argv[1] + " timeout"
注1: spawn是Pexpect模块主要的类,用以实现启动子程序,它有丰富的方法与子程序交互从而实现用户对子程序的控制。查看spawn的帮助手册

 |  __init__(self, c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值