automation using python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting automation using python
# 1  
Old 08-23-2012
Java automation using python

Im trying to write an automation script using python. I expect this script to log in to a remote server, execute commands and get its output.

Code:
import pexpect 
child=pexpect.spawn('ssh myuser@192.168.151.80')
child.expect('Password:')
child.sendline('mypassword')
get_output = child.sendline('uname')
if get_output == 'Solaris':
  print 'this is solaris server'
else:
  print 'this is some other machine'

The fifth line of the code is wrong. Please let me know how to achieve this, I want to get the output of the command sent.

Also if the output is more than one line, how to store the output?
Is there any other better python module for automatio/remote execution ?

Last edited by Arun_Linux; 08-23-2012 at 10:28 AM..
# 2  
Old 08-23-2012
The reason you have to use a third-party brute-forcing utility to kludge stored plaintext passwords into ssh is because stored passwords are such a bad idea, ssh is actually designed to prevent you from using them. So are most other login systems.

If you used ssh as it was intended, with ssh keys, your job would be much simpler.

Code:
NAME=`ssh username@host uname`

# 3  
Old 08-24-2012
Corona688: Im able to login to the remote server and execute the commands. Problem is, im unable to get the output of the commands executed.
# 4  
Old 08-27-2012
Yes, because you're using such a complicated scheme. Using ssh as intended, getting its output is trivial. You can even send entire scripts over the wire with a simple redirect instead of using third-party brute forcing tools like expect.
# 5  
Old 08-28-2012
Finally found the solution... Heres the code..
Code:
import pexpect, datetime
child=pexpect.spawn('ssh myuser@192.168.151.80')
child.expect('Password:')
child.sendline('mypassword')
time.sleep(2)
child.sendline('uname')
get_output = child.before.split('\n')[-1]
if get_output == 'Solaris':
  print 'this is solaris server'
else:
  print 'this is some other machine'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

scp Automation

Hi, I have a requirement to automate SCP command. I have to write the scp command in server1 to copy file from server2 and paste it in server3. I will be passing server2 and server3 as variables. Pls suggest. (1 Reply)
Discussion started by: usrrenny
1 Replies

5. UNIX for Advanced & Expert Users

Linking Automation

Hi, How to link the files automatically in linux on daily basis ? For example : I have file abc.20130911.txt ln -s source/ abc.20130911.txt dest/abc.20130911.txt But in my machine files will be generate with date time stamp every day, without manual linking every day is there any... (4 Replies)
Discussion started by: Balasankar
4 Replies

6. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

7. Shell Programming and Scripting

suggest a python module for automation..

Im planning to automate my testcases. Please suggest me a python module that will ease my automation scripting. General steps involved in the testcase execution: (all steps below are done in linux machine) 1) start the application under test (AUT) 2) connect to oracle/ms sql db and update... (1 Reply)
Discussion started by: Arun_Linux
1 Replies

8. UNIX for Advanced & Expert Users

Need help in automation

Hi, I wanted to automate the scp command where i do not want to enter the password each time. So thought of using expect command. Script is executing without any issues but files are not copied to remote server. Can any one help me? Below is my shell script.. #!/bin/ksh ... (6 Replies)
Discussion started by: balasubramani04
6 Replies

9. Shell Programming and Scripting

Help in automation...

Hi All, I need to run the same command on many servers. I am using ssh for the same. Following is the script that I am using to fire the same command on multiple machines. #!/bin/bash # Linux/UNIX box with ssh key based login #SERVERS="iqmevrick,iqmango" # SSH User name USR="root" #... (1 Reply)
Discussion started by: nua7
1 Replies
Login or Register to Ask a Question