本文整理汇总了Python中telnetlib.Telnet方法的典型用法代码示例。如果您正苦于以下问题:Python telnetlib.Telnet方法的具体用法?Python telnetlib.Telnet怎么用?Python telnetlib.Telnet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类telnetlib
的用法示例。
在下文中一共展示了telnetlib.Telnet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: interactive
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def interactive(self):
"""
Mimic an interctive session.
"""
# Get the Telnet prompt
prompt = self.wait_for_prompt()
sys.stdout.write("%s" % prompt)
while True:
command = sys.stdin.readline().strip()
# Fake command that ease quitting the interactive session
if command == "exit":
break
self.send_command(command)
data = self.recv_command()
sys.stdout.write(data)
示例2: __init__
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def __init__(self, ip, user, password, read_timeout=None):
import telnetlib
self.tn = telnetlib.Telnet(ip, timeout=15)
self.read_timeout = read_timeout
if b'Login as:' in self.tn.read_until(b'Login as:', timeout=read_timeout):
self.tn.write(bytes(user, 'ascii') + b"\r\n")
if b'Password:' in self.tn.read_until(b'Password:', timeout=read_timeout):
# needed because of internal implementation details of the telnet server
time.sleep(0.2)
self.tn.write(bytes(password, 'ascii') + b"\r\n")
if b'for more information.' in self.tn.read_until(b'Type "help()" for more information.', timeout=read_timeout):
# login succesful
from collections import deque
self.fifo = deque()
return
raise PyboardError('Failed to establish a telnet connection with the board')
示例3: portScanner
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def portScanner(url,dosyaAdi):
raporIcerik=""
baslangic=int(raw_input("Start port: "))
bitis=int(raw_input("Finish port: "))
for port in range(baslangic, bitis, 1):
try:
i=str(port)
baglanti = telnetlib.Telnet(url, i)
baglanti.write("\n")
print "[+]", str(port), " - ", baglanti.read_all().splitlines()[0]
raporIcerik+="[+]", str(port), " - ", baglanti.read_all().splitlines()[0]+"\n"
baglanti.close()
except:
pass
rapor = open(dosyaAdi, "a")
rapor.write(raporIcerik)
rapor.close()
示例4: method
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def method(url,dosyaAdi):
try:
telnetBaglanti = telnetlib.Telnet(url, 80)
telnetBaglanti.write("OPTIONS / HTTP/1.1\n")
komut = "Host: " + url + "\n\n\n\n"
telnetBaglanti.write(komut)
sayfa = telnetBaglanti.read_all()
deger = str(sayfa).find("Allow")
deger2 = str(sayfa).find("\n", deger + 1)
Metodlar = sayfa[deger:deger2]
print "Methods: ", Metodlar
raporIcerik="[+]Methods: "+Metodlar+"\n"
rapor = open(dosyaAdi, "a")
rapor.write(raporIcerik)
rapor.close()
except:
pass
示例5: _test_read_any_eager_A
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def _test_read_any_eager_A(self, func_name):
"""
read_very_eager()
Read all data available already queued or on the socket,
without blocking.
"""
want = [self.block_long, 'x' * 100, 'y' * 100, EOF_sigil]
expects = want[1] + want[2]
self.dataq.put(want)
telnet = telnetlib.Telnet(HOST, self.port)
self.dataq.join()
func = getattr(telnet, func_name)
data = ''
while True:
try:
data += func()
self.assertTrue(expects.startswith(data))
except EOFError:
break
self.assertEqual(expects, data)
示例6: test_read_lazy_A
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def test_read_lazy_A(self):
want = ['x' * 100, EOF_sigil]
self.dataq.put(want)
telnet = telnetlib.Telnet(HOST, self.port)
self.dataq.join()
time.sleep(self.block_short)
self.assertEqual('', telnet.read_lazy())
data = ''
while True:
try:
read_data = telnet.read_lazy()
data += read_data
if not read_data:
telnet.fill_rawq()
except EOFError:
break
self.assertTrue(want[0].startswith(data))
self.assertEqual(data, want[0])
示例7: test_read_very_lazy_A
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def test_read_very_lazy_A(self):
want = ['x' * 100, EOF_sigil]
self.dataq.put(want)
telnet = telnetlib.Telnet(HOST, self.port)
self.dataq.join()
time.sleep(self.block_short)
self.assertEqual('', telnet.read_very_lazy())
data = ''
while True:
try:
read_data = telnet.read_very_lazy()
except EOFError:
break
data += read_data
if not read_data:
telnet.fill_rawq()
self.assertEqual('', telnet.cookedq)
telnet.process_rawq()
self.assertTrue(want[0].startswith(data))
self.assertEqual(data, want[0])
示例8: _test_command
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def _test_command(self, data):
""" helper for testing IAC + cmd """
self.setUp()
self.dataq.put(data)
telnet = telnetlib.Telnet(HOST, self.port)
self.dataq.join()
nego = nego_collector()
telnet.set_option_negotiation_callback(nego.do_nego)
txt = telnet.read_all()
cmd = nego.seen
self.assertTrue(len(cmd) > 0) # we expect at least one command
self.assertIn(cmd[0], self.cmds)
self.assertEqual(cmd[1], tl.NOOPT)
self.assertEqual(len(''.join(data[:-1])), len(txt + cmd))
nego.sb_getter = None # break the nego => telnet cycle
self.tearDown()
示例9: cmd
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def cmd(self, cmd_str, wait_ret=True):
if not isinstance(cmd_str, str):
raise TypeError("Invalid command string", cmd_str)
if not self.is_open:
raise attenuator.Error("Telnet connection not open for commands")
cmd_str.strip(self.tx_cmd_separator)
self._tn.read_until(_ascii_string(self.prompt), 2)
self._tn.write(_ascii_string(cmd_str + self.tx_cmd_separator))
if wait_ret is False:
return None
match_idx, match_val, ret_text = self._tn.expect(
[_ascii_string("\S+" + self.rx_cmd_separator)], 1)
if match_idx == -1:
raise attenuator.Error(
"Telnet command failed to return valid data")
ret_text = ret_text.decode()
ret_text = ret_text.strip(self.tx_cmd_separator +
self.rx_cmd_separator + self.prompt)
return ret_text
示例10: connectraw
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def connectraw(self):
# Try up to 10 times to connect to LMS
connection_failed = 0
self.rawserver = None
logging.debug(u"Connecting to LMS raw service on {0}:{1}".format(self.server, self.port))
while True:
if connection_failed >= 10:
logging.debug(u"Could not connect to raw LMS service")
break
try:
# Connection to LMS
self.rawserver = telnetlib.Telnet(self.server, self.port)
# Subscribe to notification events that should wake up the system to collect data
self.rawserver.write("subscribe pause,play,mixer,playlist\n".encode('ascii'))
break
except IOError:
self.rawserver = None
connection_failed += 1
time.sleep(1)
if self.rawserver is None:
raise IOError(u"Could not connect raw to LMS")
示例11: test_SB_commands
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def test_SB_commands(self):
# RFC 855, subnegotiations portion
send = [tl.IAC + tl.SB + tl.IAC + tl.SE,
tl.IAC + tl.SB + tl.IAC + tl.IAC + tl.IAC + tl.SE,
tl.IAC + tl.SB + tl.IAC + tl.IAC + 'aa' + tl.IAC + tl.SE,
tl.IAC + tl.SB + 'bb' + tl.IAC + tl.IAC + tl.IAC + tl.SE,
tl.IAC + tl.SB + 'cc' + tl.IAC + tl.IAC + 'dd' + tl.IAC + tl.SE,
EOF_sigil,
]
self.dataq.put(send)
telnet = telnetlib.Telnet(HOST, self.port)
self.dataq.join()
nego = nego_collector(telnet.read_sb_data)
telnet.set_option_negotiation_callback(nego.do_nego)
txt = telnet.read_all()
self.assertEqual(txt, '')
want_sb_data = tl.IAC + tl.IAC + 'aabb' + tl.IAC + 'cc' + tl.IAC + 'dd'
self.assertEqual(nego.sb_seen, want_sb_data)
self.assertEqual('', telnet.read_sb_data())
nego.sb_getter = None # break the nego => telnet cycle
示例12: test_telnet
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def test_telnet(hostname, port):
# This attempts to test the validity of a controller
# It returns a tuple (probably not the best way to do this) which is in the format:
# Initial Connection Test (bool), Version Response Test (bool), Version Response (str)
try:
tn = telnetlib.Telnet(host=hostname, port=port, timeout=3)
except socket.timeout:
return False, False, None
except ConnectionRefusedError:
return False, False, None
try:
tn.write("n\r\n")
version_string = tn.read_until("}",3)
except:
return True, False, None
return True, True, version_string
# The following was used for testing during development
示例13: start_bootloader
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def start_bootloader(self, bin_path):
log.info("Starting openocd")
proc = subprocess.Popen([
'openocd',
'-s', '/usr/share/openocd',
'-f', 'assets/adapters/{}.cfg'.format(self._jtag_hardware),
'-f', 'assets/boards/ed20.cfg'
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
wait = 10
log.info("Waiting for {} seconds".format(wait))
sleep(wait)
client = telnetlib.Telnet('localhost', 4444)
log.debug(client.read_until("> "))
log.info("Halting CPU")
client.write("soft_reset_halt\n")
log.debug(client.read_until("> "))
sleep(0.1)
client.write("reset halt\n")
log.debug(client.read_until("> "))
sleep(0.1)
log.info("Loading new image to RAM")
client.write("load_image {} 0xa1f00000\n".format(bin_path))
log.debug(client.read_until("> "))
sleep(0.1)
log.info("Starting up new image")
client.write("resume 0xa1f00000\n")
except:
try:
log.exception(proc.communicate()[0])
except:
pass
proc.terminate()
raise
proc.terminate()
示例14: __init__
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def __init__(self, host):
self.tlnt = telnetlib.Telnet(host, 23)
# Announce that we will send strings character per character
self.write_raw(telnetlib.IAC + telnetlib.WONT + telnetlib.LINEMODE)
示例15: close
# 需要导入模块: import telnetlib [as 别名]
# 或者: from telnetlib import Telnet [as 别名]
def close(self):
"""
Close the Telnet session.
"""
return self.tlnt.close()