简体   繁体   English

使用localhost上的python脚本将文件(scp)传输并运行命令从jumpserver到finalserver

[英]Transfer file(scp) and run command from jumpserver to finalserver using python script on localhost

A file needs to be uploaded to final-server and a command needs to be run using that file as input. 需要将文件上载到最终服务器,并需要使用该文件作为输入来运行命令。

Following will be the steps for whole action to be performed (but I am stuck at step 3): 以下是将要执行的全部操作的步骤(但我陷入了步骤3):

1 - transfer file from localhost to jumpserver (using scp) 1-将文件从本地主机传输到Jumpserver(使用scp)

2 - ssh to jumpserver 2-SSH到Jumpserver

3 - transfer file from jumpserver to finalserver (using scp) 3-将文件从Jumpserver传输到finalserver(使用scp)

4 - ssh to finalserver 4-SSH到最终服务器

5 - run the command on finalserver (with file as input) 5-在finalserver上运行命令(以文件作为输入)

try:
    cmd_str = "scp " + file_path + " " + user + "@" + jumpbox + ":/tmp/"
    print "---------local to jumpbox initiated:"
    #print cmd_str

    child = pexpect.spawn(cmd_str) #-------------------------------------------------------step 1
    child.expect("password:")
    #pwd = raw_input("please enter password along with yubikey:")
    pwd = getpass.getpass("Enter password along with yubikey:")
    child.sendline(pwd)
    i = child.expect(['Permission denied', filename])
    if i==0:
        print "Permission denied on host. Can't login"
        pwd = getpass.getpass("Enter password along with yubikey:")
        child.sendline(pwd)
        #child.kill(0)
    elif i==1:
        print "file successfully transferred to jumpbox"

        #----SSHing into jumpbox
        ssh_jump_cmd = "ssh " + user + "@" + jumpbox 
        print ssh_jump_cmd
        ssh_child = pexpect.spawn(ssh_jump_cmd) #-------------------------------------------------------step 2
        ssh_child.expect("password:")
        pwd2 = getpass.getpass("Enter password along with yubikey:")
        ssh_child.sendline(pwd2)
        i2 = ssh_child.expect(['Permission denied', '[#\$]'])
        if i2 == 0:
            print "Permission denied on host. Can't login. Try again"
            pwd2 = getpass.getpass("Enter password along with yubikey:")
            ssh_child.sendline(pwd2)
        elif i2 == 1:
            print "--------------Inside Jumpbox:"

            #sending file from jumpbox to finalserver
            ba_cmd = "scp /tmp/"+ filename + " " + cmd_var + ":/tmp/"
            print ba_cmd
            ba_child = pexpect.spawn(ba_cmd) #-------------------------------------------------------step 3
            ba_child.expect("Enter passphrase for key:")
            phrase = getpass.getpass("Enter passphrase: ")
            ba_child.sendline(phrase)
            print "Password should be asked:"
            i3 = ba_child.expect(['Enter passphrase for key', 'Permission denied', filename])
            if i3 == 0: 
                phrase = getpass.getpass("Enter passphrase: ")
                ba_child.sendline(phrase)
            elif i3 == 1:
                print "Lost connection:"
                ba_child.kill(0)
            elif i3 == 2: 
                ssh_ba_cmd = "ssh " + smba
                ba_ssh_child = pexpect.spawn(ssh_ba_cmd) #-------------------------------------------------------step 4
                ba_ssh_child.expect("Enter passphrase for key: ")
                phrase = getpass.getpass("Enter passphrase: ")
                ba_ssh_child.sendline(phrase)
                i4 = ba_ssh_child.expect(['Permission denied', '[#\$]'])
                if i4 == 0:
                    print "Passphrase incorrect. Exiting"
                    ba_ssh_child.kill(0)

                elif i4 == 1:
                    final_cmd = "ls -l" + filename #- just an example for this question
                    final_child = pexpect.spawn(final_cmd)
                    print "********************"
                    print sys.stdout

I spawn child using Python Pexpect and got successful till SSHing to jumpserver. 我使用Python Pexpect生成了孩子,并且在通过SSH连接到Jumpserver之前获得了成功。 When script tries to SCP from jumpserver to finalserver, following error gets generated : 当脚本尝试将SCP从Jumpserver迁移到finalserver时,将生成以下错误:

scp /tmp/filename finalserver:/tmp/
End Of File (EOF). Exception style platform.
<pexpect.spawn object at 0x7efddfdad650>
version: 3.1
command: /usr/bin/scp
args: ['/usr/bin/scp', 'filename', 'finalserver:/tmp/']
searcher: <pexpect.searcher_re object at 0x7efddfdad5d0>
buffer (last 100 chars): ''
before (last 100 chars): 'ssh: Could not resolve hostname finalserver: Name or service not known\r\r\nlost connection\r\n'
after: <class 'pexpect.EOF'>
match: None
match_index: None
exitstatus: 1
flag_eof: True
pid: 7244
child_fd: 5
closed: False
timeout: 30
delimiter: <class 'pexpect.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1

I went through http://stackoverflow.com/questions/17913540/python-ssh-in-to-a-jumpserver-and-then-ssh-in-to-a-host-from-the-jumpserver-to-e but still need help. 我经历了http://stackoverflow.com/questions/17913540/python-ssh-in-to-a-jumpserver-and-then-ssh-in-to-a-host-from-the-jumpserver-to-e但仍需要帮助。

TIA. TIA。

>    ba_child = pexpect.spawn(ba_cmd)

You are spawning a new local child. 您正在生一个新的本地孩子。 You should instead be sending the scp command to the ssh_child which is running a shell on the jumpserver. 相反,您应该将scp命令发送到在ssh_child上运行Shell的ssh_child。

Shaky on the syntax here, but something like 语法不稳定,但类似

ssh_child.sendline(ba_cmd)

I suggest to transfer files between server and execute commands with paramiko, just look at example 我建议在服务器之间传输文件并使用paramiko执行命令,仅看示例

import paramiko 
local_path = "foo/bar"
remote_path = "bar/foo"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("remote_url", username='username')
sftp = ssh.open_sftp()
ssh.exec_command('mkdir -p '+ remote_path)
sftp.put(local_path, remote_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python ssh进入跳转服务器,然后从跳转服务器ssh到主机执行命令 - python ssh in to a jumpserver and then ssh in to a host from the jumpserver to execute a command 如何使用python中的第三台本地计算机使用scp在两个远程服务器之间传输文件 - How to transfer a file between two remote servers using scp from a third, local machine in python 在不使用SCP的情况下在远程计算机上运行python脚本 - Run python script in remote machine without using SCP 在opensolaris上的python脚本中使用scp - using scp in a python script on opensolaris 使用密码从命令行运行SCP - Run SCP From Command Line With Password 使用python脚本进行实时文件传输 - Real Time File Transfer using python script 在 MacOS 上使用 Spotlight 从 .command 文件运行 Python 脚本 - Run a Python script from a .command file with Spotlight on MacOS 是否可以在 Python 中使用 SCP 从目录传输文件但忽略隐藏文件或符号链接? - Is it possible to transfer files from a directory using SCP in Python but ignore hidden files or sym links? 想要从shell脚本传递scp命令的密码,以便将文件从一台服务器传输到另一台服务器 - want to pass the password from the shell script for the scp command for transferring the file from one server to another 无法使用./从命令行运行python脚本<scriptname> - Unable to run python script from command line using ./<scriptname>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM