943,822 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 14432
  • Python RSS
Oct 10th, 2005
0

Connecting to a Linux Machine via SSH

Expand Post »
Hi friends

I am trying to connect to a Linux machine from a Windows PC. I used to do it thru TELNET, but now I want to use SSH for better security. I tried with PYSSH, but I am going nowhere with it... Parmaiko is not working in Windows... Is there any way this can be done? I just want python to login, issue a command, get the output & then log back out...

Thanks in advance....
Similar Threads
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
xav.vijay is offline Offline
52 posts
since Aug 2005
Oct 10th, 2005
0

Re: Connecting to a Linux Machine via SSH

I just installed the pyssh module, it does not really have any good examples to follow. If I figure someting out I will let you know.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Dec 10th, 2005
0

Re: Connecting to a Linux Machine via SSH

I have found a way to SSH to a *IX machine... I tried it with Linux, AIX & Solaris Machines... it works fine..

I downloaded a command line version of PUTTY, called as PLINK. Its a .EXE file.

when executed from the command line

Python Syntax (Toggle Plain Text)
  1. c:\plink username@192.168.0.1 -pw <password> <command to be executed in server>

it just runs the required command & displays the output in our local machine..

So by using os.popen(), we can execute it & fetch the output for further processing....
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
xav.vijay is offline Offline
52 posts
since Aug 2005
Dec 10th, 2005
0

Re: Connecting to a Linux Machine via SSH

Thanks for the update :-)

this could come in handy. I am always writing scripts that need to connect my windows and linux boxes. Using ssh I could use this to communicate with computers both on my intranet and also remote computers on the internet.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 9th, 2008
0

Re: Connecting to a Linux Machine via SSH

You can use the following code snippet to connect thru ssh and also do scp.

# Login to ABC Machine via SSH
child = pexpect.spawn('ssh -l %s %s'%(ABCusername, ABChostname))
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
if i == 0: # Timeout
print 'ERROR! could not login with SSH. Here is what SSH said:'
print child.before, child.after
print str(child)
sys.exit (1)
if i == 1: # In this case SSH does not have the public key cached.
child.sendline ('yes')
child.expect ('(?i)password')
if i == 2:
# This may happen if a public key was setup to automatically login.
pass
if i == 3:
child.sendline(ABCpassword)
# Now we are either at the command prompt or
# the login process is asking for our terminal type.
i = child.expect ([COMMAND_PROMPT, TERMINAL_PROMPT])
if i == 1:
child.sendline (TERMINAL_TYPE)
child.expect (COMMAND_PROMPT)

# Set command prompt to something more unique.

COMMAND_PROMPT = "\[PEXPECT\]\$ "
child.sendline ("PS1='[PEXPECT]\$ '") # In case of sh-style
i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
if i == 0:
print "# Couldn't set sh-style prompt -- trying csh-style."
child.sendline ("set prompt='[PEXPECT]\$ '")
i = child.expect ([pexpect.TIMEOUT, COMMAND_PROMPT], timeout=10)
if i == 0:
print "Failed to set command prompt using sh or csh style."
print "Response was:"
print child.before
sys.exit (1)

# Now you can do scp as follows:

child.sendline('scp ~/sample.txt %s@%s:/sample.txt'%(XYZusername, XYZhostname))
i = child.expect([pexpect.TIMEOUT, SSH_NEWKEY, COMMAND_PROMPT, '(?i)password'])
if i == 0: # Timeout
print 'ERROR! could not copy with SCP. Here is what SCP said:'
print child.before, child.after
print str(child)
sys.exit (1)
if i == 1: # In this case SSH does not have the public key cached.
child.sendline ('yes')
child.expect ('(?i)password')
if i == 2:
# This may happen if a public key was setup to automatically login.
pass
if i == 3:
child.sendline(XYZpassword)
child.expect(COMMAND_PROMPT)
print child.before

### All u r doing is; connecting to a machine called ABC and from there, u r doing scp to any required machine XYZ.

### I hope it helps u.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ms_melagiri is offline Offline
3 posts
since May 2008
Jun 9th, 2008
0

Re: Connecting to a Linux Machine via SSH

Thanks ms_melagiri ...

I will use this code my future implementations....
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
xav.vijay is offline Offline
52 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python string encoding
Next Thread in Python Forum Timeline: Image content retrieve





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC