Code Monkey home page Code Monkey logo

xtelnet's Introduction

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 )

xtelnet's People

Contributors

alabouali avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

xtelnet's Issues

Connecting to non telnet servers

Ciao Ala,

I really like the simple approach your library takes in managing "telnet" servers.

I wanted to use it to connect to my energy monitoring device, that doesn't have a proper interface to pull data out, but its official mobile app pulls data out of it via telnet on port 5001. So it actually has a raw terminal listening on port 5001 and it answers to some commands, and it returns data using "ready..." as end of response.

I can use standard telnet in linux/windows manually, it works as expected. Now I wanted to write a small python script to do it automatically.

The problem is that when I use your library, I receive the error message "An error occurred: Not a telnet service".

I think it probably recognizes there are no control strings etc, so it refuses to operate.

Is there a way to skip that check? Maybe an option, so I can make it work also with non-telnet servers, but "stupid" socket servers like this.

This is the code I'm trying:

async def main():
    host = "elios4u.axel.dom"  # IP or hostname
    port = 5001  # elios4You default port

    try:
        telnet = xtelnet.session()
        telnet.connect(u=host, p=port, timeout=5, debug_level=1)
        resp = telnet.execute("@DAT")
        print(resp)
        telnet.close()
        telnet.destroy()

xtelnet dependency on telnetlib. DeprecationWarning: 'telnetlib' is deprecated and slated for removal in Python 3.13

At a first glance xtelnet looked like exactly what i needed. So i started a test project with it to see if it can be used in production and test how it`s handling multi threading.

After looking under the hood, i notice that it`s making use of telnetlib. :(
As you (probably) already know, telnetlib will be removed in Python 3.13.

See PEP 594.

If we want xtelnet to have a long life, maybe start moving it to telnetlib3 or Exscript.

slow performance when sending commands

hello Ala,
thank you for this handy tool..

unfortunately it takes forever to perform this litte task below.
the function enteras a path and change the chmod +x for one file at the time.
the methode is called in a for loop.
I tried another methode and keep the connection open, the same result.. takes approx. 20 seconds to perform the directory change and the chmod command.

any ideas how to speed this up?

2021-05-06 19:15:41,062;DEBUG;before connection
2021-05-06 19:15:43,121;DEBUG;open telnet
2021-05-06 19:15:43,121;DEBUG;in for
2021-05-06 19:15:43,121;DEBUG;in if
2021-05-06 19:16:01,431;DEBUG;change chmod - 
2021-05-06 19:16:01,431;DEBUG;in for
2021-05-06 19:16:01,431;DEBUG;in if
2021-05-06 19:16:19,723;DEBUG;change chmod - 
2021-05-06 19:16:19,723;DEBUG;in for
2021-05-06 19:16:19,723;DEBUG;in if
2021-05-06 19:16:37,989;DEBUG;change chmod - 
2021-05-06 19:16:37,989;DEBUG;close telnet
2021-05-06 19:16:38,174;DEBUG;before connection
2021-05-06 19:16:40,199;DEBUG;open telnet
2021-05-06 19:16:40,199;DEBUG;in for
2021-05-06 19:16:40,199;DEBUG;in if
2021-05-06 19:16:58,547;DEBUG;change chmod - 
2021-05-06 19:16:58,547;DEBUG;in for
2021-05-06 19:16:58,547;DEBUG;in if
2021-05-06 19:17:16,854;DEBUG;change chmod - 
2021-05-06 19:17:16,854;DEBUG;in for
2021-05-06 19:17:16,854;DEBUG;in if
2021-05-06 19:17:35,148;DEBUG;change chmod - 
2021-05-06 19:17:35,149;DEBUG;close telnet
#Files in list for upload and chmod
files2upload = [['/usr/local/s/','AddIn','x'],['/usr/local/bin/','startup.sh','x'],['/usr/local/s/','xml_S64','x']]

#call methode
set_permission_list(files2upload)

#methode
def set_permission_list(self, list):
		cmd = "chmod +x "
		try:
			t=xtelnet.session()
			logging.debug("before connection")
			t.connect(self.ip, username=self.__TelnetUser,password=self.__TelnetPass,p=23,timeout=2)
			logging.debug("open telnet")
			for file in list:
				logging.debug("in for")
				if file[2] == 'x':
					logging.debug("in if")
					output1=t.execute('cd ' + file[0])
					output1=t.execute(cmd + file[1])
					logging.debug("change chmod - %s",output1)
			t.destroy()
			logging.debug("close telnet")
		except Exception as e:
			logging.error("chmod +x %s mit code %s",file[1],str(e))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.