This script is supposed to open a telnet session with a remote device, send a command, and write the output to a file. The CC and CA stand for Ctrl + C and Ctrl + A. The only possible explanation that I can come up with is that the remote device is not sending the Ctrl+C at the end, but the shell output shows that it is.
Code:
#!/usr/bin/python
#Import external modules
import sys
import socket
import telnetlib
import datetime
import csv
import logging
import os
#Import internal variables and functions
from ctrlchar import CA,CC
#import csv file siteProfiles.csv
with open("/home/dev/iotank/site-profiles-staging.csv") as f:
reader = csv.reader(f, delimiter=',')
data = []
for row in reader:
data.append(row)
#Variables
TIMEOUT=30 #Telnet timeout in seconds
#251 is CSLD Report, 301 Liquid Sensor Status Report, 201 is Inventory Report
COMMAND="I201"
DEVICE="00"
DATE=datetime.date.today()
TIME=datetime.datetime.now()
TIMESTAMP=TIME.strftime("%m%d%Y%H%M")
sites = len(data)
logging.basicConfig(filename='log.txt',level=logging.DEBUG)
logger= logging.getLogger("basiclog")
REPORTNAME="TESTFI" + TIMESTAMP + ".txt"
file = open("/home/dev/iotank/report/inventory/" + REPORTNAME, 'w+')
for row in xrange(sites):
SITE = data[row][0]
HOST = data[row][1]
PORT = data[row][2]
BEGINREPORT=COMMAND + DEVICE
try:
tn = telnetlib.Telnet(HOST,PORT,TIMEOUT)
tn.set_debuglevel(1)
tn.write(CA)
tn.write(COMMAND)
tn.write(DEVICE)
tn.read_until("TEMP")
file.write("z" + SITE + "\r\n" + TIMESTAMP)
file.write(tn.read_until(CC))
tn.close
file.seek(-2, os.SEEK_END)
file.truncate()
except Exception:
logger.error(SITE + HOST + "Couldn't connect", exc_info=True)
file.close()
Output:
z080
121720190130
1 ULTRA LOW DIESEL 12175 0 2929 90.17 1.70 63.77
2 DYED LSDF 3345 0 970 65.44 0.00 63.61
3 UNLEAD 1002 0 5014 21.44 0.00 70.17
4 SUPER 1078 0 2901 30.33 1.69 70.19
^A
I20100
DEC 17, 2019 2:27 AM
US 280 FUEL CENTER
IN-TANK INVENTORY
TANK PRODUCT VOLUME TC VOLUME ULLAGE HEIGHT WATER TEMP
1 ULTRA LOW DIESEL 12175 0 2929 90.17 1.70 63.79
2 DYED LSDF 3345 0 970 65.44 0.00 63.61
3 UNLEAD 1002 0 5014 21.44 0.00 70.15
4 SUPER 1078 0 2901 30.33 1.69 70.20
The top 6 lines before the Ctrl+A character is the desired output. Why does the code continue through another iteration.
You need parentheses after the tn.close. Python isn't like Ruby or Lua, you have to explicitly call the function.
tn.close # gives the value of the function (<bound method Whatever.close of <Whatever object at 0x0000d15ea5e>>
tn.close() # calls the function
Here is a step-by-step code to debug the error I'm getting:
from nltk.tree import ParentedTree
teststr = 'a) Any service and handling fee imposed on customers by Used Motor Vehicle Dealers subject to these Rules: \r\n 1) shall be charged uniformly to all retail customers; \r\n 2) may not be presented as mandatory in writing, electronically, verbally, via American Sign Language, or via other media as mandatory; nor presented as mandatory or mandated by any entity, other than the Arkansas Used Motor Vehicle Dealer who or dealership which is legally permitted to invoice, charge and collect the service and handling fee established by these Rules; \r\n 3) must follow the procedures for disclosure set out by these Rules.'
#Using ALLENNLP's parser
from allennlp.predictors.predictor import Predictor
conspredictor = Predictor.from_path("https://s3-us-west-2.amazonaws.com/allennlp/models/elmo-constituency-parser-2018.03.14.tar.gz")
treestr = conspredictor.predict(sentence=teststr)['trees']
ptree = ParentedTree.fromstring(treestr)
Here is the error I'm receiving with the traceback:
<ipython-input-391-f600cbe3ff5e> in <module>
10 treestr = conspredictor.predict(sentence=teststr)['trees']
11
---> 12 ptree = ParentedTree.fromstring(treestr)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nltk\tree.py in fromstring(cls, s, brackets, read_node, read_leaf, node_pattern, leaf_pattern, remove_empty_top_bracketing)
616 if token[0] == open_b:
617 if len(stack) == 1 and len(stack[0][1]) > 0:
--> 618 cls._parse_error(s, match, 'end-of-string')
619 label = token[1:].lstrip()
620 if read_node is not None: label = read_node(label)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\nltk\tree.py in _parse_error(cls, s, match, expecting)
677 offset = 13
678 msg += '\n%s"%s"\n%s^' % (' '*16, s, ' '*(17+offset))
--> 679 raise ValueError(msg)
680
681 #////////////////////////////////////////////////////////////
ValueError: ParentedTree.read(): expected 'end-of-string' but got '(:'
at index 273.
"...es))))))) (: :) (S (..."
^
I have a URL DataFrame. My coding aims to use machine learning to classify the url into benign or malicious.
I wanna use Host-Based features to get the the url creation_date, last_updated_date and expired_date with whois package. But it shows error.
Could somebody help me to fix it?
Here is my code and the error as following.
# URL DataFrame
URL Lable
0 http://ovaismirza-politicalthoughts.blogspot.com/ 0
1 http://www.bluemoontea.com/ 0
2 http://www.viettiles.com/public/default/ckedit... 1
3 http://173.212.217.250/hescientiststravelled/o... 1
4 http://www.hole-in-the-wall.com/ 0
### Code
date = []
for i in range(len(df)):
item = df["URL"].loc[i]
domain = urlparse(item).netloc
cr = whois.query(domain).creation_date
up = whois.query(domain).last_updated
exp = whois.query(domain).expiration_date
if cr is not None and up is not None and exp is not None:
date.append(0)
else:
date.append(1)
### ErrorException
Traceback (most recent call last)
<ipython-input-26-0d7930e66020> in <module>
3 item = df["URL"].loc[i]
4 domain = urlparse(item).netloc
----> 5 cr = whois.query(domain).creation_date
6 up = whois.query(domain).last_updated
7 exp = whois.query(domain).expiration_date
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/whois/__init__.py in query(domain, force, cache_file, slow_down, ignore_returncode)
48
49 while 1:
---> 50 pd = do_parse(do_query(d, force, cache_file, slow_down, ignore_returncode), tld)
51 if (not pd or not pd['domain_name'][0]) and len(d) > 2: d = d[1:]
52 else: break
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/whois/_1_query.py in do_query(dl, force, cache_file, slow_down, ignore_returncode)
42 CACHE[k] = (
43 int(time.time()),
---> 44 _do_whois_query(dl, ignore_returncode),
45 )
46 if cache_file: cache_save(cache_file)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/whois/_1_query.py in _do_whois_query(dl, ignore_returncode)
59 r = p.communicate()[0]
60 r = r.decode() if PYTHON_VERSION == 3 else r
---> 61 if not ignore_returncode and p.returncode != 0: raise Exception(r)
62 return r
63
Exception: whois: connect(): Operation timed out
% IANA WHOIS server
% for more information on IANA, visit http://www.iana.org
% This query returned 1 object
refer: whois.verisign-grs.com
domain: COM
organisation: VeriSign Global Registry Services
address: 12061 Bluemont Way
address: Reston Virginia 20190
address: United States
contact: administrative
name: Registry Customer Service
organisation: VeriSign Global Registry Services
address: 12061 Bluemont Way
address: Reston Virginia 20190
address: United States
phone: +1 703 925-6999
fax-no: +1 703 948 3978
e-mail: info#verisign-grs.com
contact: technical
name: Registry Customer Service
organisation: VeriSign Global Registry Services
address: 12061 Bluemont Way
address: Reston Virginia 20190
address: United States
phone: +1 703 925-6999
fax-no: +1 703 948 3978
e-mail: info#verisign-grs.com
nserver: A.GTLD-SERVERS.NET 192.5.6.30 2001:503:a83e:0:0:0:2:30
nserver: B.GTLD-SERVERS.NET 192.33.14.30 2001:503:231d:0:0:0:2:30
nserver: C.GTLD-SERVERS.NET 192.26.92.30 2001:503:83eb:0:0:0:0:30
nserver: D.GTLD-SERVERS.NET 192.31.80.30 2001:500:856e:0:0:0:0:30
nserver: E.GTLD-SERVERS.NET 192.12.94.30 2001:502:1ca1:0:0:0:0:30
nserver: F.GTLD-SERVERS.NET 192.35.51.30 2001:503:d414:0:0:0:0:30
nserver: G.GTLD-SERVERS.NET 192.42.93.30 2001:503:eea3:0:0:0:0:30
nserver: H.GTLD-SERVERS.NET 192.54.112.30 2001:502:8cc:0:0:0:0:30
nserver: I.GTLD-SERVERS.NET 192.43.172.30 2001:503:39c1:0:0:0:0:30
nserver: J.GTLD-SERVERS.NET 192.48.79.30 2001:502:7094:0:0:0:0:30
nserver: K.GTLD-SERVERS.NET 192.52.178.30 2001:503:d2d:0:0:0:0:30
nserver: L.GTLD-SERVERS.NET 192.41.162.30 2001:500:d937:0:0:0:0:30
nserver: M.GTLD-SERVERS.NET 192.55.83.30 2001:501:b1f9:0:0:0:0:30
ds-rdata: 30909 8 2 E2D3C916F6DEEAC73294E8268FB5885044A833FC5459588F4A9184CFC41A5766
whois: whois.verisign-grs.com
status: ACTIVE
remarks: Registration information: http://www.verisigninc.com
created: 1985-01-01
changed: 2017-10-05
source: IANA
Domain Name: VIETTILES.COM
Registry Domain ID: 1827514943_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.pavietnam.vn
Registrar URL: http://www.pavietnam.vn
Updated Date: 2018-09-07T01:13:32Z
Creation Date: 2013-09-14T04:35:12Z
Registry Expiry Date: 2019-09-14T04:35:12Z
Registrar: P.A. Viet Nam Company Limited
Registrar IANA ID: 1649
Registrar Abuse Contact Email: abuse#pavietnam.vn
Registrar Abuse Contact Phone: +84.19009477
Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited
Name Server: NS1.PAVIETNAM.VN
Name Server: NS2.PAVIETNAM.VN
Name Server: NSBAK.PAVIETNAM.NET
DNSSEC: unsigned
URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/
Last update of whois database: 2018-12-25T13:33:54Z <<<
By the way, can I use other methods to get the URL creation_date, updated_date and expired_date instead of Whois in Python3?
Thanks in advance!
import BAC0
bacnet = BAC0.connect()
my_obj_list = [('file', 1),
('analogInput', 1002),
('analogInput', 1),
('analogInput', 1006),
('analogInput', 1011),
('analogInput', 1010),
('analogInput', 1001)]
# # Provide it as an argument
fx = BAC0.device('16102:19', 1610219, bacnet, object_list = my_obj_list)
p=fx.points
for point in p:
print(point)
The code is returning the point values as expected, but throwing an exception. Can not figure out what I'm doing wrong.
error
2018-11-26 17:45:51,864 - INFO | Starting BAC0 version 0.99.944 (Lite)
2018-11-26 17:45:51,908 - INFO | Using ip : 192.168.0.16
2018-11-26 17:45:51,909 - INFO | Starting app...
2018-11-26 17:45:51,910 - INFO | BAC0 started
2018-11-26 17:45:51,910 - INFO | Registered as Simple BACnet/IP App
2018-11-26 17:45:54,529 - INFO | Changing device state to DeviceDisconnected'>
2018-11-26 17:45:54,726 - INFO | Changing device state to RPDeviceConnected'>
2018-11-26 17:45:54,928 - INFO | Device 1610219:[device1610219] found... building points list
2018-11-26 17:45:57,674 - INFO | Ready!
2018-11-26 17:45:57,676 - INFO | Polling started, values read every 10 seconds
Exception in thread rpm_poll:
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:_website\BacTest\venv\lib\site-packages\BAC0\tasks\TaskManager.py", line 45, in run
self.process()
File "C:_website\BacTest\venv\lib\site-packages\BAC0\tasks\TaskManager.py", line 52, in process
self.task()
File "C:_website\BacTest\venv\lib\site-packages\BAC0\tasks\Poll.py", line 77, in task
self.device.read_multiple(list(self.device.points_name), points_per_request=25)
File "C:_website\BacTest\venv\lib\site-packages\BAC0\core\devices\mixins\read_mixin.py", line 452, in read_multiple
self.read_single(each,points_per_request=1, discover_request=discover_request)
File "C:_website\BacTest\venv\lib\site-packages\BAC0\core\devices\mixins\read_mixin.py", line 459, in read_single
return self.properties.network.read(request)
File "C:_website\BacTest\venv\lib\site-packages\BAC0\core\io\Read.py", line 87, in read
args_split, arr_index=arr_index, vendor_id=vendor_id, bacoid=bacoid))
File "C:_website\BacTest\venv\lib\site-packages\BAC0\core\io\Read.py", line 310, in build_rp_request
addr, obj_type, obj_inst, prop_id = args[:4]
ValueError: not enough values to unpack (expected 4, got 2)
device1610219/ai_2 : 2.30 noUnits
device1610219/zone_temp : 45.00 degreesFahrenheit
device1610219/ai_6 : 75.00 degreesFahrenheit
device1610219/ai_11 : 1.00 65535
device1610219/ai_10 : -53.30 degreesFahrenheit
device1610219/ai_1 : 0.00 noUnits
Process finished with exit code 0
I've made some tests trying to replicate your bug and I think you may be fighting with a weird device.
Using the exact same script I succeed reading all points.
If I may suggest though, declaring your device using default "poll" parameters will assure reading of all points every 10 seconds.
Using points will force a reading of points (one by one) when called which will slow the process. For that, I would use point.lastValue
When the device polls its point list, internally, it'll use a ReadPropertyMultiple that will read a bunch of points and properties at the same time. It is more efficient.
Something like (playing with format...) :
import BAC0
bacnet = BAC0.lite()
# # Provide it as an argument
fx = BAC0.device('2:5', 5, bacnet)
for name in fx.points_name:
if fx[name].units:
val = '{:>10.2f}'.format(fx[name].lastValue)
units = fx[name].units
else:
units = '({})'.format(fx[name].properties.units_state)
val = '{:>10}'.format(fx[name].lastValue)
print('{:<20} : {} {:<10}'.format(fx[name].properties.name, val, units))
(Extract of result)
2018-12-06 20:43:17,167 - INFO | Starting BAC0 version 0.99.944 (Lite)
2018-12-06 20:43:17,283 - INFO | Using ip : 192.168.210.11
2018-12-06 20:43:17,285 - INFO | Starting app...
2018-12-06 20:43:17,292 - INFO | BAC0 started
2018-12-06 20:43:17,292 - INFO | Registered as Simple BACnet/IP App
2018-12-06 20:43:19,295 - INFO | Changing device state to DeviceDisconnected'>
2018-12-06 20:43:20,156 - INFO | Changing device state to RPMDeviceConnected'>
2018-12-06 20:43:20,716 - INFO | Device 5:[FX14 0005] found... building points
2018-12-06 20:43:32,691 - INFO | Ready!
2018-12-06 20:43:32,696 - INFO | Polling started, values read every 10 seconds
nvoAI3 : -1.17 degreesCelsius
nvoAI4 : 42.33 percent
nvoAI6 : 354.00 kilopascals
nvoAI5 : 1.85 percent
nvoAI1 : 22.05 degreesCelsius
nvoAI2 : 20.84 degreesCelsius
[...]
nciOvrdDO5.State : 1 (['AUTO', 'ON', 'OFF'])
nvoAlarmPompe : 1 (['OFF', 'ON'])
nvoAlrmGravePompe : 1 (['OFF', 'ON'])
nvoTempOccup : 1 (['OFF', 'ON'])
nciModeOperation : 2 (['ARRET', 'AUTO', 'CHAUFF', 'REFROID', 'ELECTR'])
nciOvrdDO2.State : 1 (['AUTO', 'ON', 'OFF'])
nciOvrdDO3.State : 1 (['AUTO', 'ON', 'OFF'])
nciOvrdDO4.State : 1 (['AUTO', 'ON', 'OFF'])
nciOvrdDO1.State : 1 (['AUTO', 'ON', 'OFF'])
nciModeDeshum : 1 (['Aucune', 'Ventilation', 'Rechauff'])
nciOvrdAO1.State : 2 (['AUTO', 'MAN', '100', '0'])
nciOvrdAO2.State : 1 (['AUTO', 'MAN', '100', '0'])
nvoDI7 : inactive (('Off', 'On'))
nvoDI8 : inactive (('Off', 'On'))
nvoDI10 : inactive (('Off', 'On'))
nvoDI9 : inactive (('Off', 'On'))
nvoDI6 : inactive (('Off', 'On'))
nvoDI4 : inactive (('Off', 'On'))
If you keep getting issues, please post one here : https://github.com/ChristianTremblay/BAC0/issues
I have a table where a text column needs be updated. The column report_2_comments is the text column. When I update the column with a small string - "This is test message", I dont have any issues but when I update using the given below message, I get this error.
e == not enough arguments for format string
context = DbContext()
try:
qry = """Update Report_Table
set {2} = '{3}'
where valid_flag = 'Y' and report_status = 'C'
and report_name = '{0}'
and date(report_run_date) = '{1}';
""".format('Daily Errors Report'
, '2016-07-09', 'report_2_comments',
'8-3-2016 01:00 EST Affected DC region 1,000 errors over 2.5 hours (2%) 8-3-2016 13:00 EST Affected Virginia 500 errors over 11 hours (2%) 1233 8-3-2016 13:00 EST Affected 212/1412121001 - Date/skljld (sdlkjd)NOT_FOUND) 90,800 errors over 11 hours (2%) sldkdsdsd Fiber cut 8-3-2016 17:00 EST Affected 16703 - sdsdsd, WV (Tune Error) 15,400 errors over 7.5 hours (0.6%) sdkjd dskdjhsd sdkjhd')
print 'update qry == ', qry
output = context.execute(qry,())
except Exception as e:
print 'e == ', e
qry
Update vbo.Report_Table
set report_2_comments = '8-3-2016 01:00 EST Affected DC region 1,000 errors over 2.5 hours (2%) 8-3-2016 13:00 EST Affected Virginia 500 errors over 11 hours (2%) 1233 8-3-2016 13:00 EST Affected 212/1412121001 - Date/skljld (sdlkjd)NOT_FOUND) 90,800 errors over 11 hours (2%) sldkdsdsd Fiber cut 8-3-2016 17:00 EST Affected 16703 - sdsdsd, WV (Tune Error) 15,400 errors over 7.5 hours (0.6%) sdkjd dskdjhsd sdkjhd'
where valid_flag = 'Y' and report_status = 'C'
and report_name = 'Daily Errors Report'
and date(report_run_date) = '2016-07-09';
Table definition.
CREATE TABLE Report_Table (
id bigint(19) NOT NULL auto_increment,
report_name varchar(200),
report_run_date datetime,
report_status char(25),
valid_flag char(1),
report_1_comments text(65535),
report_2_comments text(65535),
report_3_comments text(65535),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;