| | |
Connecting to a Linux Machine via SSH
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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....
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....
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
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....
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)
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....
•
•
Join Date: May 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
# 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.
![]() |
Similar Threads
- How to Configure Samba on RedHat Linux (*nix Hardware Configuration)
- Problems with file transfer with XP connecting to Linux (Windows NT / 2000 / XP)
- How to connect to a LINUX machine using VB6 (Visual Basic 4 / 5 / 6)
- Can I run Linux code (32bit machine) in Linux (64bit machine) (C)
- Cant Login with SSH after IP change (Linux Servers and Apache)
- Shell HELP! (Getting Started and Choosing a Distro)
Other Threads in the Python Forum
- Previous Thread: python string encoding
- Next Thread: Image content retrieve
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory drive dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify webservice wikipedia windows write wxpython xlib





