Execute local script on remote Linux host

后端 未结 2 1108
情歌与酒
情歌与酒 2021-01-06 02:34

I have a local script that will not run without root privs on the remote host. I have edited and allowed the sudoers file to run the script from the home dir (/home/username

2条回答
  •  孤城傲影
    2021-01-06 03:02

    Copy the script with scp, then run it.

    scp script.sh user@hostname:
    ssh user@hostname sudo ./script.sh > results.txt
    

    To do it in one line:

    ssh user@hostname 'cat > script.sh; chmod 755 script.sh; sudo ./script.sh' < script.sh > results.txt
    

    However, this won't work if you need to enter a password into sudo. All of ssh's stdin will be put in the script. There might be a way to do this using Expect, but I don't have much expertise there.

提交回复
热议问题