如何使用Python在本地启动SSH会话? [英] How do I start an SSH session locally using Python?
问题描述
我要问的是,如果我在系统"A"(Linux)上并且要ssh进入系统"B"(Windows):在系统"A"上,我可以执行ssh admin@xx.xx .xx.xx,它将提示我输入密码,并且在通过身份验证时,我将进入系统"B"的"$"(在系统"A"上).
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 admin@xx.xx.xx.xx which will prompt me to a password and when that gets authenticated, I will get to the "$" of System "B" (on System "A").
- 我如何一起发送用户名和密码(因为我想使用脚本)
- 如何实现我上面的方案.
推荐答案
我通常使用Paramiko来做,它更容易
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)
要安装Paramiko,可以从此处下载tar.gz文件. .
To install Paramiko, you can download the tar.gz file from here.
假设您真的是python的新手,如何安装它:
Assuming you are really new to python, how to install this :
- 下载
tar.gz
文件 - 将内容提取到文件夹 从终端
-
cd
到提取的文件夹中 - 执行此
python setup.py install
- 然后您可以尝试类似上面的示例
- 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.
这篇关于如何使用Python在本地启动SSH会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!