Skip to content

This is an easy to use telnet client module to interact with a remote system smoothly over this protocol! It is a very minimalistic alterative to "telnetlib".

License

Notifications You must be signed in to change notification settings

AlaBouali/xtelnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 

Repository files navigation

xtelnet

This is an easy to use telnet module to interact with a remote system smoothly over this protocol! It is a very minimalistic alterative to "telnetlib". xtelnet is a powerful and user-friendly Python library designed for managing Telnet sessions with ease and efficiency. With its intuitive interface and robust functionality, xtelnet simplifies the process of interacting with Telnet servers, offering a range of features for seamless communication. xtelnet offers a comprehensive solution for Telnet communication, providing developers with the tools they need to effectively manage Telnet sessions and interact with remote systems. Whether you're a seasoned developer or new to Telnet protocols, xtelnet empowers you to achieve your goals efficiently and reliably.

Why should I use xtelnet?

  • Easy to use and stable
  • Simple Authentication mechanism
  • Handle telnet negotiations automatically for you
  • Compatible with python 2 and 3
  • Set custom Telnet negotiation options
  • parse commands output and returns only necessary output ( command's ouput, nothing extra )
  • Compatible with almost all servers when it comes to authentication and executing the commands
  • Available Command line tool
  • Thread-safe: if the session is shared among threads to execute commands, the commands will be executed one by one
  • Supports running multiple sessions concurrently
  • Can connect simultaneously and run in parallel the same command on: single or some or all connected hosts
  • Allow reconnect after closing the connection
  • Allow escape ANSI characters
  • Grab banners
  • Available "ping" function to use if you want to keep the connection open
  • Supports SOCKS 4 / 5 proxies
  • Supports SSL
  • Supports sending JSON data

Install :

pip install xtelnet

or

pip3 install xtelnet

Usage on a script :

import xtelnet
t=xtelnet.Telnet_Session()
ip='192.168.0.32'#just an example
# if you are using "stupid" tcp servers, just set "allow_raw_tcp" parameter in "connect" method to true and it will stream everything over TCP
t.connect(ip, username='root',password='toor',port=23,timeout=5)
output1=t.execute('echo ala_is_king',timeout=5,buffer_read_timeout=2,remove_prompt_from_output=True,max_empty_buffers=3)
print(output1)
output2=t.execute('cd / && ls')
print(output2)
output3=t.execute('cd / && ls',read_until_match='expected_string_here')
print(output3)
t.close()#close the connection but keep the connection string to do reconnect later
t.enable_debug()# enable debug mode
t.disable_debug()# disable debug mode
t.reconnect()#reconnect to the host with the previous parameters
t.ping()#send new line to the host to keep the connectio open
t.destroy()#close the connection and remove the connection string totally, after this you can't do "reconnect"
t.connect('114.35.81.134',proxy_type=5,proxy_host='localhost',proxy_port=9150,proxy_username='user',proxy_password='pass')#use SOCKS5 proxy to connect, set 'proxy_type' to 4 to use SOCKS4 

The multi_session helps you in controlling multiple telnet sessions in parallel:

import xtelnet
t=xtelnet.Multi_Telnet_Session()
ip1='192.168.0.32'#just an example
ip2='192.168.0.4'
ip3='192.168.0.10'
ip4='192.168.0.11'
ip5='192.168.0.12'
host1=xtelnet.Telnet_Session.setup_host_configs(ip1, username='root',password='toor',port=23,timeout=5)
host2=xtelnet.Telnet_Session.setup_host_configs(ip2, username='root',password='toor',port=23,timeout=5)
host3=xtelnet.Telnet_Session.setup_host_configs(ip3, username='root',password='toor',port=23,timeout=5)
host4=xtelnet.Telnet_Session.setup_host_configs(ip4, username='root',password='toor',port=23,timeout=5)
host5=xtelnet.Telnet_Session.setup_host_configs(ip5, username='root',password='toor',port=23,timeout=5)
t.connect([host1,host2,host3,host4,host5])
print(t.sessions)#to see the connected hosts
c=t.all_execute('echo "ala is king"')#execute this command on all hosts
print(c)#print output
c=t.some_execute([ip1,ip2],'echo "ala is king"')#execute this command on some hosts
print(c)
c=t.host_execute(ip1,'echo "ala is king"')#execute this command on this host
print(c)
t.disconnect_host(ip1)#to disconnect of this host
t.disconnect_some([ip2,ip3])#to disconnect of those hosts
t.disconnect_all()#to disconnect of all hosts
t.destroy()#disconnect from all hosts

Usage from command line :

xtelnet host [options...]

options:

-username : set a username (required if username is needed to access) -password : set a password (required if password is needed to access) -port : (23 by default) set port -timeout : (5 by default) set timeout --add-command : a command to execute after login and disable shell --set-newline : ("\n" by default) set a new line indecator("\n" or "\r\n") --no-shell : (enabled by default if no commands are specified) disable shell after authentication --read-retries : times to retry reading the response if it takes too long --help : get this help message

examples:

xtelnet 127.0.0.1 -username root -password root --add-command "echo ala" --add-command "dir"

xtelnet 127.0.0.1 -username root -password root -port 2323 -timeout 5

xtelnet 127.0.0.1 -username root -password root -port 2323 -timeout 5 --no-shell

Xtelnet can be used to grab banners:

import xtelnet
telnet_banner=xtelnet.Socket_Connection.get_banner("localhost",port=23)#suppose you have telnet server running on that port

http_banner=xtelnet.Socket_Connection.get_banner("www.google.com",port=80,payload="GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n")#we send a http request as a payload to get the response

ssh_banner=xtelnet.Socket_Connection.get_banner("localhost",port=22)

Xtelnet can escape all ANSI characters :

import xtelnet
escaped_string=xtelnet.Socket_Connection.escape_ansi( unescaped_string )