simple telnet module
Project description
xtelnet
This is an easy to use telnet module to interact with a remote system smoothly over this protocol!
Why should I use xtelnet?
- Easy to use and stable
- Simple Authentication mechanism
- 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
Install :
pip install xtelnet
or
pip3 install xtelnet
Usage on a script :
import xtelnet t=xtelnet.session() ip='192.168.0.32'#just an example t.connect(ip, username='root',password='toor',p=23,timeout=5) output1=t.execute('echo ala_is_king') print(output1) output2=t.execute('cd / && ls') print(output2) t.cwd('/')#change working directory t.switch_terminal('tclsh')#change terminal type where the prompt will get changed as well (just and example of command to do it on some routers) t.switch_terminal('tclquit') t.close()#close the connection but keep the connection string to do reconnect later 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"
To start a manual interactive session after login, just do:
import xtelnet t=xtelnet.session() ip='192.168.0.32'#just an example t.connect(ip, username='root',password='toor',p=23,timeout=5) t.interact()
The multi_session helps you in controlling multiple telnet sessions in parallel:
import xtelnet t=xtelnet.multi_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.dict_host(ip1, username='root',password='toor',p=23,timeout=5) host2=xtelnet.dict_host(ip2, username='root',password='toor',p=23,timeout=5) host3=xtelnet.dict_host(ip3, username='root',password='toor',p=23,timeout=5) host4=xtelnet.dict_host(ip4, username='root',password='toor',p=23,timeout=5) host5=xtelnet.dict_host(ip5, username='root',password='toor',p=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.get_banner("localhost",p=23)#suppose you have telnet server running on that porthttp_banner=xtelnet.get_banner("www.google.com",p=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.get_banner("localhost",p=22)
Xtelnet can escape all ANSI characters :
import xtelnet escaped_string=xtelnet.escape_ansi( unescaped_string )
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.