Issue : I'm a noob with Paramiko, trying to run some commands from a python script (on personal machine) on a remote server. The remote server doesn't need a password to connect to.
For example, if I do
root#[IPaddress] on my Mac, I'm successfully able to connect to the remote server via MacbookPro terminal.
However, I'm trying to do this inside a Python script using Paramiko, and no matter what I do, I get an Authentication error or No Authentication methods available.
I went through Paramiko AuthenticationException issue but the answers there are vague for me to implement without significant experience with Paramiko. Help?
This is my code:
import paramiko
import os
from paramiko import SSHClient
#Borrowed from the linked post
class SSHClient_noauth(SSHClient):
def _auth(self, username, *args):
self._transport.auth_none(username)
return
#How do I implement?
ssh = SSHClient()
sshc = SSHClient_noauth()._auth(username="root") #Where's the ssh obj passed?
sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshc.connect("10.xxx.xxx.xxx")
Well, not to let the negative vote bog me down.
Tried doing sshc.connect(remoteIP, username=username, password="") and it worked. In case someone has been stuck for over an hour or two trying to get this working, especially for work, you might want to try putting in a "" instead of None.
Related
Im a new bie to python programming and writing a script to login to a linux machine (from a server) and check for connectivity to multiple IPs in a quick manner. i came across this thread Multiple ping script in Python which uses subprocess.Popen and does it fast but it does check for ping from the server where the script is run rather than the linux machine
I have the code to ssh to the linux machine and get its handle. snippet below
is there a way to pass on this handle to the subprocess.Popen so that the ping can be checked from the linux machine instead of the server.
I tried to read the documentation of subprocess.Popen but could not get a clarity.
kindly help.
ssh = SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
for x in range(retries):
try:
ssh.connect(ip, username = username , password = password)
log.info('Connected to the machine {0} successfully..'.format(ip))
return ssh
except (paramiko.BadHostKeyException, paramiko.AuthenticationException, paramiko.SSHException, socket.error) as e:
log.info('Exception : {0}'.format(e))
return False
I used the code using subprocess.call() from the same link pasted above but that is not quick enough..
any help would be greatly appreciated.
I have a short python script (eventually to be larger) that is remote executing a 'pwd' command on the remote host. I am just testing to connect to the host using SSH keys (yes these are set up correctly). What i have so far seems to work, but it sort of just hangs there in the IDLE window...does not error out, but also if i check the host last login date, it doesn't match when i executed the script. Is there a way to have the output show (similar to expect, so i can see if i am actually even logging in).
Tried to use password instead, same exact behavior.
#!/usr/bin/python
from fabric import Connection
sshConnection = Connection(
host = 'hostname.com',
user='myuser',
connect_kwargs={
"key_filename":r'C:\Users\user\Desktop\id_rsa',
},
)
sshConnection.run('pwd')
close()
basically getting this in the IDLE window
>
RESTART: /PATH/TO/SCRIPT/ON/WINDOWS/LAPTOP/script.py
|
I advise you to use fabric2.4.0 and Python 3
from fabric import Connection as connection, task
#task
def deploy(ctx):
with connection(host=host, user=user) as c:
c.run('pwd')
Put the code above in a file called fabfile.py and you run it from your command line
fab deploy
In past I've connected to VPN via Linux in Windows perfectly with python by using the following code:
import win32ras
hdl, retcode = win32ras.Dial (None, None, (vpn_name, ip, "", username, password, ""), None) #changing 'ip' will connect to that server ip
win32ras.HangUp (hdl) #This disconnects the connection
So this works perfectly fine in windows with python but now I want to do it in ubuntu with python and I'm not sure how to do this at all. I want to do the same, define a VPN name and change its IP on connecting and input via username/password, if there's any other way, like directly connecting to the VPN without even making one then that's obviously better.
I'm finding the solution on internet as of now, will update if I find something.
How about using the Linux PPTP Client: http://pptpclient.sourceforge.net/ ?
A few options for calling it from Python:
1) Call it as a command-line tool using subprocess: https://docs.python.org/2/library/subprocess.html
2) Build it as a library and call it via Cython: http://cython.org/
3) Build it as a Python package (and for bonus points, make it available to others!): https://docs.python.org/2/extending/extending.html
I'm trying to write a fabric function that puts a script on a remote host and runs it as root. I don't have the root password to login to the machine, nor am I a sudoer, but I do have a root principle in kerberos. Typically, I can connect to a machine as root with the following:
kinit username/root
(enter root principle pass)
ssh root#host
Connecting in this manner I'm not prompted for a password when ssh'ing to the host.
So I want to emulate this process using fabric. To do so I assumed the following,
kinit user/root
fab task1 task2 --user=root
Unfortunately fabric prompts me for a password, while I do not have the root password, I can't supply this. Fabric will not let me pass a null for the password as far as I can tell. Any ideas?
Looks like Fabric doesn't support Kerberos authentication. If I remember correctly paramiko library doesn't support it either and Fabric uses paramiko (not sure), so it doesn't have corresponding support.
You should go and ask here:
http://docs.fabfile.org/en/1.4.1/index.html#getting-help
May be use IRC channel so as to get quick response.
Regards,
There is an open pull request for support of Kerberos in Fabric and it looks like it's working and is almost ready to be merged:
https://github.com/fabric/fabric/pull/1261
Fabric 2.6.0 supports gssapi through paramiko (03/2022). You'll also need to install python-gssapi.
You just need to pass connect_kwargs to __init__():
class GSSConnection(Connection):
def __init__(self, host):
connect_kwargs = dict(
gss_auth=True,
gss_deleg_creds=True,
gss_kex=True,
)
super().__init__(host, connect_kwargs=connect_kwargs)
I'm working on retrieving the Windows eventlog from a remote machine using Python.
I tried the following code:
import win32evtlog
server = 'aRemoteMachineHostName'
logtype = 'System'
hand = win32evtlog.OpenEventLog(server,logtype)
total = win32evtlog.GetNumberOfEventLogRecords(hand)
and I get an access denied error.
Then I tried use win32security.Logon to do the authentication before I access the eventlog.
But I find in the win32security.Logon document, it says the API only supports local logon.
So, I'm a little bit stuck here. I wonder whether Python can do a remote Windows logon?
Any tips or hints will be helpful.
Thanks.
might be easier to use snmp
As long as the user who is executing the script has the credentials to connect and read the event log, this ActiveState Recipe works for me. I've used it in a few different situations.
ActiveState WinEvtLogViewer