Execute command/script using different shell in SS

2019-02-20 08:47发布

I am rather new to Linux and Paramiko, but the issue I am having is anytime I attempt to change a shell the remote Paramiko session will hang.

The remote host is in /etc/csh by default I am running various scripts, some require csh and others require bash. Any of my scripts running in csh work properly since the remote host is in csh by default.

To run the other scripts I need to be in bash. Whenever I attempt to change the shell using bash or /bin/bash paramiko connection will simply hang. I am using the following command to verify shells prior to connection and after attempting to temporarily change the shell to see what works, but nothing has. This is using Paramiko and Python 3.6.5. Note: this also fails the other way around; if I put the remote host in bash by default it will fail to switch to csh

main.py

connection = SSH.SSH(hostname, username, password)
connection.changeShell('echo $0 ; echo $shell; /bin/bash ; echo $shell ; echo $0')

This has also been tried as just bash and chsh

SSH.py

class SSH:
    client = None

    def __init__(self, address, username, password):
        print("Login info sent.")
        print("Connecting to server.")
        self.client = client.SSHClient()    # Create a new SSH client
        self.client.set_missing_host_key_policy(client.AutoAddPolicy())
        self.client.connect(address, username=username,
                password=password, look_for_keys=False) # connect

    def changeShell(self, command):
        print("Sending your command")
        # Check in connection is made previously
        if (self.client):
            stdin, stdout, stderr = self.client.exec_command(command)
            while not stdout.channel.exit_status_ready():
                # Print stdout data when available
                if stdout.channel.recv_ready():
                    # Retrieve the first 1024 bytes
                    alldata = stdout.channel.recv(2048)
                    while stdout.channel.recv_ready():
                        # Retrieve the next 1024 bytes
                        alldata += stdout.channel.recv(2048)

                    # Print as string with utf8 encoding
                    print(str(alldata, "utf8"))

            stdin.close()
            stdout.close()
            stderr.close()

        else:
            print("Connection not opened.")

1条回答
时光不老,我们不散
2楼-- · 2019-02-20 09:38

Your question has nothing to do with Paramiko. Try to paste your command in SSH terminal - It won't work either.


The syntax aaa ; bbb executes the commands one after another. bbb won't be executed until aaa finishes. Similarly, /bin/bash ; echo $shell executes bash and the echo won't be executed until bash finishes, what it never does, hence the hang.

You actually do not want to execute echo after bash - you want to execute echo within bash.

If you want to execute a script/commands within a different shell, you have three options:

  • Specify the shell that the script needs in the script itself using shebang - This the the right way for scripts.

    #!/bin/bash
    
  • Execute the script/commands using shell command-line:

    /bin/bash script.sh
    

    or

    /bin/bash -c "command1 ; command2 ; ..."
    
  • Write the script/command to be executed to a shell input, like I've shown you in your previous question:

    Pass input/variables to bash script over SSH using Python Paramiko

查看更多
登录 后发表回答