Package: xtelnet
xtelnetThis 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 stableSimple Authentication mechanismCompatible with almost all servers when it comes to authentication and executing the commandsAvailable Command line toolThread-safe: if the session is shared among threads to execute commands, the commands will be executed one by oneSupports running multiple sessions concurrentlyCan connect simultaneously and run in parallel the same command on: single or some or all connected hostsAllow reconnect after closing the connectionAllow escape ANSI charactersGrab bannersAvailable "ping" function to use if you want to keep the connection openSupports SOCKS 4 / 5 proxiesSupports SSLInstall :pip install xtelnetorpip3 install xtelnetUsage on a script :importxtelnett=xtelnet.session()
ip='192.168.0.32'#just an examplet.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 directoryt.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 latert.reconnect()#reconnect to the host with the previous parameterst.ping()#send new line to the host to keep the connectio opent.destroy()#close the connection and remove the connection string totally, after this you can't do "reconnect"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 SOCKS4To start a manual interactive session after login, just do:importxtelnett=xtelnet.session()
ip='192.168.0.32'#just an examplet.connect(ip, username='root',password='toor',p=23,timeout=5)
t.interact()The multi_session helps you in controlling multiple telnet sessions in parallel:importxtelnett=xtelnet.multi_session()
ip1='192.168.0.32'#just an exampleip2='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 hostsc=t.all_execute('echo "ala is king"')#execute this command on all hostsprint(c)#print outputc=t.some_execute([ip1,ip2],'echo "ala is king"')#execute this command on some hostsprint(c)
c=t.host_execute(ip1,'echo "ala is king"')#execute this command on this hostprint(c)
t.disconnect_host(ip1)#to disconnect of this hostt.disconnect_some([ip2,ip3])#to disconnect of those hostst.disconnect_all()#to disconnect of all hostst.destroy()#disconnect from all hostsUsage 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 messageexamples: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 5xtelnet 127.0.0.1 -username root -password root -port 2323 -timeout 5 --no-shellXtelnet can be used to grab banners:importxtelnettelnet_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 responsessh_banner=xtelnet.get_banner("localhost",p=22)Xtelnet can escape all ANSI characters :importxtelnetescaped_string=xtelnet.escape_ansi( unescaped_string )