How to automate logging into a router from a gateway switch and running commands inside the router - cisco

I work in an environment where i have to log into multiple routers and get basic bgp , interface details etc. Unfortunately these routers are only accessible via logging into a gateway switch and then entering my username and IP of the router ,after which password entry prompt comes and i enter the password to log in to the router and get all the details.
I used paramiko to automate this and was so far able to log into the gateway switch and send commands. unfortunately im not able to get the correct code to log into the the router from the switch and give the password and show commands. Is this possible and if so how ?
Any help on this will be appreciated. Thanks
The below is the code i use to log into the switch
import paramiko
import getpass
import time
Host = "10.163.3.185"
username = "a2201471-3"
password = "ToW4Tj#7+u+z#FS"
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=Host, username=username, password=password)
print("Seccessfully Connected\n")
remote_connection = ssh_client.invoke_shell()
remote_connection.send("terminal len 0\n")
remote_connection.send("ssh -l gark_mnsey 10.162.248.47") ## this is the
command which i mannually type inside the switch to get the password prompt of the router.
time.sleep(1)
output = remote_connection.recv(18999).decode('ascii')
print(output)
ssh_client.close

Related

Python request authentication without password

I have a need to log in to an administrative profile on a site using 81 port. Username is "admin", and a password is empty. That is, if I open the login window through the browser, I will only need to specify the username in the input form.
I'm trying to do this:
s = requests.session
But using s.get(url=f'http://{ip}:81', auth=('admin', '')) i get (Responce [401])
And also using s.get(url=f'http://{ip}:81', auth=('admin', None)) i get (Responce [401])
Question: How can I get (Responce [200]) without password with python request?
Problem resolved:
from requests.auth import HTTPDigestAuth

How to connect via asyncpg with password? From Python 3.10 all example I see connect without password

I need example of connect to postgresql using asyncpg from python not only with username, as in documentation on site https://magicstack.github.io/asyncpg/current/usage.html, but with password too.
Here are some examples:
https://www.programcreek.com/python/example/102920/asyncpg.connect
tl;dr
return await asyncpg.connect(
user=DBUSER,
password=DBPASSWORD,
database=DBNAME,
host=DBHOST,
port=DBPORT,
)
parameters are optional, with defaults values, like default host is localhost, default port is 5432, etc.

Python SMTP Authentication error when sending email through Outlook only when password is provided

I am trying to use the generic python SMTP lib modules for sending an email from an outlook web application (not office 365) through Python. I am using the right host server name and port no. But still, I am unsure why I am receiving an authentication error. I understand that this is a frequently asked question, but I still wonder why it is unable to authenticate. Any help on this would be appreciated. Thank you.
Our email is configured with a DUO set-up (Two-factor authentication). I initially assumed this might be the reason. But the other methods I used worked perfectly fine without doing any additional setups
Some methods I tried were.
Using Python's win32com.client module in Python for sending email
through Desktop Outlook App.
Using Powershell to send an email
The code is pretty straight forward and simple.
import smtplib
from getpass import getpass
email = '********'
password =getpass("Enter your password")
with smtplib.SMTP('xxxx.xxx.xxx',25) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(email,password)
subject = 'Test'
body = 'Testing email'
msg = f'Subject:{subject}\n\n{body}'
smtp.sendmail(email,email,msg)
It always shows this error, irrespective of the ports I am using.
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Error: authentication failed: authentication failure')
EDIT:
Ok!! so the code absolutely runs fine if I remove the login and prompt password statement. But this seems to be dangerous and I am not sure why it's allowing us to send emails even though we are not logged in. Maybe the Windows Server Active Directory is already registered on the domain

Spotipy - Cannot log in to authenticate (Authorization Code Flow)

I am working with the Spotipy Python library to connect to the Spotify web API. I want to get access to my Spotify's user account via Authorization Code Flow. I am using Python 3.5, Spotipy 2.4.4, Google Chrome 55.0.2883.95 (64-bit) and Mac OS Sierra 10.12.2
First, I went to the Spotify Developer website to register the program to get a Client ID, a Client Secret key and enter a redirect URI (https://www.google.com) on my white-list.
Second, I set the environment variables from terminal:
export SPOTIPY_CLIENT_ID='my_spotify_client_id'
export SPOTIPY_CLIENT_SECRET='my_spotify_client_secret'
export SPOTIPY_REDIRECT_URI='https://www.google.com'
Then I try to run the example code typing 'python3.5 script.py my_username' from terminal. Here is the script:
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-read'
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print "Usage: %s username" % (sys.argv[0],)
sys.exit()
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print track['name'] + ' - ' + track['artists'][0]['name']
else:
print "Can't get token for", username
When running this code, it takes me to a log in screen on my browser. I enter my Spotify's credential to grant access to my app. But when I finally click on 'log in' (or 'Iniciar sesiĆ³n' in Spanish) nothing happens. I tried to log in with my Facebook account but it does not work either. It seems that I get a Bad Request every time that I click on 'log in'. Here is a capture of Chrome:
The process is incomplete because when trying to enter the redirect URI back on terminal I get another Bad Request:
raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request
I tried to restart my computer, clean my browser cookies, use another different browser but did not work. It seems that I am not the only one having this problem. Is it maybe a bug? Please, avoid answers like "read the documentation of the API going here". Thank you.
Solved it. In order to run the Authorization Code Flow example code provided in the Spotipy's documentation correctly, I specified the redirect URI in line 13 of the example script when calling util.prompt_for_user_token, even when I had done this previously when setting environment variables:
token = util.prompt_for_user_token(username, scope, redirect_uri = 'https://example.com/callback/')
Likewise, do not use https://www.google.com or similar web address as your redirect URI. Instead, try 'https://example.com/callback/' or 'http://localhost/' as suggested here. Do not forget that the redirected URL once you are logged in must have the word code included.
Cheers,

Web2py - Using a Gmail address

I am beginner with web2py. I have just created a new project.
I want to use a gmail address, let's say g#gmail.com. What do I need to modify ?
mail.settings.server = 'logging' or 'smtp.gmail.com:587' # your SMTP server
mail.settings.sender = 'g#gmail.com' # your email
mail.settings.login = 'g#gmail.com:mypassword' # your credentials or None
Is this OK ?
What is the purpose of 'logging' ?
Should be
mail.settings.server = 'smtp.gmail.com:587'
Setting mail.settings.server = 'logging' has the effects of logging to console requests for sending emails but does not send the emails. It is useful for debugging email problems.

Resources