1

What I mean to ask is, if I am on System "A" (Linux) and I want to ssh into System "B" (Windows): On System "A", I can do ssh [email protected] which will prompt me to a password and when that gets authenticated, I will get to the "$" of System "B" (on System "A").

  1. how do I send username and password together as a single line (since I want to use a script)
  2. How to achieve the scenario that I have above.

2 Answers 2

2

I generally do it with Paramiko, its easier

import paramiko

# ssh 
print 'enter ssh'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # this will automatically add the keys
ssh.connect(machineHostName, username=user, password=password)

# Run your commands
# example 1 : ls command
print 'do a ls command'
stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
time.sleep(2)
# example 2 : change ip address
print 'changing ip address'
stdin, stdout, stderr = ssh.exec_command('sed -i -- s/'+oldIp+'/'+newIp+'/g /etc/sysconfig/network-scripts/ifcfg-eth0')
print stdout.readlines()
time.sleep(2)

To install Paramiko, you can download the tar.gz file from here.

Assuming you are really new to python, how to install this :

  • Download the tar.gz file
  • Extract the contents to a folder
  • cd into that extracted folder, from your terminal
  • execute this python setup.py install
  • then you can try something like the above example

NOTE : if you get stuck with installation comment here, and I can help you.

Sign up to request clarification or add additional context in comments.

4 Comments

Rather than manually downloading and installing it, it's typically much easier to pip install paramiko...
Suddev: Are you sure the exec_command() works for you back to back, with that sleep? I can only execute one exec_command() at a time - the 2nd time, i never get any o/p or error.. just blank stuff and I dont think the 2nd command ever got executed either!
Yes it's the right way to do it .. I have scripts exactly similar which works perfecrly
@user2939055 - please accept the answer, if you think it has worked for you. If you are stuck, add those in comments
1
  1. Instead of using a passphrase for authentication, you could use ssh keys as described here.

  2. Start your ssh client on System "A" using subprocess.call(['/path/to/ssh', '[email protected]', 'remote_script.sh'])

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.