网上关于pexpect的介绍基本都类似于这样http://blog.csdn.net/sdustliyang/article/details/23373485,但是并没有关于下述问题的解释
问题:可以ssh到主机,但是后面执行的命令无法生效
代码如下:
1 import pexpect 2 import sys 3 4 child = pexpect.spawn('ssh user@ip') 5 fout = file('mylog.txt','w') 6 child.logfile = fout 7 8 child.expect('password:') 9 child.sendline('mypassword')10 11 child.expect('#')12 child.sendline('ls /home')14 child.expect('#') 日志信息:
user@ip's password: mypassword ###################################################################### # Notice # # # # 1. Please DO NOT upgrade the kernel, as the kernel upgrade would # # damage the original operating system. # # # # 2. Please create unique passwords that use a combination of words,# # numbers, symbols, and both upper-case and lower-case letters. # # Avoid using simple adjacent keyboard combinations such as # # "Qwert!234","Qaz2wsx",etc. # # # # 3. Unless necessary, please DO NOT open or use high-risk ports, # # such as Telnet-23, FTP-20/21, NTP-123(UDP), RDP-3389, # # SSH/SFTP-22, Mysql-3306, SQL-1433,etc. # # # # Any questions please contact 4000-955-988 # ######################################################################ls /home
可以看到ssh是成功连接的,但是ls /home命令并没有执行。什么原因呐?我也不知道....网上也找不到