'''set a substitution variable'''
self.vars[varname] = value
+ def getvar(self, varname):
+ '''return a substitution variable'''
+ return self.vars[varname]
+
def setwinvars(self, vm, prefix='WIN'):
'''setup WIN_XX vars based on a vm name'''
for v in ['VM', 'HOSTNAME', 'USER', 'PASS', 'SNAPSHOT', 'BASEDN', 'REALM', 'DOMAIN']:
ret[i] = self.substitute(ret[i])
return ret
+ """We may have objects such as pexpect.EOF that are not strings"""
+ if not isinstance(text, str):
+ return text
while True:
var_start = text.find("${")
if var_start == -1:
else:
return subprocess.call(cmd, shell=shell, cwd=dir)
+ def run_child(self, cmd, dir="."):
+ cwd = os.getcwd()
+ cmd = self.substitute(cmd)
+ if isinstance(cmd, list):
+ self.info('$ ' + " ".join(cmd))
+ else:
+ self.info('$ ' + cmd)
+ if isinstance(cmd, list):
+ shell=False
+ else:
+ shell=True
+ os.chdir(dir)
+ ret = subprocess.Popen(cmd, shell=shell)
+ os.chdir(cwd)
+ return ret
def cmd_output(self, cmd):
'''return output from and command'''
return ret
+ def get_nameserver(self):
+ '''Get the current nameserver from /etc/resolv.conf'''
+ child = self.pexpect_spawn('cat /etc/resolv.conf')
+ child.expect('nameserver')
+ child.expect('\d+.\d+.\d+.\d+')
+ return child.after
+
def vm_poweroff(self, vmname, checkfail=True):
'''power off a VM'''
self.setvar('VMNAME', vmname)
def get_ipconfig(self, child):
'''get the IP configuration of the child'''
- child.sendline("ipconfig")
+ child.sendline("ipconfig /all")
child.expect('Ethernet adapter ')
child.expect("[\w\s]+")
self.setvar("WIN_NIC", child.after)
child.expect('Default Gateway')
child.expect('\d+.\d+.\d+.\d+')
self.setvar('WIN_DEFAULT_GATEWAY', child.after)
+ child.expect("C:")
+
+ def run_tlntadmn(self, child):
+ '''remove the annoying telnet restrictions'''
+ child.sendline('tlntadmn config maxconn=1024')
+ child.expect("The settings were successfully updated")
+ child.expect("C:")
def disable_firewall(self, child):
'''remove the annoying firewall'''
child.expect("Ok")
child.expect("C:")
+ def set_dns(self, child):
+ child.sendline('netsh interface ip set dns "${WIN_NIC}" static ${INTERFACE_IP} primary')
+ i = child.expect(['C:', pexpect.EOF, pexpect.TIMEOUT], timeout=5)
+ if i > 0:
+ return True
+ else:
+ return False
+
def set_ip(self, child):
- '''fix the IP address to the same value it had when we
+ """fix the IP address to the same value it had when we
connected, but don't use DHCP, and force the DNS server to our
- DNS server. This allows DNS updates to run'''
+ DNS server. This allows DNS updates to run"""
self.get_ipconfig(child)
child.sendline('netsh')
+ child.expect('netsh>')
child.sendline('offline')
- child.sendline('interface ip set dns "${WIN_NIC}" static ${DNSSERVER} primary')
+ child.expect('netsh>')
+ child.sendline('routing ip add persistentroute dest=0.0.0.0 mask=0.0.0.0 name="${WIN_NIC}" nhop=${WIN_DEFAULT_GATEWAY}')
+ child.expect('netsh>')
child.sendline('interface ip set address "${WIN_NIC}" static ${WIN_IPV4_ADDRESS} ${WIN_SUBNET_MASK} ${WIN_DEFAULT_GATEWAY} 1 store=persistent')
- i = child.expect(["The syntax supplied for this command is not valid. Check help for the correct syntax", pexpect.EOF, pexpect.TIMEOUT], timeout=5)
+ i = child.expect(['The syntax supplied for this command is not valid. Check help for the correct syntax', 'netsh>', pexpect.EOF, pexpect.TIMEOUT], timeout=5)
if i == 0:
child.sendline('interface ip set address "${WIN_NIC}" static ${WIN_IPV4_ADDRESS} ${WIN_SUBNET_MASK} ${WIN_DEFAULT_GATEWAY} 1')
- child.sendline('routing ip add persistentroute dest=0.0.0.0 mask=0.0.0.0 name="${WIN_NIC}" nhop=${WIN_DEFAULT_GATEWAY}')
- child.sendline('online')
+ child.expect('netsh>')
child.sendline('commit')
+ child.sendline('online')
child.sendline('exit')
child.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=5)
-
- def open_telnet(self, hostname, username, password, retries=60, delay=5, set_time=False, set_ip=False, disable_firewall=True):
+ return True
+
+ def open_telnet(self, hostname, username, password, retries=60, delay=5, set_time=False, set_ip=False, disable_firewall=True, run_tlntadmn=True):
'''open a telnet connection to a windows server, return the pexpect child'''
set_route = False
+ set_dns = False
while retries > 0:
child = self.pexpect_spawn("telnet " + hostname + " -l '" + username + "'")
i = child.expect(["Welcome to Microsoft Telnet Service",
"No more connections are allowed to telnet server",
"Unable to connect to remote host",
"No route to host",
- "Connection refused"])
+ "Connection refused",
+ pexpect.EOF])
if i != 0:
child.close()
time.sleep(delay)
continue
child.expect("password:")
child.sendline(password)
- child.expect("C:")
+ i = child.expect(["C:",
+ "Denying new connections due to the limit on number of connections",
+ "No more connections are allowed to telnet server",
+ "Unable to connect to remote host",
+ "No route to host",
+ "Connection refused",
+ pexpect.EOF])
+ if i != 0:
+ child.close()
+ time.sleep(delay)
+ retries -= 1
+ continue
+ if set_dns:
+ set_dns = False
+ if self.set_dns(child):
+ continue;
if set_route:
child.sendline('route add 0.0.0.0 mask 0.0.0.0 ${WIN_DEFAULT_GATEWAY}')
child.expect("C:")
+ set_route = False
if set_time:
self.run_date_time(child, None)
+ set_time = False
+ if run_tlntadmn:
+ self.run_tlntadmn(child)
+ run_tlntadmn = False
if disable_firewall:
self.disable_firewall(child)
+ disable_firewall = False
if set_ip:
- self.set_ip(child)
set_ip = False
- set_time = False
- set_route = True
+ if self.set_ip(child):
+ set_route = True
+ set_dns = True
continue
return child
raise RuntimeError("Failed to connect with telnet")