I am developing a Python script in version 3.61 to connect to an Imap server and select all email messages older than a certain date. I imported the imapclient module and then provided the following in the IDLE shell:
HOST = 'imap.comcast.net'
USERNAME = 'username'
PASSWORD = 'topsecret'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
I received the following errors after using the login option:
Traceback (most recent call last):
File "C:\Python\Python36-32\lib\imaplib.py", line 1011, in _command_complete
typ, data = self._get_tagged_response(tag)
File "C:\Python\Python36-32\lib\imaplib.py", line 1123, in
_get_tagged_response
self._check_bye()
File "C:\Python36-32\lib\imaplib.py", line 926, in _check_bye
raise self.abort(bye[-1].decode(self._encoding, 'replace'))
imaplib.IMAP4.abort: Zimbra IMAP server terminating connection
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
server.login(USERNAME, PASSWORD)
File "C:\Python\Python36-32\lib\site-packages\imapclient\imapclient.py",
line
215, in login
unpack=True,
File "C:\Python\Python36-32\lib\site-packages\imapclient\imapclient.py",
line
1180, in _command_and_check
typ, data = meth(*args)
File "C:\Python36-32\lib\imaplib.py", line 588, in login
typ, dat = self._simple_command('LOGIN', user, self._quote(password))
File "C:\Python\Python36-32\lib\imaplib.py", line 1188, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Python\Python36-32\lib\imaplib.py", line 1013, in _command_complete
raise self.abort('command: %s => %s' % (name, val))
imaplib.IMAP4.abort: command: LOGIN => Zimbra IMAP server terminating
connection
The docs indicate to me that this should be sufficient to make the connection but it is not clear why the connection is terminated after attempting the login.
Any help provided would be most welcome.
Related
I've build a very simple script that fetches emails from an Outlook inbox. It looks like this:
from imap_tools import MailBox
inbox = MailBox("outlook.office365.com").login("email", "password")
for email in inbox.fetch(): # bulk=True
print(email)
It's working when I use it like this. Though, when passing the arg bulk=True on the fetch() function it will raise the following error:
Traceback (most recent call last):
File "/Users/julius/Library/Application Support/JetBrains/IntelliJIdea2022.3/scratches/scratch_1.py", line 5, in <module>
for email in inbox.fetch(bulk=True):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 171, in fetch
for fetch_item in (self._fetch_in_bulk if bulk else self._fetch_by_one)(nums, message_parts, reverse): # noqa
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/imap_tools/mailbox.py", line 144, in _fetch_in_bulk
fetch_result = self.client.fetch(','.join(message_nums), message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 548, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1230, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/imaplib.py", line 1008, in _command
raise self.abort('socket error: %s' % val)
imaplib.IMAP4.abort: socket error: EOF occurred in violation of protocol (_ssl.c:2396)
Has anyone got a clue on how to fix this?
Right answer: combine bulk and limit args.
Text from docs:
For actions with a large number of messages imap command may be too large and will cause exception at server side, use 'limit' argument for fetch in this case.
I'm pretty new with Paramiko and python in general. I'm trying to connect to a server and execute some commands on it by ssh through the use of Paramiko's API and a program called Pomerium. My ssh config file is as shown below
Host *.stg-id-proxy.lab.com
ProxyCommand pomerium-cli tcp --listen - %h:%p
User placeholder
and my python code in file "ssh4.py" is as follows
import paramiko
host = "place.stg-id-proxy.lab.com"
port = 22
username = "placeholder"
password = "pass#!#"
command = "pwd"
proxy = paramiko.ProxyCommand("pomerium-cli tcp --listen - %h:%p")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password, allow_agent=False, look_for_keys=False, sock=proxy, banner_timeout=60)
stdin, stdout, stderr = ssh.exec_command(command)
lines = stdout.readlines()
print(lines)
but when I tried running:
python3 ssh4.py
I keep getting the following error
Exception (client): Error reading SSH protocol banner
Traceback (most recent call last):
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2271, in _check_banner
buf = self.packetizer.readline(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 622, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2094, in run
self._check_banner()
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2275, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Traceback (most recent call last):
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2271, in _check_banner
buf = self.packetizer.readline(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 380, in readline
buf += self._read_timeout(timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/packet.py", line 622, in _read_timeout
raise socket.timeout()
socket.timeout
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "ssh4.py", line 11, in <module>
ssh.connect(host, port, username, password, allow_agent=False, look_for_keys=False, sock=proxy, banner_timeout=60)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/client.py", line 406, in connect
t.start_client(timeout=timeout)
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 699, in start_client
raise e
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2094, in run
self._check_banner()
File "/Users/placeholder/Library/Python/3.8/lib/python/site-packages/paramiko/transport.py", line 2275, in _check_banner
raise SSHException(
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
So, any help would be appreciated. Also documentation for Pomerium can be found here:
https://github.com/pomerium/cli
Thank you in advance.
I am getting this error while searching a user with "uid" with python ldap3 module
Error:
Traceback (most recent call last):
File "/Users/robinmalhotra/PycharmProjects/pythonProject/main.py", line 6, in <module>
print(c.extend.standard.who_am_i())
File "/Users/robinmalhotra/.local/share/virtualenvs/pythonProject-AY4VAyDV/lib/python3.9/site-packages/ldap3/extend/__init__.py", line 65, in who_am_i
return WhoAmI(self._connection,
File "/Users/robinmalhotra/.local/share/virtualenvs/pythonProject-AY4VAyDV/lib/python3.9/site-packages/ldap3/extend/operation.py", line 53, in send
resp = self.connection.extended(self.request_name, self.request_value, self.controls)
File "/Users/robinmalhotra/.local/share/virtualenvs/pythonProject-AY4VAyDV/lib/python3.9/site-packages/ldap3/core/connection.py", line 1275, in extended
response = self.post_send_single_response(self.send('extendedReq', request, controls))
File "/Users/robinmalhotra/.local/share/virtualenvs/pythonProject-AY4VAyDV/lib/python3.9/site-packages/ldap3/strategy/base.py", line 336, in send
raise LDAPSocketOpenError(self.connection.last_error)
ldap3.core.exceptions.LDAPSocketOpenError: unable to send message, socket is not open
Here is my Script:
from ldap3 import Server, Connection, ALL
s = Server(host='oimdevsand', port=389, use_ssl=False, get_info='ALL')
c = Connection(s, user='uid=idmsa,ou=People,ou=auth,o=ABX', password='XYZ', auto_bind='NONE', version=3, authentication='SIMPLE', \
client_strategy='SYNC', auto_referrals=True, check_names=True, read_only=False, lazy=False, raise_exceptions=False)
print(c.extend.standard.who_am_i())
c.search("ABC","o=abc","(uid=abc)")
print(c.entries)
I am trying to hit an api using http.client of pyhton3 to simulate how I would do the same in a web browser.
However, http.client feels that url is inappropriate.
This is what I am trying to do.
import http.client
connection = http.client.HTTPSConnection("https://analyticsapi.zoho.com/api/EmailAddress/WorkspaceName/TableName?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=************&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=TRUE&ZOHO_FILE=/home/dev1/Desktop/Zoho/temporary.csv")
connection.request("GET", "/")
response = connection.getresponse()
print("Status: {} and reason: {}".format(response.status, response.reason))
connection.close()
And this is the error I am getting.
$ python3 pyToTestPushingCSV.py
Traceback (most recent call last):
File "/usr/lib/python3.5/http/client.py", line 798, in _get_hostport
port = int(host[i+1:])
ValueError: invalid literal for int() with base 10: '//analyticsapi.zoho.com/api/usename/ATable/InsideTable?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=****
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pyToTestPushingCSV.py", line 3, in <module>
connection = http.client.HTTPSConnection("https://analyticsapi.zoho.com/api/usename/ATable/InsideTable?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=****************&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=TRUE&ZOHO_FILE=/home/dev1/Desktop/Zoho/temporary.csv")
File "/usr/lib/python3.5/http/client.py", line 1233, in __init__
source_address)
File "/usr/lib/python3.5/http/client.py", line 762, in __init__
(self.host, self.port) = self._get_hostport(host, port)
File "/usr/lib/python3.5/http/client.py", line 803, in _get_hostport
raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
http.client.InvalidURL: nonnumeric port: '//analyticsapi.zoho.com/api/usename/ATable/InsideTable?ZOHO_ACTION=IMPORT&ZOHO_OUTPUT_FORMAT=XML&ZOHO_ERROR_FORMAT=XML&ZOHO_API_VERSION=1.0&authtoken=*************&ZOHO_IMPORT_TYPE=APPEND&ZOHO_AUTO_IDENTIFY=TRUE&ZOHO_ON_IMPORT_ERROR=ABORT&ZOHO_CREATE_TABLE=TRUE&ZOHO_FILE=/home/dev1/Desktop/Zoho/temporary.csv'
When I hit the URL with my Browser it gives a good response back in XML which in short, succeeds.
This is where I referred for documentation
Can you pin point where I went wrong?
As per documentation HTTPS support is only available if Python was compiled with SSL support (through the ssl module).
Also the default port for https connection is 443. The module seems to be hitting that port by default.
I am using a Python script which requires to have root access.
First I am becoming a normal user, then I want to become root. But, stdin.write is still throwing error.
Here is my script:
import os
import paramiko
import subprocess
user="a554511"
p="xxxxxxx"
root="abcdefgh"
x="vljhggsajgfj"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(x,username=user,password=p)
stdin.write('su -')
stdin.write("\n")
stdin.write(root)
stdin.flush()
stdin,stdout,stderr=ssh.exec_command('pwd')
x=stdout.readlines()
print(x)
ssh.close()
The error I get is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/file.py", line 339, in write
self._write_all(data)
File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/file.py", line 456, in _write_all
count = self._write(data)
File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 1220, in _write
self.channel.sendall(data)
File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 744, in sendall
sent = self.send(s)
File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 698, in send
return self._send(s, m)
File "/usr/lib/python2.6/site-packages/paramiko-1.15.2-py2.6.egg/paramiko/channel.py", line 1058, in _send
raise socket.error('Socket is closed')
socket.error: Socket is closed