Ssh communicator with paramiko - python

I try to create a ssh class on Python.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password)
stdin, stdout, stderr = ssh.exec_command('ls -l')
ssh.close()
This code provides to connect server but when i try to connect another server, i have this error:
userauth is OK
transport._log() => Authentication type (publickey) not permitted.
transport._log() => Allowed methods: ['password']
transport._log() => EOF in transport thread
I couldn't resolve the error. any idea?

Related

SFTP Connection From Python [duplicate]

I have executed commands on server using ssh. Now I have to do another ssh to different IP while keeping old ssh active.
This new IP is port forward which will then used to do SFTP.
Issue I am facing is both ssh connections are on same port so not able to do second ssh.
Which is failing the SFTP.
Any support for same will be helpful.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=username, password=password, port=22)
time.sleep(3)
#Invoke shell to open multiple channel with in one SSH
chan = ssh.invoke_shell()
chan.send(ip1+'\n')
time.sleep(5)
chan.send(pass1+'\n')
time.sleep(10)
chan.send("ssh "+ip2+'\n')
time.sleep(10)
chan.send(pass2+'\n')
time.sleep(5)
#Execute command
chan.send(cmd)
#connect to another ip to do sftp
ssh1 = paramiko.SSHClient()
ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("127.0.0.4", username=usr2, password=pass2, port=22)
sftp=ssh.open_sftp()
It looks like misunderstanding. Your code does not do any port forwarding.
For the correct code, see Nested SSH using Python Paramiko.
If you need SFTP, not shell, just do:
jhost.open_sftp()
instead of
jhost.exec_command(command)
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Paramiko SSHClient.connect fails while SSH works

in the terminal, the following works just fine:
> ssh me#host -p 22
but the following python code gives me a TimeoutError:
from paramiko import SSHClient
client = SSHClient()
client.connect(hostname='host', port=22, username='me', look_for_keys=True)
Any idea why that would be the case? The connection uses RSA keys for authentication, and I'm over a VPN. Seems like I'm missing some context I need to tell paramiko about, but I can't figure out what it is.
I usually connect to a remote server by adding an explicit link to my private key:
HOST = 'hostname'
USER = 'username'
PKEY = '/path/to/rsa_private_key'
PKEY_PASS = 'xxx'
k = paramiko.RSAKey.from_private_key_file(PKEY, PKEY_PASS)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "connecting"
ssh.connect(HOST, username=USER, pkey=k)
connect_status = 'connected' if ssh else 'failed to connect'
print connect_status

SSH to server through middle server using python

I'm trying to do ssh through middle server with python.
I tried to work as written at this example : SSH to machine through a middle host
but when I'm running the code, I'm getting the following exception paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
When I'm opens the terminal that executes the program password authentication is required in order to proceed
I also edited the ~/.ssh/config as follows:
Host cybnode13
ProxyCommand ssh root#cybhead1.lnx.biu.ac.il nc %h %p
My code is attached below
proxy_command = 'ssh -p %s %s#%s nc %s %s' % (password, proxy_user, proxy_host, host, 22)
proxy = paramiko.ProxyCommand(proxy_command)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=proxy_user, password=password, sock=proxy)
stdin, stdout, stderr = client.exec_command('echo HELLO')

Create a SSH session connect to device through a terminal server

I want to connect to a network device. But in out policy, I have to ssh successfully to a terminal server first, then from this one, ssh to network device. In Python, I use Paramiko :
import paramiko
ssh = paramiko.SSHClient()
print("OTP : ")
otp = raw_input()
ssh.port=9922
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect('10.0.0.1',9922,username='khangtt',password=str('12345')+str(otp))
stdin, stdout, stderr = ssh.exec_command('whoami')
stdin.close()
for line in stdout.read().splitlines():
print(line)
Connect to server sucessfully, I can see my username in the output. But I don't know how to SSH to device. I used this but nothing happen to set input user/pass :
stdin, stdout, stderr = ssh.exec_command('telnet 10.80.1.120')
stdin.close()
for line in stdout.read().splitlines():
print(line)
Since you are using paramiko, use the documentation here: https://pynet.twb-tech.com/blog/python/paramiko-ssh-part1.html. Its very explicit.
Or you can also try the codes here to connect:
ssh.connect('10.0.0.1', 'khangtt', str('12345')+str(otp), look_for_keys=False, allow_agent=False)
Or
ssh.connect('10.0.0.1', 'khangtt', str('12345')+str(otp))
in case you don't need look for keys and allow agent.

python ssh connection problem

I tried this code:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.0.222', username='sshuser', password='pass')
stdin, stdout, stderr = ssh.exec_command("pwd")
stdout.readlines()
and the ssh connection works, but as soon as I use:
stdin, stdout, stderr = ssh.exec_command("pwd")
I get this error message:
Exception in thread Thread-1 (most likely raised during interpreter shutdown)
How can I just do the "pwd" command and get the output?
Thanks!
try:
stdin, stdout, stderr = ssh.exec_command("pwd")
except SSHException:
ssh.close()
That will keep it from crashing like that, but won't fix your problem. Make sure you can connect with a regular ssh client and run pwd. Then make sure your login credentials are correct.

Categories