I'm a python noob here just trying to learn a new skill so please be gentle :)
After executing the following script it appears that my script creates a telnet session and I can see the cisco devices banner but is not passing the username/password when prompted.
I've tried changing tn.read_until() and tn.read_very_eager() neither of which is triggering the script to move on to write the desired input. I've also tried forcing the username and password to be input as a byte as well as adding + '\n' to my write functions. I've also used sleep to pass a little extra time to wait for the banner to finish printing out.
tldr: when executing the script I see the username prompt, but can't get further than that.
any assistance here is welcomed.
'''
from time import sleep
import telnetlib
from telnetlib import Telnet
from getpass import getpass
Username = input('please provide your username ')
password = getpass()
# f is the .txt document that lists the IP's we'll be using.
f = open('devicess.txt')
for line in f:
print ('Configuring Device ' + (line))
device = (line)
#open connection to variable
tn = telnetlib.Telnet(device)
#For those devices in the above list, connect and run the below commands
for device in f:
tn.expect('Username: ')
tn.write(Username)
tn.expect('Password: ')
tn.write(password)
tn.write('show ver')
print('connection established to ' + device)
tn.write('logout')
output = tn.read_all()
print(output)
print('script complete')
'''
Related
I am trying to establish a serial connection via an RS232 port on the PR4000 controller from MKS. This controller is connected to a pressure gauge, and I try to read the pressure from my PC with the following script:
import time
import serial
import bitarray
ba = bitarray.bitarray()
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='COM7',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
#ser.isOpen()
print ('Enter your commands below.\r\nInsert "exit" to leave the application.')
data_in=0
while True :
data_in = input(">> ")
if data_in == 'exit':
ser.close()
break
else:
ser.write((data_in).encode('utf-8'))
out = ''
time.sleep(0.1)
while ser.inWaiting() > 0:
out = ser.read(ser.inWaiting()).decode('utf8')
if out != '':
print (out)
This code is inspired from this post :
Full examples of using pySerial package
you can find the doc of the controller here :
https://www.idealvac.com/files/manuals/PR4000_InstructionManual.pdf
The interface chapter start at page 43.
basically, the RS interface works with an requests and answers syntax.
example of answer :
RT,ON : set the remote on the controller.
?RT : ask for the state of the remote mode.
I managed to establish the connection with hyper terminal
But with python, I've tried to enter the commands and I can't have any answers, the serial buffer is empty.
Do you think the problem is in the format of the requests ?
Do you think the problem is in the format of the requests ?
A command to the device needs to be terminated with a carriage return character. Suggest you append a CR character (i.e. '\r') to data_in before it is sent.
Refer to the code that inspired your version for an example.
I am constructing a command to pass to requests library to Post an attachment - as in
files= attachment = {"attachment": ("image.png", open("C:\tmp\sensor.png", "rb"), "image/png")}
The code is working but I cannot get PyTest to test it as -is because of the open command which is executed when evaluated. Here is simplified code of the problem
import pytest
def openfile():
cmd = {"cmd": open(r"C:\tmp\sensor.png")}
return cmd
def test_openfile():
cmd = openfile()
#assert str(cmd) == str({"cmd": open(r"C:\tmp\sensor.png")}) # this works
assert cmd == {"cmd": open(r"C:\tmp\sensor.png")} # this does not
PyTest complains that the two side are different but then confirms they are the same in the diff panel!
Expected :{'cmd': <_io.TextIOWrapper name='C:\tmp\sensor.png' mode='r' encoding='cp1252'>}
Actual :{'cmd': <_io.TextIOWrapper name='C:\tmp\sensor.png' mode='r' encoding='cp1252'>}
'Click to see difference' - Opening diff panel reports 'Contents are identical'!
I can just stick with comparing the generated string with expected string but am wondering if there is a better way to do this.
Ideas?
You need to test the properties of the actual file buffer that is returned by the open call, instead of the references to that buffer, for example:
def test_openfile():
cmd = openfile()
expected_filename = r"C:\tmp\sensor.png"
assert "cmd" in cmd
file_cmd = cmd["cmd"]
assert file_cmd.name == expected_filename
with open(expected_filename) as f:
contents = f.read()
assert file_cmd.read() == contents
Note that in a test you may not have the file contents, or have them in another place like a fixture, so testing the file contents may have to be adapted, or may not be needed, depending on what you want to test.
After talking this through with a friend I think my original approach is perfectly valid. For anyone that trips over this question here's why:
I am trying to pytest building of an executable parameter to pass to another library for execution. The execution of the parameter is not relevant, just that it is correctly formatted. The test is to compare what is generated with the expected parameter ( as if I typed it) .
Therefore casting to string or json and comparing is appropriate since that is what a human does to manually check the code!
I've created my first ever python script and it works fine in the foreground, but when I run it in the background it creates, but fails to write anything to the file. I run the script with command : python -u testit.py &
Please help me understand why this happens.
#!/usr/bin/env python
import time
import datetime
dt = datetime.datetime.now()
dtLog = dt.strftime("ThermoLogs/TempLOG%Y%m%d")
f = open(dtLog,"a")
while True:
dt = datetime.datetime.now()
print('{:%Y-%m-%d %H:%M:%S}'.format(dt))
f.write('{:%Y-%m-%d %H:%M:%S}'.format(dt))
f.write('\n')
time.sleep(5)
The output will arrive in bursts (it is being buffered). With the sleep set to 0.01 I am getting bursts about every 2 seconds. So for a delay of 5 you will get them much less frequent. You may also note that it outputs all pending outputs when you terminate it.
To make the output arrive now call f.flush().
I don't see any difference between foreground and background. However it should not buffer stdout when going to a terminal.
I'm having problems saving the output of a script that applies configs to multiple devices and lets me know if a connection to a device has timed out. The script successful logs into the devices, runs the commands, and displays the output. It saves the timed out devices to .txt file, however if multiple devices experience timeouts, it deletes the line with the timed out device and only displays the name of the last device that timed out. Could someone please show me what I should add to my code, so that the names of the all the devices that the script can't connect to?
Timeouts=open("Connection time out.txt", "a")
try:
net_connect = ConnectHandler(**ios_device)
except (AuthenticationException):
print ('Authentication Failure: ' + ip_address_of_device)
continue
except (NetMikoTimeoutException):
print ('Timeout to device: ' + ip_address_of_device)
Timeouts.write(ip_address_of_device)
continue
except (EOFError):
print ('End of file whilte attempting device: ' + ip_address_of_device)
continue
except (SSHException):
print ('SSH might not be enabled: ' + ip_address_of_device)
continue
except Exception as unknown_error:
print (str(unknown_error))
continue
#with open ("Connection time out.txt") as f:
#for line in f:
#print (line),
output=net_connect.send_config_set(commands_list)
print(output)
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
I'm currently trying to write a script to read a shell via paramiko and, by far, if I prompt a single command it works great.
the problem is that my actual goal is to handle a debug flow and of course I cannot use a timeout because the script should listen without time limitations.
So the other option is to let the user stop the flow with ctrl-c. I added a "try, except" handler like
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
and it seemed like it wasn't working: nothing happened when pressing ctrl-c. But then I realized that it was just taking forever: after a few minutes the script actually stops and the message appears.
Does anyone know how to handle this problem?
just for the reference here's my code
import paramiko
import time
import re
import socket
import os
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
address = '192.168.1.1'
username = 'admin'
password = ''
ssh.connect(address, port=22, username = username, password = password)
shell = ssh.invoke_shell(height=400)
shell.settimeout(2)
def prompt_console(command = '', channel = shell, timeout = False):
print(channel.recv(9999).decode('utf-8')) #<-- this is for removing the initial prompt from buffer
if timeout == False:
shell.settimeout(3600) #<--increasing the timeout
channel.send(command+'\n')
log = ''
while True:
try:
log += channel.recv(1).decode('utf-8') #<--every loop it loads a char into the "log" variable
if log[-9:] == '--More-- ':
shell.send(" ")
log.replace('--More-- \r \r\t\t','\r\t\t')
if len(re.findall('.*\n',log))>0: #<--it yields every line to python console
yield log
log = ''
except (IOError, SystemExit):
raise
except KeyboardInterrupt:
print ("Crtl+C Pressed. Shutting down.")
I also tried to put the try-except outside the while loop but it didn't work. I also tried many suggestions that I've found on google but they didn't work either: they all stop the script eventually but for all of them it takes 5-10 minutes.