python通过telnetlib模块远程连接交换机
2017-12-28 16:23
513 查看
转载说明:转载请注明出处:http://blog.csdn.net/qq_25134989/article/details/78923564
下面直接上源码
下面是telnetlib模块的API用法(中文版)(来源:http://python.usyiyi.cn/documents/python_352/library/telnetlib.html)
源代码: Lib/telnetlib.py
854。此外,它为协议字符(见下文)和telnet选项提供符号常量。telnet选项的符号名称遵循
telnet命令的符号常量有:IAC,DONT,DO,WONT,WILL,SE(子协议结束),NOP(无操作),DM(数据标记),BRK中止输出),AYT(您有),EC(擦除字符),EL(擦除线),GA(前进),SB(子协商开始)。
class
不要重新打开已连接的实例。
这个类有许多
也可以看看
RFC 854 - Telnet协议规范
Telnet协议的定义。
读取直到遇到预期的给定字节字符串,或直到超时秒过去。
当找不到匹配项时,返回可用的可用空间,可能为空字节。如果连接已关闭且没有可用的已处理过的数据,则引发
读取所有数据,直到EOF为字节;阻塞直到连接关闭。
读取至少一个字节的处理过的数据,除非命中EOF。如果命中EOF,返回
读取I / O(热切)中可以没有阻塞的所有内容。
如果连接关闭且没有可用的已处理过的数据,则引发
阅读随时可用的数据。
如果连接关闭且没有可用的已烹饪数据,则提高
处理和返回队列中已有的数据(延迟)。
如果连接关闭且无可用数据,则提升
返回在cooked队列中可用的任何数据(非常懒惰)。
如果连接关闭且无可用数据,则提升
返回在SB / SE对之间收集的数据(子选项开始/结束)。当使用
连接到主机。可选的第二个参数是端口号,默认为标准Telnet端口(23)。可选的timeout参数指定阻塞操作(如连接尝试)的超时(以秒为单位)(如果未指定,将使用全局默认超时设置)。
不要尝试重新打开已连接的实例。
当调试级别为
设置调试级别。debuglevel的值越高,您获得的调试输出越多(在
关闭连接。
返回内部使用的套接字对象。
返回内部使用的套接字对象的文件描述符。
向套接字写入一个字节字符串,将任何IAC字符加倍。这可以阻止连接被阻止。如果连接关闭,可能会产生
在版本3.3中已更改:此方法用于引发
交互功能,模拟一个非常笨的Telnet客户端。
直到从正则表达式列表中匹配到一个为止。
第一个参数是正则表达式的列表,其被编译(regex
objects)或未编译(字节串)。可选的第二个参数是超时,以秒为单位;默认是无限期阻塞。
返回三个项的元组:匹配的第一个正则表达式的列表中的索引;返回匹配对象;和字节读取直到并包括匹配。
如果找到文件结尾且未读取任何字节,请提出
when nothing matches, return
the bytes received so far (may be empty bytes if a timeout happened).
如果正则表达式以贪婪匹配(例如
/ O定时。
每次在输入流上读取telnet选项时,使用以下参数调用此回调(如果设置):callback(telnet socket,command(DO / DONT / WILL / WONT) 。其后没有其他操作由telnetlib完成。
一个简单的例子说明典型的使用:
21.19.
Telnet客户端
21.19.1.
Telnet对象
21.19.2.
Telnet示例
21.18.
SMTP服务器
21.20.
根据RFC 4122的UUID对象
报告错误
显示源
输入搜索字词或模块,类
英文版如下(来源:https://docs.python.org/2/library/telnetlib.html#telnetlib.Telnet.open)
Source code: Lib/telnetlib.py
The
provides a
that implements the Telnet protocol. See RFC
854 for details about the protocol. In addition, it provides symbolic constants for the protocol characters (see below), and for the telnet options. The symbolic names of the telnet options follow the definitions in
with the leading
see the module source itself.
The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase Character), EL (Erase Line), GA
(Go Ahead), SB (Subnegotiation Begin).
class
a connection to a Telnet server. The instance is initially not connected by default; the
must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor, to, in which case the connection to the server will be established before the constructor returns. The optional timeout parameter
specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).
Do not reopen an already connected instance.
This class has many
that some of them raise
the end of the connection is read, because they can return an empty string for other reasons. See the individual descriptions below.
Changed in version 2.6: timeout was added.
See also
RFC 854 - Telnet Protocol Specification
Definition of the Telnet protocol.
have the following methods:
Read until a given string, expected, is encountered or until timeout seconds have passed.
When no match is found, return whatever is available instead, possibly the empty string. Raise
the connection is closed and no cooked data is available.
Read all data until EOF; block until connection closed.
Read at least one byte of cooked data unless EOF is hit. Return
is hit. Block if no data is immediately available.
Read everything that can be without blocking in I/O (eager).
Raise
connection closed and no cooked data available. Return
in the midst of an IAC sequence.
Read readily available data.
Raise
connection closed and no cooked data available. Return
in the midst of an IAC sequence.
Process and return data already in the queues (lazy).
Raise
connection closed and no data available. Return
midst of an IAC sequence.
Return any data available in the cooked queue (very lazy).
Raise
connection closed and no data available. Return
Return the data collected between a SB/SE pair (suboption begin/end). The callback should access these data when it was invoked with a
This method never blocks.
New in version 2.3.
Connect to a host. The optional second argument is the port number, which defaults to the standard Telnet port (23). The optionaltimeout parameter specifies a timeout in seconds for blocking operations
like the connection attempt (if not specified, the global default timeout setting will be used).
Do not try to reopen an already connected instance.
Changed in version 2.6: timeout was added.
Print a debug message when the debug level is
present, they are substituted in the message using the standard string formatting operator.
Set the debug level. The higher the value of debuglevel, the more debug output you get (on
Close the connection.
Return the socket object used internally.
Return the file descriptor of the socket object used internally.
Write a string to the socket, doubling any IAC characters. This can block if the connection is blocked. May raise
the connection is closed.
Interaction function, emulates a very dumb Telnet client.
Multithreaded version of
Read until one from a list of a regular expressions matches.
The first argument is a list of regular expressions, either compiled (
or uncompiled (strings). The optional second argument is a timeout, in seconds; the default is to block indefinitely.
Return a tuple of three items: the index in the list of the first regular expression that matches; the match object returned; and the text read up till and including the match.
If end of file is found and no text was read, raise
Otherwise, when nothing matches, return
the text received so far (may be the empty string if a timeout happened).
If a regular expression ends with a greedy match (such as
than one expression can match the same input, the results are non-deterministic, and may depend on the I/O timing.
Each time a telnet option is read on the input flow, this callback (if set) is called with the following parameters: callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other action is done
afterwards by telnetlib.
A simple example illustrating typical use:
转载说明:转载请注明出处:http://blog.csdn.net/qq_25134989/article/details/78923564
下面直接上源码
# -*- coding: utf-8 -*- import telnetlib import time def telnetip(tnip): # 连接Telnet服务器 tn = telnetlib.Telnet(tnip, port=23, timeout=50) # 输入登录用户名 tn.read_until('Username:') tn.write(username + '\n') # 输入登录密码 # tn.read_until('Password:') tn.write(password + '\n') time.sleep(1) tn.write('system' + '\n') # 输入命令 tn.write('domain paoa' + '\n') tn.write('****' + '\n') tn.write('****' + '\n') time.sleep(50) result1 = tn.read_very_eager() # 获得结果 print result1 # 命令执行完毕后,终止Telnet连接(或输入exit退出) tn.close() # tn.write('exit\n') if __name__ == '__main__': # 配置选项 ip = '****' # Telnet交换机IP username = '****' # 登录用户名 password = '****' # 登录密码 telnetip(ip)
下面是telnetlib模块的API用法(中文版)(来源:http://python.usyiyi.cn/documents/python_352/library/telnetlib.html)
源代码: Lib/telnetlib.py
telnetlib模块提供了实现Telnet协议的
Telnet类。有关协议的详细信息,请参见 RFC
854。此外,它为协议字符(见下文)和telnet选项提供符号常量。telnet选项的符号名称遵循
arpa/telnet.h中的定义,删除了前导
TELOPT_。对于传统上不包括在
arpa/telnet.h中的选项的符号名称,请参阅模块源本身。
telnet命令的符号常量有:IAC,DONT,DO,WONT,WILL,SE(子协议结束),NOP(无操作),DM(数据标记),BRK中止输出),AYT(您有),EC(擦除字符),EL(擦除线),GA(前进),SB(子协商开始)。
class
telnetlib.
Telnet(host=None, port=0[, timeout])
Telnet表示到Telnet服务器的连接。实例最初未默认连接;必须使用
open()方法建立连接。或者,主机名和可选端口号也可以传递给构造函数,在这种情况下,将在构造函数返回之前建立与服务器的连接。可选的timeout参数指定阻塞操作(如连接尝试)的超时(以秒为单位)(如果未指定,将使用全局默认超时设置)。
不要重新打开已连接的实例。
这个类有许多
read_*()方法。注意,当读取连接结束时,其中一些引发
EOFError,因为它们可能由于其他原因返回一个空字符串。请参阅下面的各个说明。
也可以看看
RFC 854 - Telnet协议规范
Telnet协议的定义。
21.19.1.Telnet对象
Telnet实例具有以下方法:
Telnet.
read_until(expected, timeout=None)
读取直到遇到预期的给定字节字符串,或直到超时秒过去。
当找不到匹配项时,返回可用的可用空间,可能为空字节。如果连接已关闭且没有可用的已处理过的数据,则引发
EOFError。
Telnet.
read_all()
读取所有数据,直到EOF为字节;阻塞直到连接关闭。
Telnet.
read_some()
读取至少一个字节的处理过的数据,除非命中EOF。如果命中EOF,返回
b''。如果没有可用的数据,则阻止。
Telnet.
read_very_eager()
读取I / O(热切)中可以没有阻塞的所有内容。
如果连接关闭且没有可用的已处理过的数据,则引发
EOFError。如果没有熟的数据,返回
b''。除非在IAC序列中间,否则不要阻塞。
Telnet.
read_eager()
阅读随时可用的数据。
如果连接关闭且没有可用的已烹饪数据,则提高
EOFError。如果没有熟的数据,返回
b''。除非在IAC序列中间,否则不要阻塞。
Telnet.
read_lazy()
处理和返回队列中已有的数据(延迟)。
如果连接关闭且无可用数据,则提升
EOFError。如果没有熟的数据,返回
b''。除非在IAC序列中间,否则不要阻塞。
Telnet.
read_very_lazy()
返回在cooked队列中可用的任何数据(非常懒惰)。
如果连接关闭且无可用数据,则提升
EOFError。如果没有熟的数据,返回
b''。这种方法永远不会阻塞。
Telnet.
read_sb_data()
返回在SB / SE对之间收集的数据(子选项开始/结束)。当使用
SE命令调用时,回调应该访问这些数据。这种方法永远不会阻塞。
Telnet.
open(host, port=0[, timeout])
连接到主机。可选的第二个参数是端口号,默认为标准Telnet端口(23)。可选的timeout参数指定阻塞操作(如连接尝试)的超时(以秒为单位)(如果未指定,将使用全局默认超时设置)。
不要尝试重新打开已连接的实例。
Telnet.
msg(msg, *args)
当调试级别为
>0时,打印调试消息。如果存在额外的参数,它们将在消息中使用标准字符串格式化运算符进行替换。
Telnet.
set_debuglevel(debuglevel)
设置调试级别。debuglevel的值越高,您获得的调试输出越多(在
sys.stdout)。
Telnet.
close()
关闭连接。
Telnet.
get_socket()
返回内部使用的套接字对象。
Telnet.
fileno()
返回内部使用的套接字对象的文件描述符。
Telnet.
write(buffer)
向套接字写入一个字节字符串,将任何IAC字符加倍。这可以阻止连接被阻止。如果连接关闭,可能会产生
OSError。
在版本3.3中已更改:此方法用于引发
socket.error,现在是
OSError的别名。
Telnet.
interact()
交互功能,模拟一个非常笨的Telnet客户端。
Telnet.
mt_interact()
interact()的多线程版本。
Telnet.
expect(list, timeout=None)
直到从正则表达式列表中匹配到一个为止。
第一个参数是正则表达式的列表,其被编译(regex
objects)或未编译(字节串)。可选的第二个参数是超时,以秒为单位;默认是无限期阻塞。
返回三个项的元组:匹配的第一个正则表达式的列表中的索引;返回匹配对象;和字节读取直到并包括匹配。
如果找到文件结尾且未读取任何字节,请提出
EOFError。Otherwise,
when nothing matches, return
(-1, None, data)where data is
the bytes received so far (may be empty bytes if a timeout happened).
如果正则表达式以贪婪匹配(例如
.*)结束或者如果多个表达式可以匹配相同的输入,则结果是非确定性的,并且可能取决于I
/ O定时。
Telnet.
set_option_negotiation_callback(callback)
每次在输入流上读取telnet选项时,使用以下参数调用此回调(如果设置):callback(telnet socket,command(DO / DONT / WILL / WONT) 。其后没有其他操作由telnetlib完成。
21.19.2.Telnet示例
一个简单的例子说明典型的使用:import getpass import telnetlib HOST = "localhost" user = input("Enter your remote account: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until(b"login: ") tn.write(user.encode('ascii') + b"\n") if password: tn.read_until(b"Password: ") tn.write(password.encode('ascii') + b"\n") tn.write(b"ls\n") tn.write(b"exit\n") print(tn.read_all().decode('ascii'))
目录
21.19. telnetlib-
Telnet客户端
21.19.1.
Telnet对象
21.19.2.
Telnet示例
上一主题
21.18. smtpd-
SMTP服务器
下一主题
21.20. uuid-
根据RFC 4122的UUID对象
这一页
报告错误显示源
快速搜索
输入搜索字词或模块,类英文版如下(来源:https://docs.python.org/2/library/telnetlib.html#telnetlib.Telnet.open)
Source code: Lib/telnetlib.py
The
telnetlibmodule
provides a
Telnetclass
that implements the Telnet protocol. See RFC
854 for details about the protocol. In addition, it provides symbolic constants for the protocol characters (see below), and for the telnet options. The symbolic names of the telnet options follow the definitions in
arpa/telnet.h,
with the leading
TELOPT_removed. For symbolic names of options which are traditionally not included in
arpa/telnet.h,
see the module source itself.
The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase Character), EL (Erase Line), GA
(Go Ahead), SB (Subnegotiation Begin).
class
telnetlib.
Telnet([host[, port[, timeout]]])
Telnetrepresents
a connection to a Telnet server. The instance is initially not connected by default; the
open()method
must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor, to, in which case the connection to the server will be established before the constructor returns. The optional timeout parameter
specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).
Do not reopen an already connected instance.
This class has many
read_*()methods. Note
that some of them raise
EOFErrorwhen
the end of the connection is read, because they can return an empty string for other reasons. See the individual descriptions below.
Changed in version 2.6: timeout was added.
See also
RFC 854 - Telnet Protocol Specification
Definition of the Telnet protocol.
20.14.1. Telnet Objects
Telnetinstances
have the following methods:
Telnet.
read_until(expected[, timeout])
Read until a given string, expected, is encountered or until timeout seconds have passed.
When no match is found, return whatever is available instead, possibly the empty string. Raise
EOFErrorif
the connection is closed and no cooked data is available.
Telnet.
read_all()
Read all data until EOF; block until connection closed.
Telnet.
read_some()
Read at least one byte of cooked data unless EOF is hit. Return
''if EOF
is hit. Block if no data is immediately available.
Telnet.
read_very_eager()
Read everything that can be without blocking in I/O (eager).
Raise
EOFErrorif
connection closed and no cooked data available. Return
''if no cooked data available otherwise. Do not block unless
in the midst of an IAC sequence.
Telnet.
read_eager()
Read readily available data.
Raise
EOFErrorif
connection closed and no cooked data available. Return
''if no cooked data available otherwise. Do not block unless
in the midst of an IAC sequence.
Telnet.
read_lazy()
Process and return data already in the queues (lazy).
Raise
EOFErrorif
connection closed and no data available. Return
''if no cooked data available otherwise. Do not block unless in the
midst of an IAC sequence.
Telnet.
read_very_lazy()
Return any data available in the cooked queue (very lazy).
Raise
EOFErrorif
connection closed and no data available. Return
''if no cooked data available otherwise. This method never blocks.
Telnet.
read_sb_data()
Return the data collected between a SB/SE pair (suboption begin/end). The callback should access these data when it was invoked with a
SEcommand.
This method never blocks.
New in version 2.3.
Telnet.
open(host[, port[, timeout]])
Connect to a host. The optional second argument is the port number, which defaults to the standard Telnet port (23). The optionaltimeout parameter specifies a timeout in seconds for blocking operations
like the connection attempt (if not specified, the global default timeout setting will be used).
Do not try to reopen an already connected instance.
Changed in version 2.6: timeout was added.
Telnet.
msg(msg[, *args])
Print a debug message when the debug level is
>0. If extra arguments are
present, they are substituted in the message using the standard string formatting operator.
Telnet.
set_debuglevel(debuglevel)
Set the debug level. The higher the value of debuglevel, the more debug output you get (on
sys.stdout).
Telnet.
close()
Close the connection.
Telnet.
get_socket()
Return the socket object used internally.
Telnet.
fileno()
Return the file descriptor of the socket object used internally.
Telnet.
write(buffer)
Write a string to the socket, doubling any IAC characters. This can block if the connection is blocked. May raise
socket.errorif
the connection is closed.
Telnet.
interact()
Interaction function, emulates a very dumb Telnet client.
Telnet.
mt_interact()
Multithreaded version of
interact().
Telnet.
expect(list[, timeout])
Read until one from a list of a regular expressions matches.
The first argument is a list of regular expressions, either compiled (
regex objects)
or uncompiled (strings). The optional second argument is a timeout, in seconds; the default is to block indefinitely.
Return a tuple of three items: the index in the list of the first regular expression that matches; the match object returned; and the text read up till and including the match.
If end of file is found and no text was read, raise
EOFError.
Otherwise, when nothing matches, return
(-1, None, text)where text is
the text received so far (may be the empty string if a timeout happened).
If a regular expression ends with a greedy match (such as
.*) or if more
than one expression can match the same input, the results are non-deterministic, and may depend on the I/O timing.
Telnet.
set_option_negotiation_callback(callback)
Each time a telnet option is read on the input flow, this callback (if set) is called with the following parameters: callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other action is done
afterwards by telnetlib.
20.14.2. Telnet Example
A simple example illustrating typical use:import getpass import sys import telnetlib HOST = "localhost" user = raw_input("Enter your remote account: ") password = getpass.getpass() tn = telnetlib.Telnet(HOST) tn.read_until("login: ") tn.write(user + "\n") if password: tn.read_until("Password: ") tn.write(password + "\n") tn.write("ls\n") tn.write("exit\n") print tn.read_all()
转载说明:转载请注明出处:http://blog.csdn.net/qq_25134989/article/details/78923564
相关文章推荐
- python通过telnetlib模块远程连接交换机
- python中使用telnetlib模块远程实现连接
- 通过python代码远程连接服务器进行操作之paramiko模块
- Python远程连接模块-Telnet
- 解决python通过cx_Oracle模块连接Oracle乱码的问题
- python使用paramiko模块通过ssh2协议对交换机进行配置
- python 使用sqlalchemy,mysqldb模块通过ssh隧道连接mysql
- python 使用mysqldb模块通过ssh隧道连接mysql
- 利用Python通过telnet、ssh连接交换机、路由器等设备配置备份 第2版(粗略版)
- Python用MySQLdb, pymssql 模块通过sshtunnel连接远程数据库
- python 使用mysqldb模块通过ssh隧道连接mysql
- python使用paramiko模块通过ssh2协议对交换机进行配置
- python 使用mysqldb模块通过SSHTunnelForwarder隧道连接mysql
- python远程连接paramiko 模块
- python 使用mysqldb模块通过SSHTunnelForwarder隧道连接mysql
- python下面通过ctypes模块调用c++库的方法
- python使用socket远程连接错误处理方法
- 局域网如何通过SSH 2连接上VMware 10虚拟机新装的centOS系统远程登录
- python利用paramiko连接远程服务器执行命令
- [PowerShell]- 通过WMI连接远程机器并不提示输入密码