ssh/login to a buildserver on the network and logout

丶灬走出姿态 提交于 2019-12-11 13:27:25

问题


I am trying to ssh into a buildserver on the network and run some commands and logout of the server,i have looked at other posts and written the following code but its not working?can anyone suggest what is wrong or is there a better way to accomplish this?thanks in advance

import os
import sys
import pexpect
#os.system(ssh username@buildservername)
child = pexpect.spawn('ssh username@buildservername', logfile=sys.stdout)
#child.expect('Are you sure you want to continue connecting (yes/no)?')
#child.sendline('yes')
#child.expect('password:')
child.sendline('password')
cmd = 'hostname'
os.system(cmd)
os.chdir('//local/mnt/workspace')
os.mkdir('newdir')
os.getcwd()

回答1:


You may take a look a the Paramiko library, especialy the SFTPClient

It's a native Python SSHv2 protocol library.

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername', username='yadomi', password='password')

sftp = ssh.open_sftp()
sftp.chdir('/local/mnt/workspace')
sftp.mkdir('newdir')


来源:https://stackoverflow.com/questions/29957312/ssh-login-to-a-buildserver-on-the-network-and-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!