python code for making telnet connection to NSK machine - python

I am not able to do telnet connection to my server. I am able to do it manually but through program it is not happening.
Below is the code from cmd which works fine -
C:\Users\Administrator>telnet <host>
WELCOME TO BRAMHA [PORT $ZTC0 #23 WINDOW $ZTN0.#PTUEAKH]
TELSERV - T9553J01 - (25JUN2009)
Available Services:
TACL EXIT
Enter Choice> TACL
TACL 1> logon super.super
Password:
Last Logon: 01 MAR 2022, 00:34
1> exit
Are you sure you want to stop this TACL (\BRAMHA.$X7A2)?yes
Connection to host lost.
C:\Users\Administrator>
I tried below Python code-
import os,re,telnetlib
host = "abc.com"
tn = telnetlib.Telnet(host)
tn.read_until(b"Enter Choice>", timeout=10) # <- add this line
# tn.read_until(b"> ", timeout=10) # if prompt has space behind
#tn.write(b"exit\n")
tn.write(b"tacl\n")
tn.read_until(b"TACL 1>", timeout=10)
tn.write(b"logon super.super\n")
tn.read_until(b"Password:", timeout=10)
tn.write(b"123abc\n")
tn.read_until(b"1>", timeout=10)
tn.write(b"exit\n")
tn.read_until(b"*?", timeout=10)
tn.write(b"yes\n")
x = tn.read_all()
print (x)
but while executing it is simply waiting and nothing is happening.

Related

Python: How can I capture the boot logs through SSH on a remote Linux machine?

Objective: I want to write a Python test script to execute a reboot command through SSH on a remote Linux machine, capture the boot logs and check if the boot is successful.
Problem: I am able to send a reboot command using Paramiko and the machine reboots as expected, but I couldn't capture the boot logs and print them out. My code also seems to run without waiting for the boot process to finish.
Here is part of my code:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(host, username=username, password=password)
except paramiko.SSHException as e:
ssh.get_transport().auth_none(username) # without password
# execute reboot and capture the boot logs
stdin, stdout, stderr = ssh.exec_command("/sbin/reboot")
print(stdout.readlines())
print(stderr.readlines())
# check if reboot is done
exit_status = ''
msg = ''
while True:
if stdout_channel.exit_status_ready():
exit_status = ssh.stdout.channel.recv_exit_status()
print("Exit status: %s" % exit_status)
break
time.sleep(10)
ssh.close()
if exit_status == 0:
print("Reboot successful")
else:
print(Reboot not successful")
Logs are not captured and the following output is printed out before the machine finishing rebooting:
[]
[]
Exit status: 0
Reboot successful
Questions:
a) How can I capture the boot logs?
b) How to properly check for status after boot process is completed? Alternatively, I think I can ssh again and simply run a command after waiting some time for it to reboot.
you need /var/log/boot.log or /var/log/dmesg
You need monitoring. Generally, for LAN you need something like zabbix, but it is to much for 2 hosts. Other way is any script for sending message from remote host to your log server or messenger(using messenger API) or vice versa. For your script you can make systemd Unit service for run it after/before anything.

Exit out of a console connection in Python

I am working on some python code to log into a whitebox device that houses a virtual application (VTA). There will be two different VTAs installed and the code will log into the physical device, then log into the VTA using a virsh console (Name of VTA).
The issue I am running into is exiting one VTA and then virsh console into another. The exit command simply brings me to a login prompt again but will not exit out of the console connection.
In order to do so, I must send a "control + ]" in order to break out of the console. I have been searching online to try and find a solution but the only option I have found is to send and "exit" followed by "\x1b". However, This does not actually break out of the console window. Rather, it ends the session which is not what I am looking for.
Is there a way to send a "Control + ]" in python?
Here is some of the code showing the steps:
from paramiko import SSHClient, AutoAddPolicy
import time
import re
import os
import sys
import progressbar
import stat
def create_connection(host):
username = ''
password = ''
port = 22
connection_info = {
'port': port,
'username': username,
'password': password
}
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(host, timeout=10, auth_timeout=10, **connection_info)
ssh_session = client.invoke_shell()
ssh_session.settimeout(10.0)
return ssh_session
def send_commands(ssh_session, commands, sleep_time=1):
for command in commands:
ssh_session.send(command)
time.sleep(sleep_time)
output = ssh_session.recv(1048576)
decoded_output = output.decode()
return decoded_output
console_commands = [
'virsh console vw-vta\n',
'\n',
'\n', # Place the username of the VTA here
'\n' # Place the password of the VTA here
]
show_mgmt_commands = [
'ifconfig ens2 | grep Bcast\n'
]
exit_console = [
'exit\n'
'\x1b'
]
validate_commands = [
'virsh list\n'
]
def validation():
host = input('What is the IP address? ')
print('\n')
print(f'\033[1;33m--< Checking {host} for a valid VTA >------------\033[0m')
try:
ssh_session = create_connection(host)
except Exception as l:
print(f"\033[1;31mCannot connect to {host}!\033[0m")
return
validate = send_commands(ssh_session, validate_commands)
if 'y1564' in validate:
print(f"\033[1;32mThere is a Y1564 VTA running! Obtaining information...\033[0m")
ssh_session = create_connection(host)
console = send_commands(ssh_session, console_commands)
try:
show_mgmt = send_commands(ssh_session, show_mgmt_commands, sleep_time=2)
except Exception as e:
print(f"\033[1;31mCouldn't reach the console on " f"\033[1;33m{host}\033[0m"f"\033[1;31m. This VTA will need to be rebuilt.\033[0m")
if 'Login incorrect' in show_mgmt:
print(f"\033[1;31m--< Begin ERROR MESSAGE >------------\033[0m")
print(show_mgmt)
print(f"\033[1;31m--< End ERROR MESSAGE >------------\033[0m")
print(f"\033[1;31mThis VTA has the incorrect password!\033[0m")
print(f'{host},VTA Password Error', file=f)
exit = send_commands(ssh_session, exit_console)
return
else:
try:
mgmt = show_mgmt.split('addr:')[1].split(" ")[0]
except Exception as g:
print(f"\033[1;31mThis VTA is corrupt and will need to be rebuilt!\033[0m")
exit = send_commands(ssh_session, exit_console)
return
print("Y1564 VTA IP: ", mgmt)
exit = send_commands(ssh_session, exit_console)
else:
print("\033[1;33mThere is NOT a Y1564 VTA running on \033[0m"f"\033[1;34m {host}\033[0m")
ssh_session.close()
if __name__ == '__main__':
full_check()
When this function finishes, the second part of the code calls a similar function. However it fails because the previous function did not break out of the console connection. The code attempts to send the commands for the next function while it is still inside the previous VTA.
Here is an output showing what it is doing:
What is the IP address? 1.1.1.1
--< Checking 1.1.1.1 for a valid VTA >------------
There is a Y1564 VTA running! Obtaining information...
Y1564 VTA IP: 10.10.10.10
exit
logout
--< Checking 1.1.1.1 for a valid VTA >------------
Ubuntu 16.04.6 LTS y1564 ttyS0
y1564 login:
virsh list
Password:
There is NOT a VTA running on 1.1.1.1
The output above shows that when the exit command is run and followed by the \x1b, it does not properly exit out but attempts to send the "virsh list" command from the next part.
The hex character you're using to send the ctrl+] is wrong. Update your exit console commands to this:
exit_console = [ 'exit\n', '\x01D' ]
ASCII Reference: http://www.physics.udel.edu/~watson/scen103/ascii.html
It also looks like you're using the invoke_shell() method, you can use the close() method to exit out of that particular shell and establish a new one as needed.
Paramiko reference: http://docs.paramiko.org/en/stable/api/channel.html

How to connect to windows 2012 machine from Centos using Python3

My requirement is ability to run a PowerShell script on a Windows 2012 server remotely, this has to be triggered from a Linux server using Python script.
Need suggestions on best way to handle this and also sample code (if possible).
Below are the steps I intend to achieve but i see it's not working as expected.
PowerShell scripts to be executed are already placed in Windows server (2012).
Python3 program running on Linux (CentOS) does SSH to Windows server (2012) using netmiko module.
sends the command (PowerShell command to execute script in remote Windows server) over the SSH connection.
I was able to connect to the remote Windows server using Python. But I don't see this method working as expected.
Need an effective and efficient way to achieve this.
from netmiko import ConnectHandler
device = ConnectHandler(device_type="terminal_server",
ip="X.X.X.x",
username="username",
password="password")
hostname = device.find_prompt()
output = device.send_command("ipconfig")
print (hostname)
print (output)
device.disconnect()
Nothing much is done for 'terminal_server" device type. You have to do manual passing at the moment.
Below is extracted from COMMON_ISSUES.md
Does Netmiko support connecting via a terminal server?
There is a 'terminal_server' device_type that basically does nothing post SSH connect. This means you have to manually handle the interaction with the terminal server to connect to the end device. After you are fully connected to the end network device, you can then 'redispatch' and Netmiko will behave normally
from __future__ import unicode_literals, print_function
import time
from netmiko import ConnectHandler, redispatch
net_connect = ConnectHandler(
device_type='terminal_server', # Notice 'terminal_server' here
ip='10.10.10.10',
username='admin',
password='admin123',
secret='secret123')
# Manually handle interaction in the Terminal Server
# (fictional example, but hopefully you see the pattern)
# Send Enter a Couple of Times
net_connect.write_channel("\r\n")
time.sleep(1)
net_connect.write_channel("\r\n")
time.sleep(1)
output = net_connect.read_channel()
print(output) # Should hopefully see the terminal server prompt
# Login to end device from terminal server
net_connect.write_channel("connect 1\r\n")
time.sleep(1)
# Manually handle the Username and Password
max_loops = 10
i = 1
while i <= max_loops:
output = net_connect.read_channel()
if 'Username' in output:
net_connect.write_channel(net_connect.username + '\r\n')
time.sleep(1)
output = net_connect.read_channel()
# Search for password pattern / send password
if 'Password' in output:
net_connect.write_channel(net_connect.password + '\r\n')
time.sleep(.5)
output = net_connect.read_channel()
# Did we successfully login
if '>' in output or '#' in output:
break
net_connect.write_channel('\r\n')
time.sleep(.5)
i += 1
# We are now logged into the end device
# Dynamically reset the class back to the proper Netmiko class
redispatch(net_connect, device_type='cisco_ios')
# Now just do your normal Netmiko operations
new_output = net_connect.send_command("show ip int brief")

SSH and telnet to localhost using python

I am new to python and if the question is very nooby i apologize. Simply put i need to write a script that connects to remote via ssh then telnets to localhost and execute a command on a shell there.
I am using Python 2.4.3. I have read alot of similar questions here , and alot of people suggest to use modules such as Paramiko, Pexpect etc. However this is out of question - i am supposed to use only "native" 2.4.3 libraries. I have tried messing around with subprocess module and i have managed to connect to remote shell (however i need to provide a password - and i would like to avoid that by providing a password in script for example) - but still i need to do a telnet to localhost and execute few commands on a different shell.
Could someone be so kind and give me some hints? Thanks in advance.
TL;DR I am looking for python alternative to this bash command :
./sshpass -p password ssh username#$ip -t "(sleep 1;echo "command" ; sleep 1) | telnet localhost $port;exit;bash" >> testing.txt
after a simple search:
telnet:
link
import getpass
import sys
import telnetlib
HOST = "hostname"
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()
ssh:
link
import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = raw_input('hostname: ')
username = raw_input('username: ')
password = getpass.getpass('password: ')
s.login (hostname, username, password)
s.sendline ('uptime') # run a command
s.prompt() # match the prompt
print s.before # print everything before the prompt.
s.sendline ('ls -l')
s.prompt()
print s.before
s.sendline ('df')
s.prompt()
print s.before
s.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print str(e)

Python Error Handling & Connection Errors

I'm working on a Telnet client. I started coding on my notebook(Windows) and at the finish I uploaded it on my server(Debian). Both systems works with Python 3. At my notebook the script works well, but on Debian, it does make errors.
The Code:
import telnetlib
import sys
try:
HOST = sys.argv[1]
user = sys.argv[3]
password = sys.argv[4]
cmd= sys.argv[5]
port=int(sys.argv[2])
tn = telnetlib.Telnet(HOST, port)
tn.read_until(b"username: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(cmd.encode('ascii') + b"\n")
except ConnectionRefusedError:
print('ERROR')
else:
print('OK')
Server(CraftBukkit server with RemoteToolKit):
Mar 05, 2014 12:39:58 PM net.wimpi.telnetd.net.ConnectionManager makeConnection
INFO: connection #1 made.
Unexpected error in shell!
java.net.SocketException: Connection reset
> at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118)
> at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
> at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
> at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
> at java.io.DataOutputStream.flush(DataOutputStream.java:123)
> at net.wimpi.telnetd.io.TelnetIO.flush(Unknown Source)
> at net.wimpi.telnetd.io.TerminalIO.flush(Unknown Source)
> at net.wimpi.telnetd.io.TerminalIO.write(Unknown Source)
> at com.drdanick.McRKit.Telnet.ConsoleShell.run(ConsoleShell.java:78)
> at net.wimpi.telnetd.net.Connection.run(Unknown Source)
Mar 05, 2014 12:39:58 PM net.wimpi.telnetd.net.ConnectionManager cleanupClosed
INFO: cleanupClosed():: Removing closed connection Thread[Connection1,5,]
Greets miny
EDIT: The error handling works now! THX # Wojciech Walczak
The client doesn't report errors, but the server reports errors. If I run the same code on Windows, it doesn't make errors.
...and are you sure you're using Python 3.3 or later? ConnectionRefusedError has been added in Python 3.3.
EDIT:
Given that your client works fine when launched from your laptop, and is catching ConnectionRefusedError on another machine, I would say that the problem is not the script itself. It's rather about server's telnet/firewall settings. Are other telnet clients working in the environment in which your script is failing?
The reason for the traceback when the server is offline is because you are trying to trap a non-existent exception (which is to say that the name ConnectionRefusedError has not yet been assigned a value).
Solely for its educational purpose I would remove the `try ... except ..." and let the error be raised Then hopefully you will find out exactly what exception is being raised.
As to the Java traceback, WTF?
The error is in the script. The telnet command on Linux works well.
I ran the code w/o try and except and it doesn't report errors.
I made some tests with byte strings.
If I run this code, the machines displays different strings.
Command:
print(b"Hello there!")
Windows:
b"Hello there!"
Linux:
Hello there!
Updated Code at Debian (Windows uses still the old code):
import telnetlib
import sys
if(True):
HOST = sys.argv[1]
user = sys.argv[3]
password = sys.argv[4]
cmd= sys.argv[5]
port=int(sys.argv[2])
tn = telnetlib.Telnet(HOST, port)
tn.read_until(b"username:")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"password:")
tn.write(password.encode('ascii') + b"\n")
tn.write(cmd.encode('ascii') + b"\n")
I tested the code at Windows and the telnet server can run the commands.
When I run the code at Debian, it doesn't say nothing, but the server says *Connection reset".

Categories

Resources