[英]Reading 'ipconfig' with Python-Pexpect
I'm running freeSSHd on win XP as an SSH server and it's returning CMD shell for authenticated users, and I want to get the result of sending 'ipconfig' command automatically when I run the python script, I got the connection working but I have an issue with reading the result for each field and putting it in separate variable, the result for my code is '4' instead of '10' > first digit in IP address, I don't know where did number 4 come from. 我在win XP上作为SSH服务器在freeXPd上运行,并且正在返回经过身份验证的用户的CMD shell,我想在运行python脚本时获得自动发送“ ipconfig”命令的结果,但连接正常读取每个字段的结果并将其放在单独的变量中的问题,我的代码的结果是“ 4”,而不是“ 10”> IP地址的第一个数字,我不知道数字4的来源。 Any idea? 任何想法?
Output of Win XP ipconfig Win XP ipconfig的输出
C:\\Documents and Settings\\hussam\\Desktop>ipconfig C:\\ Documents and Settings \\ hussam \\ Desktop> ipconfig
Windows IP Configuration
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 10.0.2.10
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.0.2.15
C:\Documents and Settings\hussam\Desktop>
My Code 我的密码
import pexpect
import re
def sendcommand(conn,command):
conn.sendline(command)
conn.expect('\d')
print conn.after
def c(ip,username,password):
global conn
ft = 'ssh '+username+'@'+ip
print 'we are trying to connect to ' + ft
new = 'Are you sure you want to continue connecting'
conn = pexpect.spawn(ft)
result = conn.expect([ pexpect.TIMEOUT , new ,'[P|p]assword:' ])
if result == 0:
print 'connection error'
return
if result == 1:
conn.sendline('yes')
result = conn.expect([ pexpect.TIMEOUT , '[P|p]assword:'])
if result == 0:
print 'connection error'
return
conn.sendline(password)
conn.expect('>')
sendcommand(conn,command)
def main ():
global command
username = 'hkhrais'
ip = '10.0.2.10'
password = 'hkhrais'
command = ' ipconfig'
c(ip,username,password)
main ()
It is way simpler to use WMI
: 使用WMI
更简单:
import wmi
def return_ip_addresses(user,passwd,machine):
i = wmi.WMI(machine, user=user, password=passwd)
addresses = []
for interface in i.Win32_NetworkAdapterConfiguration(IPEnabled=1):
for ip_address in interface.IPAddress:
addresses.append(ip_address)
return addresses
if __name__ == '__main__':
print return_ip_addresses('user','secret','10.1.1.1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.