3

I am new to python. I need to login to a server daily (Desktop -> 1.32 -> 0.20 -> 3.26). For this I need to open putty and using ssh connection i am logging in. To do all this I want to write a script using python.

By using google I thought subprocess.Popen will do that. But Its not working fine.

1st trail:

import subprocess
pid = subprocess.Popen("putty.exe [email protected] -pw password").pid

Its working fine (Opening window logging into .32). But cant able to give input. I came to know that to give input for the same process we need to use pipes.

2nd trail:

from subprocess import Popen, PIPE, STDOUT
p = Popen("putty.exe [email protected] -pw password", stdout=PIPE, stdin=PIPE, stderr=STDOUT)    
grep_stdout = p.communicate(input=b'ssh xx.xx.x.20\n')[0]
print(grep_stdout.decode())

by using this i cant login for the first server also. After logging in to all servers I need the terminal as alive. how to do this???

Edit

I need to do this in a new putty window. After logging in dont close the window. I have some manual work to do.

4 Answers 4

2

There is a SSHv2 protocol implementation for python: http://www.paramiko.org/. You can easily install it with pip:

pip install paramiko

Then you can create ssh client, connect to your host and execute commands:

import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect('hostname', username='login', password='pwd')
stdin, stdout, stderr = ssh_client.exec_command('command')
Sign up to request clarification or add additional context in comments.

Comments

2

Use paramiko library python Establish a SSH connection using -

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname,username, password)

Check the status if connection is alive using -

status =  ssh.get_transport().is_active()
#returns True if connection is alive/active

ssh.exec_command() is basically a single session. Use exec_command(command1;command2) to execute multiple commands in one session

Also, you can use this to execute multiple commands in single session

channel = ssh.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')
stdin.write('''
  Command 1
  Command 2
  ''')

print stdout.read()

1 Comment

I need to pop a new window (putty) and have to login. After login is complete to all server (Desktop -> 1.32 -> 0.20 -> 3.26) don't close the putty window (I have some manual work to check).
2

use powershell to call putty in order to open a new window

from subprocess import Popen
Popen("powershell putty.exe user@host -pw mypassword")

Comments

0

I created a bat file on windows, which references putty and putty session-specific info. This bat file can run by itself on windows. To call from python, I used the subprocess.run() -- python 3.5+.

Example of bat file named putty.bat:

start c:\app\PuTTy\putty.exe -load 192.168.1.230-node1-logs -l <logon user> -pw <logon user password for putty session>

Breaking down the bat file:

  1. It begins with window's command "start".
  2. c:\app\PuTTy\putty.exe --> is the putty directory on Windows containing putty.exe.
  3. -load --> tells putty to load a putty profile. The profile is the name you see on the putty client, under "Saved Sessions".
  4. 192.168.1.230-node1-logs --> my putty session specific profile.
  5. -l for logon --> followed by the putty logon user.
  6. -pw is the logon password --> followed by the putty logon password. That concludes the contents of "putty.bat".

From within python, is used the subprocess.run() command.

Example:

import subprocess
...
...
                try:

                    process = subprocess.run(["putty.bat"], check=True, stdout=subprocess.PIPE, universal_newlines=True)

                    print(process.stdout)

                except Exception as e:

                    print("subprocess call error in open putty command")
                    
                    print(str(e))

I hope you find this helpful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.