python, linux shell 使用expect模块自动输入密码


Pexpect 是一个自动控制的 Python 模块,可以用来ssh、ftp、passwd、telnet 等命令行进行自动交互。
官方网站是 http://www.noah.org/
通过它,可以实现类似 expect 的操作。
例如我们可以用它来写python脚本,实现批量对一系列(大量的、配置相同的)的linux服务器进行操作。

 

一、安装方式
以root用户依次执行如下命令:
 wget http://pexpect.sourceforge.net/pexpect-2.3.tar.gz
 tar xzf pexpect-2.3.tar.gz
 cd pexpect-2.3
 sudo python ./setup.py install


二、简单测试

编写一个简单的脚本pexpect_test.py测试一下

 

[python]  view plain  copy
  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3. # filename: pexpect_test.py  
  4. ''''' 
  5. Created on 2010-7-2 
  6.  
  7. @author: forever 
  8. '''  
  9. import pexpect  
  10.   
  11. if __name__ == '__main__':  
  12.     user = 'forever'  
  13.     ip = '192.168.0.200'  
  14.     mypassword = 'forever'  
  15.       
  16.     print user  
  17.     child = pexpect.spawn('ssh %s@%s' % (user,ip))  
  18.     child.expect ('password:')  
  19.     child.sendline (mypassword)  
  20.       
  21.     child.expect('$')  
  22.     child.sendline('sudo -s')  
  23.     child.expect (':')  
  24.     child.sendline (mypassword)  
  25.     child.expect('#')  
  26.     child.sendline('ls -la')  
  27.     child.expect('#')  
  28.     print child.before   # Print the result of the ls command.  
  29.     child.sendline("echo '112' >> /home/forever/1.txt ")  
  30.     child.interact()     # Give control of the child to the user.  
  31.   
  32.     pass  

 如果执行命令中有 (>, |, or *) 的操作, 需要用以下形式

shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])


class spawn(object)  def __init__(self, command, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None, cwd=None, env=None) 
This is the constructor. The command parameter may be a string that includes a command and any arguments to the command. For example:
child = pexpect.spawn ('/usr/bin/ftp')
child = pexpect.spawn ('/usr/bin/ssh user@example.com')
child = pexpect.spawn ('ls -latr /tmp')
You may also construct it with a list of arguments like so:
child = pexpect.spawn ('/usr/bin/ftp', [])
child = pexpect.spawn ('/usr/bin/ssh', ['user@example.com'])
child = pexpect.spawn ('ls', ['-latr', '/tmp'])

After this the child application will be created and will be ready to talk to. For normal use, see expect() and send() and sendline().
Remember that Pexpect does NOT interpret shell meta characters such as redirect, pipe, or wild cards (>, |, or *). This is a common mistake. If you want to run a command and pipe it through another command then you must also start a shell. For example:
child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"')
child.expect(pexpect.EOF)

The second form of spawn (where you pass a list of arguments) is useful in situations where you wish to spawn a command and pass it its own argument list. This can make syntax more clear. For example, the following is equivalent to the previous example:
shell_cmd = 'ls -l | grep LOG > log_list.txt'
child = pexpect.spawn('/bin/bash', ['-c', shell_cmd])

child.expect(pexpect.EOF)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值