Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to putty and type few command

Tags:

python

putty

I want to connect to putty and want to do few step:

  1. login to Putty
  2. type few command to bring down the server
  3. Traverse to the particular path
  4. Remove the file from the directory
  5. Again start the server

I need to write the code in windows. But my server is in linux. How shall I proceed? Thanks in advance

like image 655
Reetesh Nigam Avatar asked Nov 03 '11 07:11

Reetesh Nigam


1 Answers

What you need is Paramiko, but it may be a little complicated for a beginner.

For simple, repetitive tasks you may use my script - it is located on GitHub (https://github.com/tadeck/ssh-matic) and was created to learn some Python. It is based on someone else's friendly SSH Python interface to Paramiko (code accessible here).

Using the mentioned SSH module connecting to server and executing a command is rather simple:

import ssh
server = ssh.Connection(host='host', username='user', private_key='key_path')
result = server.execute('your command')

Basically what you need is not PuTTy, but a SSH module to Python. This module should work both on Windows and Linux. Using my script you will only need to work on the command you want to invoke, plus adjust the code to your needs.

Good luck. Tell me if it helped.

like image 90
Tadeck Avatar answered Oct 20 '22 19:10

Tadeck