How to execute a remote command over ssh with arguments?

前端 未结 4 706
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-07 20:09

In my .bashrc I define a function which I can use on the command line later:

function mycommand() {
    ssh user@123.456.789.0 cd testdir;./test         


        
4条回答
  •  臣服心动
    2020-12-07 20:24

    This is an example that works on the AWS Cloud. The scenario is that some machine that booted from autoscaling needs to perform some action on another server, passing the newly spawned instance DNS via SSH

    # Get the public DNS of the current machine (AWS specific)
    MY_DNS=`curl -s http://169.254.169.254/latest/meta-data/public-hostname`
    
    
    ssh \
        -o StrictHostKeyChecking=no \
        -i ~/.ssh/id_rsa \
        user@remotehost.example.com \
    << EOF
    cd ~/
    echo "Hey I was just SSHed by ${MY_DNS}"
    run_other_commands
    # Newline is important before final EOF!
    
    EOF
    

提交回复
热议问题