How can connect from one computer to another computer using python - python-3.x

I wanted to connect from one computer to another computer using python, (or remote computer)
I have installed wmi module and trying to use below code for connection from one computer to another computer,
ip = '100.00.00.00'
username = 'MyuserName'
password = 'myPassword'
#import sys, wmi
from socket import *
try:
print("Trying to connect %s" %ip)
wmi.WMI(ip, user=username,password = password)
print('connected')
except wmi.x_wmi:
print("Wrong username or password"
While running above code, I am getting error - except wmi.x_wmi:
NameError: name 'wmi' is not defined
Could you please help me for connecting from one computer to another computer
Thanks in advance
RaviK

It looks like your fourth line:
#import sys, wmi
probably just needs to be uncommented
import sys, wmi
If you still get the error you might need to install the wmi library

Related

How do i fix HTTPSConnectionPool - Read Timed Out Error when connecting to database server

I am trying to connect to a FileMaker Databse server via python script, and my code was working before but has suddenly stopped, and i didnt make any changes to the portion of code that no longer works. I am encountering the following error:
Request error: HTTPSConnectionPool(host='**.**.*.*', port=443): Read timed out. (read timeout=30)
I have taken out the code that creates the server instance and connects/logs in, and then logs out without making any changes in the database, and i am still recieving the same error. However, i can connect to the filemaker server and database via the FileMaker applicaiton with no issues, and i can connect to the server using Telnet commands. I am on windows 10 and writing the code in PyCharm CE. I have reinstalled PyCharm, created a new virtual environment, and tried reinstalling the fmrest module, as well as using older versions. I have also increased the timeout time to give more time to login, which hasnt worked. I'm basically stumped on why i can no longer log in via the script, when it has been working perfectly in testing for the past couple weeks. My code is below.
import fmrest
from fmrest.exceptions import FileMakerError
from fmrest.exceptions import RequestException
import sys
import requests
# connect to the FileMaker Server
requests.packages.urllib3.disable_warnings()
fmrest.utils.TIMEOUT = 30
try:
fms = fmrest.Server('https://**.**.*.*',
user = '***',
password = '******',
database = 'Hangtag Order Management',
layout = 'OrderAdmin',
verify_ssl = False)
except ValueError as err:
print('Failed to connect to server. Please check server credentials and status and try again\n\n' + str(err))
sys.exit()
print(fms)
print('Attempting to connect to FileMaker Server...')
try:
fms.login()
print('Login Successful\n')
except FileMakerError as err:
print(err)
sys.exit()
except RequestException as err:
print('There was an error connecting to the server, the request timed out\n\n' + str(err))
sys.exit()
fms.logout()
This should successfully login to the database, print 'login successful' and log out. Calling print(fms) returns
<Server logged_in=False database=Hangtag Order Management layout=OrderAdmin>
but i receive the connection error upon the login attempt. I am assuming the error is server side, but i dont know enough about servers to accurately trouble shoot. Could the server have blacklisted my IP for making so many login attempts during my testing? and if so where would i undo that/prevent it from happening again?
A couple of server reboots fixed the error, not really sure of the ultimate cause.

Connecting to oracle db from python

We are connecting to oracle from python using cx_oracle package.
But the user_id, password and SID details are hardcoded in that.
My question is, is there any way to create a Datasource kind of thing? Or how we will deploy such python script sin production?
The database is in a Linux box and python is installed in another Linux box(Weblogic server is also installed in this Linux box).
import cx_Oracle
con = cx_Oracle.connect('pythonhol/welcome#127.0.0.1/orcl')
print con.version
Expectation is :
Can we deploy python in a production instance?
If yes how can we connect to the database by hiding the DB credentials?
Use some kind of 'external authentication', for example a wallet. See the cx_Oracle documentation https://cx-oracle.readthedocs.io/en/latest/user_guide/connection_handling.html#connecting-using-external-authentication
In summary:
create a wallet with mkstore which contains the username/password credentials.
copy the wallet to the machines that are running Python
make sure no bad people can access the wallet
configure Oracle Net files to point to the wallet
your scripts would connect like
connection = cx_Oracle.connect(dsn="mynetalias", encoding="UTF-8")
or
pool = cx_Oracle.SessionPool(externalauth=True, homogeneous=False, dsn="mynetalias",
encoding="UTF-8")
pool.acquire()

How to type the credentials manually in Parallel-SSH or Paramiko

I am trying to create a script that will run commands over my 1000 Cisco devices.
The device model is: Cisco Sx220 Series Switch Software, Version 1.1.4.1
The issue is that there is some kind of strange behavior for some of those Cisco devices.
When I am trying to login with regular SSH (PUTTY) with the correct credentials we are first getting 'Authentication Failure' and after 1 seconds I am getting the User Password Prompt again, typing the same credentials again is giving me a successful login.
The problem is that when I am trying to connect using my script (uses ParallelSSHClient), the connection drops after getting the authentication failure message and not able to enter the credentials again since it is getting the exception and terminal the program.
I am looking for a way to enter those credentials manual by connecting to the machine, getting the Authentication Failure message and ignoring it, recognizing that the current prompt has the User or Password appears on screen and then send it manually.
I look for this kind of procedure anywhere but without any luck.
Does ParallelSSHClient has this feature?
If Paramiko has it, I am willing to move to Paramiko.
Thanks :)
try:
client = ParallelSSHClient(hosts=ip_list, user=user, password=password)
except Exception as err:
print("There was an issue with connecting to the machine")
command_output = client.run_command(command)
Here is the accrual error that I am getting:
pssh.exceptions.AuthenticationException: ('Authentication error while connecting to %s:%s - %s', '172.31.255.10', 22, AuthenticationException('Password authentication failed',))

Steam 2FA throwing "Failed to get a web session" consistantly

I'm using steam's python library to create an app for generating 2FA codes. The issue is that steam throws me this error whenever I try to add a phone number or add 2FA to said account:
RuntimeError: Failed to get a web session. Try again in a few minutes
Fri Mar 30 08:39:04 2018 <Greenlet at 0x6e64580: handle_after_logon> failed with RuntimeError
Yes I've waited a few minutes and have been trying for hours.
Here's the code that I'm using to attempt this:
from steam import SteamClient
from steam.enums.emsg import EMsg
from steam.guard import SteamAuthenticator
#Create our steamclient instance
client = SteamClient()
#client.on("logged_on")
def handle_after_logon():
print("You are now Logged in.")
#Setup Autenticator for our steamclient instance "client". "client" is our logged in SteamClient instance as requested by documentation
sa = SteamAuthenticator(medium=client)
#My account has no phone number
print(sa.has_phone_number())
#Adding phone number because I know ahead of time I don't have it
sa.add_phone_number("myphonenumber with area code")
sa.add() # SMS code will be sent to account's phone number
sa.secrets # dict with authenticator secrets
#We're gonna need these
print(sa.secrets)
sa.finalize(str(input("SMS CODE: "))) # activate the authenticator
sa.get_code() # generate 2FA code for login
sa.remove() # removes the authenticator from the account
try:
#Login to our steamclient instance
client.cli_login("myusername","mypassword")
#client.on("loggon_on") doesn't trigger without this
client.run_forever()
#Allow us to logout using keyboard interrupt
except KeyboardInterrupt:
if client.connected:
client.logout()
Due to a lack of example code on 2FA in particular I've followed the documentation the best I could, I've looked at all of the below:
http://steam.readthedocs.io/en/latest/api/steam.client.html
http://steam.readthedocs.io/en/latest/api/steam.guard.html
https://github.com/ValvePython/steam/blob/master/recipes/1.Login/persistent_login.py
I feel like there's simply a silly error in my code, but reading through the documentation doesn't appear to be helping me solve this.
Thanks for your guys help.
I debugged for a while and it's an issue with gevent
I added this line to fix it at the beginning of my script:
from gevent import monkey
monkey.patch_all(thread=False)

bottle.py WSGI server stops responding

I'm trying to build a simple API with the bottle.py (Bottle v0.11.4) web framework. To 'daemonize' the app on my server (Ubuntu 10.04.4), I'm running the shell
nohup python test.py &
, where test.py is the following python script:
import sys
import bottle
from bottle import route, run, request, response, abort, hook
#hook('after_request')
def enable_cors():
response.headers['Access-Control-Allow-Origin'] = '*'
#route('/')
def ping():
return 'Up and running!'
if __name__ == '__main__':
run(host=<my_ip>, port=3000)
I'm running into the following issue:
This works initially but the server stops responding after some time (~24hours). Unfortunately, the logs don't contain any revealing error messages.
The only way I have been able to reproduce the issue is when I try to run a second script on my Ubuntu server that creates another server listening to a different port (ie.: exactly the same script as above but port=3001). If I send a request to the newly created server, I also do not get a response and the connection eventually times out.
Any suggestions are greatly appreciated. I'm new to this, so if there's something fundamentally wrong with this approach, any links to reference guides would also be appreciated. Thank you!
Can you make sure the server isn't sleeping.
If it is, try enabling Wake On LAN http://ubuntuforums.org/showthread.php?t=234588

Resources