OSError: [Errno 22] Invalid argument (Paramiko) - python

I ma using Paramiko to do a sftp to transfer file from Linux environment to Windows.
I tried different solutions on Stack Overflow but still having the same problem.
My script
localpath = os.path.join(os.sep, 'Users', 'me', 'Desktop', 'ELK', 'PM_XML')
serverpath = r"***/****/***"
def sftp():
ip=ipAddr.get()
while True:
current_time = time.time()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username="root",password="root")
sftp = ssh.open_sftp()
for element in sftp.listdir(serverpath):
if element.endswith(".xml"):
creation_time = sftp.stat(serverpath+element).st_mtime
if (current_time+3400 - creation_time) / (3600) <= 1:
sftp.get(serverpath+element,os.path.join(os.sep,localpath,element))
sftp.close()
ssh.close()
I am getting this error:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\paramiko\sftp_client.py", line 801, in g
et
with open(localpath, "wb") as fl:
OSError: [Errno 22] Invalid argument: '\\Users\\me\\Desktop\\ELK\\PM_XML\\A2018-10-18T11:03:00+02:00-2018-10-18T11:04:00
+02:00_user-67-0-test-vm2.lk.fr.xml'
I think the problem is due to the file name
A2018-10-18T11:03:00+02:00-2018-10-18T11:04:00 +02:00_user-67-0-test-vm2.lk.fr.xml'
because when I try to do that with a simple filename my script works fine.
Any suggestions to deal with this filename, because I would like to keep the same name used on the server.
Solved by Martin Prikryl
a suggestion replace colon ":" with "_"
element.replace(":","_")

On Windows, a filename cannot contain a colon (:), among other special characters.
Microsoft documentation on Naming Conventions:
Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
...
: (colon)
...
There's nothing you can do about it, except for removing/replacing the colon from the filename.

Related

Why is DNSPython zone transfer sporadically failing

So, I'm still at the noob level when it comes to python. I know... I know... there's probably a more efficient way to do what I'm trying but still learning and hopefully, I'll get better with practice.
For a training project, I'm writing a script to do various DNS operations against a domain. I found DNSPython and it seemed to be exactly what I needed to use and I thought I was done with it but when I tried it against a different domain it keeps failing at the zone transfer.
I've got two domains hardcoded right now for testing. The megacorpone domain iw was working as I expected however, now it's failing (with no code change) in order to get it to work I had to filter the first record '#' that was returned otherwise it failed as well.
However, the zonetransfer.me domain sometimes completes the script with error but fails errors sporadically as well, but it never displays the host records for some reason and I've not been able to figure out how to fix it yet, been banging my head against it for a while now.
The megacoprone run was working every time earlier now it's not working at all. The only thing I can think of so far is that it may be a timing issue.
Run with megacoprpone
Attempting zone transfers for megacorpone.com
Traceback (most recent call last):
File "/home/kali/Exercises/Module_7/dns-axfer.py", line 56, in zoneXFR
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
File "/usr/lib/python3/dist-packages/dns/zone.py", line 1106, in from_xfr
for r in xfr:
File "/usr/lib/python3/dist-packages/dns/query.py", line 627, in xfr
raise TransferError(rcode)
dns.query.TransferError: Zone transfer error: REFUSED
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/kali/Exercises/Module_7/dns-axfer.py", line 73, in <module>
zoneXFR()
File "/home/kali/Exercises/Module_7/dns-axfer.py", line 66, in zoneXFR
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
UnboundLocalError: local variable 'zone' referenced before assignment
Run 1 with zonetransfer.me
Attempting zone transfers for zonetransfer.me
Results for nsztm1.digi.ninja.
Zone origin: zonetransfer.me
---------------------------------------------------------------------------
Results for nsztm1.digi.ninja.
Zone origin: zonetransfer.me
---------------------------------------------------------------------------
[*] Error: <class 'dns.resolver.NoAnswer'> The DNS response does not contain an answer to the question: _acme-challenge.zonetransfer.me. IN A
Results for nsztm2.digi.ninja.
Zone origin: zonetransfer.me
---------------------------------------------------------------------------
Results for nsztm2.digi.ninja.
Zone origin: zonetransfer.me
---------------------------------------------------------------------------
[*] Error: <class 'dns.resolver.NoAnswer'> The DNS response does not contain an answer to the question: _acme-challenge.zonetransfer.me. IN A
Run 2 with no code change (zonetransfer.me)
Attempting zone transfers for zonetransfer.me
Traceback (most recent call last):
File "/home/kali/Exercises/Module_7/dns-axfer.py", line 56, in zoneXFR
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
File "/usr/lib/python3/dist-packages/dns/zone.py", line 1106, in from_xfr
for r in xfr:
File "/usr/lib/python3/dist-packages/dns/query.py", line 596, in xfr
_net_write(s, tcpmsg, expiration)
File "/usr/lib/python3/dist-packages/dns/query.py", line 364, in _net_write
current += sock.send(data[current:])
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/kali/Exercises/Module_7/dns-axfer.py", line 73, in <module>
zoneXFR()
File "/home/kali/Exercises/Module_7/dns-axfer.py", line 66, in zoneXFR
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
UnboundLocalError: local variable 'zone' referenced before assignment
My script: bash away... I can always take constructive criticism.
#!/usr/bin/python3
import sys, argparse
import dns.query
import dns.zone
import dns.resolver
from colorama import Fore, Style
bracket = f"{Fore.BLUE}[{Fore.GREEN}*{Fore.BLUE}]{Style.RESET_ALL} "
bracket_err = f"{Fore.BLUE}[{Fore.RED}*{Fore.BLUE}]{Style.RESET_ALL} "
'''
parser = argparse.ArgumentParser()
parser.add_argument('domain')
args = parser.parse_args()
'''
# domain = (sys.argv[1])
domain = 'megacorpone.com'
#domain = 'zonetransfer.me'
def line():
print ('-' * 75)
return None
def resolveDNS(system):
resolver = dns.resolver.Resolver()
results = resolver.query(system , "A")
return results
def getNS ():
name_servers = dns.resolver.query(domain, 'NS')
print ("\nThe name servers for " + domain + " are:")
line()
for system in name_servers:
A_records = resolveDNS(str(system))
for item in A_records:
answer = ','.join([str(item)])
print (bracket, "{:30}".format(str(system).rstrip('.')), "{:15}".format(answer))
return name_servers
def getMX():
mail_server = dns.resolver.query(domain, 'MX')
print("\nMail servers for", domain)
line()
for system in mail_server:
A_records = resolveDNS(str(system.exchange))
for item in A_records:
answer = ','.join([str(item)])
print(bracket, "{:30}".format(str(system.exchange).rstrip('.')), "{:15}".format(str(answer)), '\t', "{:5}".format("Preference:"), str(system.preference))
return None
def zoneXFR():
print ("\nAttempting zone transfers for", domain,)
for server in name_servers:
try:
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
line()
for host in zone:
if str(host) != '#':
A_records = resolveDNS(str(host) + "." + domain)
for item in A_records:
answer = ','.join([str(item)])
print(bracket, "{:30}".format(str(host) + "." + domain), answer)
except Exception as e:
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
line()
print (bracket_err, f"{Fore.RED}Error:{Style.RESET_ALL}", e.__class__, e)
name_servers = getNS()
getMX()
zoneXFR()
print("\n")
I see that you are trying well-known name servers that are specifically set up for testing. However, for the benefit of other readers, I will add a couple explanations.
As you are probably aware, most name servers will not allow zone transfers nowadays. That being said, it is possible that each of the name servers for a given domain name will behave differently (they could have different configurations and even be running different software).
In the case of megacorpone.com there are 3 name servers listed:
ns2.megacorpone.com.
ns3.megacorpone.com.
ns1.megacorpone.com.
ns2.megacorpone.com is the only one that did allow a zone transfer.
This message
dns.query.TransferError: Zone transfer error: REFUSED
means what it means: your query was refused. Probably you talked to the wrong name server.
Then you have another error which suggest a variable scoping issue:
UnboundLocalError: local variable 'zone' referenced before assignment
You are calling functions in this order:
name_servers = getNS()
getMX()
zoneXFR()
If name_servers fails, then the subsequent call to zoneXFR will fail too. Because this code:
for server in name_servers:
will try to iterate over an empty list.
Intermittent DNS resolution failures are common so a few checks are required here. At the very least, make sure that the list of NS is not empty.
Another issue: you start a for loop outside of the try block so your control structure is broken right in the middle:
for server in name_servers:
try:
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
line()
Do this instead:
try:
for server in name_servers:
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
...
I suspect that your script fails intermittently because the list of name servers is not always returned in the same order. If the first NS returned is ns1.megacorpone.com. or ns3.megacorpone.com. then the code crashes. If the scripts starts with ns2.megacorpone.com (the sole NS allowing zone transfers) then it seems to work OK.
When this code fails (AXFR denied):
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
then zone is not defined, and that's why you cannot print it in your exception block. Instead, show the domain name or some other variables that you know are defined and valid.
So if the AXFR is denied, your script should handle this exception dns.query.TransferError, and quietly move on to the next NS if any, until the list has been exhausted.
Another bit of advice: you try to resolve resources names that are different than '#'. Instead, look at the record type. You should only resolve CNAME, MX or NS. The other common types are TXT, A, AAAA, SOA. The rest are more exotic such as NAPTR, LOC or SRV. Nothing that should be resolved I think.
Fixed up your code, doesn't look great yet, but it works
#!/usr/bin/python3
# you might want to run python3 -m pip install dnspython before running this script
import sys
import dns.query
import dns.zone
import dns.resolver
# formatting setup
from colorama import Fore, Style
bracket = f"{Fore.BLUE}[{Fore.GREEN}*{Fore.BLUE}]{Style.RESET_ALL} "
bracket_err = f"{Fore.BLUE}[{Fore.RED}*{Fore.BLUE}]{Style.RESET_ALL} "
def drawLine():
print ('-' * 75)
# read arguments
try:
domain = (sys.argv[1])
except:
print("[!] USAGE: python3 zt.py DOMAIN_NAME")
sys.exit(0)
# DNS functions
def resolveDNS(name):
resolver = dns.resolver.Resolver()
results = resolver.query(name , "A")
return results
def getNS (domain):
mapping = {}
name_servers = dns.resolver.query(domain, 'NS')
print ("\nThe name servers for " + domain + " are:")
drawLine()
for name_server in name_servers:
A_records = resolveDNS(str(name_server))
for item in A_records:
answer = ','.join([str(item)])
mapping[str(name_server)] = answer
print (bracket, "{:30}".format(str(name_server).rstrip('.')), "{:15}".format(answer))
return mapping
def zoneXFR(server):
try:
zone = dns.zone.from_xfr(dns.query.xfr(str(server).rstrip('.'), domain))
except Exception as e:
print (bracket_err, f"{Fore.RED}Error:{Style.RESET_ALL}", e.__class__, e)
else:
print ("\nResults for",server, "\nZone origin:", str(zone.origin).rstrip('.'))
drawLine()
for host in zone:
if str(host) != '#':
A_records = resolveDNS(str(host) + "." + domain)
for item in A_records:
answer = ','.join([str(item)])
print(bracket, "{:30}".format(str(host) + "." + domain), answer)
drawLine()
name_servers = getNS(domain)
for server in name_servers:
print ("\nAttempting zone transfers for", server,name_servers[server])
zoneXFR(name_servers[server])

'No such file or directory' ERROR information when using "PRINT"

When I 'print' some hex string, some interesting error information in python, I wonder why this error is caused.
Win10(I tried it on ubuntu, No ERROR),python 2.7
enc_hex = '''f982f01c'''
enc_ascii = enc_hex.decode('hex')
print(enc_ascii)
Traceback (most recent call last):
File ".\xxxx.py", line 7, in <module>
print(enc_ascii)
IOError: [Errno 2] No such file or directory
Well,in fact I want to know why "print" a special set of hex will cause file operation, other hex string will not error
Try using codecs.decode:
import codecs
enc_hex = '''f982f01c'''
enc_ascii = codecs.decode(enc_hex, 'hex')
print(enc_ascii)
Output:
b'\xf9\x82\xf0\x1c'
it seems like directory problems . in windows you have to use forward slash (/) while accessing the directory.similar was happened in my case then i use forward slash in windows then it works.

Compiled Extension File error Report in LiClipse Python

doc_holder_str = ''
sample_H_value = open("C:\testGupixwin\BX-N-H.HED", "r")
standard_conc_value = open("C:\testGupixwin\gupixwin_H_stdConc.txt", "r")
sample_H_value_str = sample_H_value.readline()
while sample_H_value_str is not '' :
stripper_sample_H = float(sample_H_value_str[5:].lstrip(' '))
I'm trying to write a piece of code (as shown above) which reads some values, do some calculations on it and returns the said values. I am using the LiClipse IDE, for python.
I have tested my code and it works, but when I tried to run it with real data, ( for which I created a new folder to put in all the files I will be working with) I received an OS error suggesting I inserted an invalid argument.
The error report says ;
Traceback (most recent call last):
File "C:\Python34\workspace\Gupixwin_Project.py", line 11, in <module>
sample_H_value = open("C:\testGupixwin\BX-N-H.HED", "r")
OSError: [Errno 22] Invalid argument: 'C:\testGupixwin\\BX-N-H.HED'
on clicking on the C:\testGupixwin\\BX-N-H.HED it bring up a message box suggesting, and I quote,
The definition was found at C:\testGupixwin\BX-N-H.HED, (which
cannot be opened because it is a compiled extension)
I must point out that I feel the error is that the system sees ...\\BX-N.... Instead of ..\BX-N... Which I expect.
Some one suggested I do this
[Open Window -> Preferences, goto PyDev -> Editor -> Code Style ->
File Types, look for "Valid source files (comma-separated)" and append
", log".]
I HAVE DONE THIS BUT I STILL GET THE OSERROR REPORT.
Thanks for any assistance
I think the problem is the escaping with \
alternate the string in: open("C:\testGupixwin\BX-N-H.HED", "r")
with:
open( r"C:\testGupixwin\BX-N-H.HED", "r" ) #rawstring
# or
open( "C:\\testGupixwin\\BX-N-H.HED", "r" ) #escaping the '\' with '\'
(also do this in the following line)

Open np.memmap() binary file within a python with-context manager

I have a very strange problem where I can't open a file from one of my larger scripts.
This problem is intermittent and I can't seem to understand the error.
I am getting this error:
IOError: [Errno 22] invalid mode ('w+') or filename: 'D:\\R4a\\2014_05\\R01359\\R01359_data_dwnhi.dat'
from this command in my script:
with open(os.path.normpath(os.path.join(sonpath,base+'_data_dwnhi.dat')), 'w+') as ff:
fp = np.memmap(ff, dtype='int16', mode='w+', shape=np.shape(tmp2))
when I use:
if os.path.exists(os.path.normpath(os.path.join(sonpath,base+'_data_dwnhi.dat')))== True:
print os.path.normpath(os.path.join(sonpath,base+'_data_dwnhi.dat')) + ' is a file'
it evaluates as True. Also when I run the open command by itself in ipython it sucessfully opens the file with:
open('D:\\R4a\\2014_05\\R01359\\R01359_data_dwnhi.dat', 'w+')
I am at a loss for words and think errno 22 error is misleading. As I stated earlier, the problem is intermittent and works on some files. Does anyone have an idea what I am doing wrong?
I am working on a windows 8 machine 64 bit and running python version 2.7.8 from an Anaconda 2.1.0 (64-bit) distribution.
Your opening it like a normal file, hence the error: IOError: [Errno 22] invalid mode. Do this instead:
with open(path, 'wb') as f:
pass
after some trial-errors, instruct np.memmap() to open a file with other than .__doc__ reported mode strings.
#------------------------------------------------------++++++++ TRY:
# ||||||||
# vvvvvvvv
|>>> MM = np.memmap( "temporary_DATA.memmap", mode = 'wNONSENSE+' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27.anaconda\lib\site-packages\numpy\core\memmap.py", line 208, in __new__
(valid_filemodes + list(mode_equivalents.keys())))
ValueError: mode must be one of ['r', 'c', 'r+', 'w+', 'readwrite', 'write', 'readonly', 'copyonwrite']
# ^ ^ ^ ^ ^ ^ ^ ^
# | | | | | | | |
#---------------------------------+----+----+-----+-----+------------+--------+-----------+
finally this error Traceback has shown alternative strings, not listed in np.memmap.__doc__, which do work fine
with open( os.path.normpath( os.path.join( sonpath,
base + '_data_dwnhi.dat'
)
),
'w+'
) as ff:
fp = np.memmap( ff, mode = 'readwrite', # options listed in np Traceback
shape = np.shape( tmp2 ),
dtype = 'int16'
)
For hawkish pythoneers, the post intentionally uses non-PEP-8 source code formatting as it is authors experience that during a learning phase, code read-ability improves the focus on task solution and helps getting used to the underlying concepts rather than to spend efforts on formally adhering typography. Hope the principle of providing help is respected and the non-PEP-8 styling format is forgiven in the name of the ease of reading.

Python: attempting to open serial caused IOError: [Errno 6] Device not configured

I trying to open two serial ports in Python with the following code:
for i in range(0, 2):
if platform.system() == "Darwin":
pos = 2+i
else:
pos = i
port = serial.Serial(current_ports[pos], BAUD_RATE, timeout=TIMEOUT)
time.sleep(1.516)
port.write('#')
time.sleep(1.516)
out = ''
print "Reading MAC Address...."
while port.inWaiting() > 0:
out += port.read(1)
print out
if out == '04:E9:E5:00:EC:51':
led_port = port
elif out == '04:E9:E5:01:0C:E0':
matrix_port = port
Sometimes the ports open successfully, sometimes they don't. When they don't, I get this error message:
Reading MAC Address....
Traceback (most recent call last):
File "animation.py", line 227, in <module>
main()
File "animation.py", line 208, in main
led_port, matrix_port = get_ports()
File
"/Users/collinschupman/Documents/FutureCities/MurmurWall/Onsite/Raspi/helper_functions.py", line 41, in get_ports
while port.inWaiting() > 0:
File "/Library/Python/2.7/site-packages/serial/serialposix.py", line 449, in inWaiting
s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
IOError: [Errno 6] Device not configured
As you can see, it gets to the inWaiting() call and then throws this error.
For a little reference, the code is sending a message to a couple Arduinos so they can be identified by their MAC addresses.
Is there anything blatantly incorrect I'm doing Python-side which would cause this setup to fail once and a while? I'd say this code works 50% of the time right now.
Thanks,
Collin
Is there a getty process running on the serial ports on the Arduinos? Or even on the serial port of the local machine which is connecting to the Arduinos? If so perhaps it is interfering and should be stopped. Here is a reference which may be relevant: http://codeandlife.com/2012/07/29/arduino-and-raspberry-pi-serial-communication/
I had similar problem and I fixed it with the following solution
$ sudo nano /etc/inittab
and go to the bottom of the file, you will see
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
or similar
simply add a # character to the beginning,now it looks like :
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
save the file and reboot
Hope this works
Reference:
http://www.hobbytronics.co.uk/raspberry-pi-serial-port

Categories

Resources