1. 程式人生 > >python, linux shell 使用expect模組自動輸入密碼

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測試一下

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

相關推薦

python, linux shell 使用expect模組自動輸入密碼

Pexpect 是一個自動控制的 Python 模組,可以用來ssh、ftp、passwd、telnet 等命令列進行自動互動。 官方網站是 http://www.noah.org/ 通過它,可以實現類似 expect 的操作。 例如我們可以用它來寫python指令碼

linux 普通使用者登入後,執行 shell指令碼切自動輸入密碼切換到root

1.首先安裝expect 2.2.編寫指令碼  /home/zukgit/suz.sh #!/bin/bash # #!/usr/bin/expect -f 使用這種方式 程式無法執行 expect -c " spawn su - expect \":\" send

通過expect自動輸入密碼登陸遠程服務器

新建 文件 安裝 nbsp exp 輸入密碼 spa 執行 0.10 通過expect免自動輸入密碼登陸遠程服務器 1、前提必須已經安裝expect 2、新建login.sh,文件內容如下 #!/usr/bin/expect -f spawn ssh [email 

shell中scp自動輸入密碼

shell``` #!/bin/bash des_pass=testtest expect -c " spawn scp target/aaa-0.0.1.war [email protected]/* */:/home/GameUser/MagicvesselUserWeb/aaa-releas

ssh采用expect實現自動輸入密碼登錄、拷貝

cep .html tro from 效果 方式 目標 led 交互 1. 引言 最近做了一個項目,需要頻繁與另一臺主機進行文件的傳輸;中間想到了很多方式:FTP、samba、curl等,但是還是感覺scp最好用。 SCP使用教程可參閱:http://www.jb51.

linux shell 安裝模組自動跳過確認

我們在搭建專案執行環境時,往往會用到shell指令碼,不然每次安裝都去手動一行一行命令去敲,費時費力。 下面有一個例項指令碼,install.sh,內容如下(以Ubuntu為例,centos解決辦法一

shell指令碼中自動輸入密碼

在shell指令碼中需要用root用的來執行指令:sudo 自動輸入密碼echo "password" | sudo -S netstat -tlnp       -S          The -S (stdin) option causes sudo to read t

linux使用expect實現自動登入(自動輸入密碼)

小蝙個人比較懶,每次都要登入多臺伺服器上工作,都要檢視note,什麼ip啦,埠啦,使用者名稱啦,密碼啦如果只有一個,那還是挺好記的,但是如果有10個,甚至更多,而且為了安全,每個伺服器的密碼都不一樣,ip和埠也不一樣,這樣就非常痛苦了,顧小蝙利用了一下expect軟體,用指令

expect 普通用戶自動輸入密碼到root下,執行命令

expect案例:當前服務器取消了直接使用root登錄服務器,只能使用普通用戶先登錄,然後再su - root 執行root下的命令。 shell腳本如下: #!/usr/bin/expect -fset password {root_password}spawn su - rootexpect "

Linux下後臺程式完成自動輸入密碼等互動行為的例子

今天要開發一個定時任務,然後加入cron列表中。但是有個問題擺在眼前,指令碼的執行中需要輸入資料庫密碼: mysql -u root -p << SQL use db; set names utf8; `cat task.sql` SQL 執行上述程式碼後,會

Shell指令碼互動之:自動輸入密碼

Shell指令碼互動之:自動輸入密碼 2016年04月09日 19:41:26 zhangjikuan 閱讀數:58208 標籤: Shell互動自動輸入密碼expect重定向管道 更多

linux指令碼實現自動輸入密碼

使用Linux的程式設計師對輸入密碼這個舉動一定不陌生,在Linux下對使用者有嚴格的許可權限制,幹很多事情越過了許可權就得輸入密碼,比如使用超級使用者執行命令,又比如ftp、ssh連線遠端主機等等,如下圖 那麼問題來了,在指令碼自動化執行的時候需要輸入密碼怎麼辦?比如你

[work] sshfs掛載時自動輸入密碼

大家都知道登陸ssh可以用sshpass自動輸入密碼,但是掛載sshfs卻不行,其實sshpss是可以給sshfs輸入密碼的,只需要使用引數-o ssh_command=’sshpass -p password ssh’。 例如: sshfs -o ssh_command='sshpass

Vivo/Oppo手機安裝app時自動輸入密碼以及點選安裝

Vivo手機和Oppo等手機在安裝App的時候總是會有一個討厭的彈窗讓我們輸入密碼或者點選繼續安裝,嚴重的影響了開發效率。如下圖所示 這簡直是無法忍受。於是想著寫一個軟體幫我自動點選。第一是想到的用輔助功能在介面上點。這樣是可以,但是經常服務在後臺被莫名其妙的停止了,而且在密碼窗介

如何在ssh遠端linux伺服器時不需要輸入密碼

目的:   期望A伺服器在對B伺服器執行ssh或者scp等命令的時候不需要輸入密碼 實現方法:   1.通過安裝sshpass服務   2.通過金鑰驗證的方式 操作過程:   一、通過sshpass的方式達到密碼非互動     1.安裝sshpass服務(centeros 7上可直接yum安裝,

postgresql在windows下備份的批處理檔案(修改版),提示輸入密碼時,能自動輸入密碼

在網上找到一篇文章《PostgreSQL Windows自動備份指令碼》 覺得不錯,試了一下,發現批處理檔案執行停止在提示輸入密碼那。 >pg_dump -U postgres -W  database1 > c:/backup.tar >passwo

指令碼連結 ssh 自動輸入密碼

上週工作中,遇見一些重複性的工作,為了方便,於是寫起了指令碼。 在這個指令碼中,需要通過 s s

ubuntu執行sh指令碼sudo自動輸入密碼

示例: sudo apt-get update  第一種方法:使用管道(上一個命令的 stdout 接到下一個命令的 stdin): #!/bin/bashecho password | sudo

linux shell ssh實現自動登入,並且執行一些操作並返回到當前主機

#!/usr/bin/expect -f #-------------------------------------------------- about us # product: monitorone # Author:matthew # Last Modified:

如何自動輸入密碼ssh連線到其他機器

想在shell腳本里ssh連線到另外一臺伺服器後執行一些命令,但是不希望通過互動方式輸入登入密碼。 主要的解決方法有三種: 1. 生成ssh公鑰,建立和對方機器的信任關係; 2. 使用expect指令碼; 3. 使用sshpass。 這裡介紹一下sshpass相