Execute bash script on remote server non-interactively

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have a bash script that accepts exactly 3 arguments and I have created a web interface in PHP to run this script on a remote server. The user just enters username host and password of the remote server.



I found this command to execute bash script on remote server:



ssh root@host 'bash -s' < script.sh


But this command prompts for a password and also doesn't use any arguments. But I need something that can be run non-interactively.



Something like:



ssh root@host -password="password" 'bash -s' < script.sh









share|improve this question



















  • 3




    set up ssh keys for passwordless login: askubuntu.com/questions/46930/… If that's not an option, use sshpass
    – ignite
    Sep 10 at 11:49











  • to setup ssh key I have to login to remote server manually. thats what i don't want
    – Amarjit Singh
    Sep 10 at 11:50











  • @AmarjitSingh: You don't need to log in manually to set up ssh keys -- just use ssh-copy-id. Or am I misunderstanding something?
    – Daniel Pryden
    Sep 10 at 17:16










  • @DanielPryden ssh-copy-id command also prompts for the password. But I need a command that is non-interactive.
    – Amarjit Singh
    Sep 10 at 19:15










  • @AmarjitSingh: So you have two servers, and you don't have interactive access to either, but you can run arbitrary shell commands on the first server that contain the root password for the second server in plain text? If you don't have shell access, that implies that this is someone else's server -- do you really want to put the password to another machine in plain text there?
    – Daniel Pryden
    Sep 10 at 20:50














up vote
6
down vote

favorite












I have a bash script that accepts exactly 3 arguments and I have created a web interface in PHP to run this script on a remote server. The user just enters username host and password of the remote server.



I found this command to execute bash script on remote server:



ssh root@host 'bash -s' < script.sh


But this command prompts for a password and also doesn't use any arguments. But I need something that can be run non-interactively.



Something like:



ssh root@host -password="password" 'bash -s' < script.sh









share|improve this question



















  • 3




    set up ssh keys for passwordless login: askubuntu.com/questions/46930/… If that's not an option, use sshpass
    – ignite
    Sep 10 at 11:49











  • to setup ssh key I have to login to remote server manually. thats what i don't want
    – Amarjit Singh
    Sep 10 at 11:50











  • @AmarjitSingh: You don't need to log in manually to set up ssh keys -- just use ssh-copy-id. Or am I misunderstanding something?
    – Daniel Pryden
    Sep 10 at 17:16










  • @DanielPryden ssh-copy-id command also prompts for the password. But I need a command that is non-interactive.
    – Amarjit Singh
    Sep 10 at 19:15










  • @AmarjitSingh: So you have two servers, and you don't have interactive access to either, but you can run arbitrary shell commands on the first server that contain the root password for the second server in plain text? If you don't have shell access, that implies that this is someone else's server -- do you really want to put the password to another machine in plain text there?
    – Daniel Pryden
    Sep 10 at 20:50












up vote
6
down vote

favorite









up vote
6
down vote

favorite











I have a bash script that accepts exactly 3 arguments and I have created a web interface in PHP to run this script on a remote server. The user just enters username host and password of the remote server.



I found this command to execute bash script on remote server:



ssh root@host 'bash -s' < script.sh


But this command prompts for a password and also doesn't use any arguments. But I need something that can be run non-interactively.



Something like:



ssh root@host -password="password" 'bash -s' < script.sh









share|improve this question















I have a bash script that accepts exactly 3 arguments and I have created a web interface in PHP to run this script on a remote server. The user just enters username host and password of the remote server.



I found this command to execute bash script on remote server:



ssh root@host 'bash -s' < script.sh


But this command prompts for a password and also doesn't use any arguments. But I need something that can be run non-interactively.



Something like:



ssh root@host -password="password" 'bash -s' < script.sh






command-line server bash scripts ssh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 16 at 18:33









Zanna

48.1k13120228




48.1k13120228










asked Sep 10 at 11:42









Amarjit Singh

20017




20017







  • 3




    set up ssh keys for passwordless login: askubuntu.com/questions/46930/… If that's not an option, use sshpass
    – ignite
    Sep 10 at 11:49











  • to setup ssh key I have to login to remote server manually. thats what i don't want
    – Amarjit Singh
    Sep 10 at 11:50











  • @AmarjitSingh: You don't need to log in manually to set up ssh keys -- just use ssh-copy-id. Or am I misunderstanding something?
    – Daniel Pryden
    Sep 10 at 17:16










  • @DanielPryden ssh-copy-id command also prompts for the password. But I need a command that is non-interactive.
    – Amarjit Singh
    Sep 10 at 19:15










  • @AmarjitSingh: So you have two servers, and you don't have interactive access to either, but you can run arbitrary shell commands on the first server that contain the root password for the second server in plain text? If you don't have shell access, that implies that this is someone else's server -- do you really want to put the password to another machine in plain text there?
    – Daniel Pryden
    Sep 10 at 20:50












  • 3




    set up ssh keys for passwordless login: askubuntu.com/questions/46930/… If that's not an option, use sshpass
    – ignite
    Sep 10 at 11:49











  • to setup ssh key I have to login to remote server manually. thats what i don't want
    – Amarjit Singh
    Sep 10 at 11:50











  • @AmarjitSingh: You don't need to log in manually to set up ssh keys -- just use ssh-copy-id. Or am I misunderstanding something?
    – Daniel Pryden
    Sep 10 at 17:16










  • @DanielPryden ssh-copy-id command also prompts for the password. But I need a command that is non-interactive.
    – Amarjit Singh
    Sep 10 at 19:15










  • @AmarjitSingh: So you have two servers, and you don't have interactive access to either, but you can run arbitrary shell commands on the first server that contain the root password for the second server in plain text? If you don't have shell access, that implies that this is someone else's server -- do you really want to put the password to another machine in plain text there?
    – Daniel Pryden
    Sep 10 at 20:50







3




3




set up ssh keys for passwordless login: askubuntu.com/questions/46930/… If that's not an option, use sshpass
– ignite
Sep 10 at 11:49





set up ssh keys for passwordless login: askubuntu.com/questions/46930/… If that's not an option, use sshpass
– ignite
Sep 10 at 11:49













to setup ssh key I have to login to remote server manually. thats what i don't want
– Amarjit Singh
Sep 10 at 11:50





to setup ssh key I have to login to remote server manually. thats what i don't want
– Amarjit Singh
Sep 10 at 11:50













@AmarjitSingh: You don't need to log in manually to set up ssh keys -- just use ssh-copy-id. Or am I misunderstanding something?
– Daniel Pryden
Sep 10 at 17:16




@AmarjitSingh: You don't need to log in manually to set up ssh keys -- just use ssh-copy-id. Or am I misunderstanding something?
– Daniel Pryden
Sep 10 at 17:16












@DanielPryden ssh-copy-id command also prompts for the password. But I need a command that is non-interactive.
– Amarjit Singh
Sep 10 at 19:15




@DanielPryden ssh-copy-id command also prompts for the password. But I need a command that is non-interactive.
– Amarjit Singh
Sep 10 at 19:15












@AmarjitSingh: So you have two servers, and you don't have interactive access to either, but you can run arbitrary shell commands on the first server that contain the root password for the second server in plain text? If you don't have shell access, that implies that this is someone else's server -- do you really want to put the password to another machine in plain text there?
– Daniel Pryden
Sep 10 at 20:50




@AmarjitSingh: So you have two servers, and you don't have interactive access to either, but you can run arbitrary shell commands on the first server that contain the root password for the second server in plain text? If you don't have shell access, that implies that this is someone else's server -- do you really want to put the password to another machine in plain text there?
– Daniel Pryden
Sep 10 at 20:50










1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted











Make sure that you have read security considerations




Install sshpass it's a tool for non-interactive ssh password authentication.



sudo apt install sshpass


You can use it like:



sshpass -p 'password' ssh user@server/IP


Then use it like this to run your script with its arguments:



sshpass -p 'password' ssh user@server "bash -s" < ./script.sh arg1 arg2


If it didn't work then what I suggest is to use scp and move your script to remote server, then run your command and remove the script:



sshpass -p 'password' scp script.sh user@server:/tmp/script.sh
sshpass -p 'password' ssh user@server /tmp/script.sh arg1 ar2 arg3
sshpass -p 'password' ssh user@server rm /tmp/script.sh



Security considerations [man sshpass]




First and foremost, users of sshpass should realize that ssh's
insistance on only getting the password interactively is not without
reason. It is close to impossible to securely store the password, and
users of sshpass should consider whether ssh's public key
authentication provides the same end-user experience, while involving
less hassle and being more secure.



The -p option should be considered the least secure of all of
sshpass's options. All system users can see the password in the
command line with a simple "ps" command. Sshpass makes a minimal
attempt to hide the password, but such attempts are doomed to create
race conditions without actually solving the problem. Users of sshpass
are encouraged to use one of the other password passing techniques,
which are all more secure.



In particular, people writing programs that are meant to communicate
the password programatically are encouraged to use an anonymous pipe
and pass the pipe's reading end to sshpass using the -d option.







share|improve this answer






















  • The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
    – pa4080
    Sep 10 at 12:10










  • Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
    – Ravexina
    Sep 10 at 12:10











  • @Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
    – Amarjit Singh
    Sep 10 at 19:12










  • @AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
    – Ravexina
    Sep 10 at 19:41











  • I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
    – Daniel Pryden
    Sep 10 at 20:51










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1073940%2fexecute-bash-script-on-remote-server-non-interactively%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
6
down vote



accepted











Make sure that you have read security considerations




Install sshpass it's a tool for non-interactive ssh password authentication.



sudo apt install sshpass


You can use it like:



sshpass -p 'password' ssh user@server/IP


Then use it like this to run your script with its arguments:



sshpass -p 'password' ssh user@server "bash -s" < ./script.sh arg1 arg2


If it didn't work then what I suggest is to use scp and move your script to remote server, then run your command and remove the script:



sshpass -p 'password' scp script.sh user@server:/tmp/script.sh
sshpass -p 'password' ssh user@server /tmp/script.sh arg1 ar2 arg3
sshpass -p 'password' ssh user@server rm /tmp/script.sh



Security considerations [man sshpass]




First and foremost, users of sshpass should realize that ssh's
insistance on only getting the password interactively is not without
reason. It is close to impossible to securely store the password, and
users of sshpass should consider whether ssh's public key
authentication provides the same end-user experience, while involving
less hassle and being more secure.



The -p option should be considered the least secure of all of
sshpass's options. All system users can see the password in the
command line with a simple "ps" command. Sshpass makes a minimal
attempt to hide the password, but such attempts are doomed to create
race conditions without actually solving the problem. Users of sshpass
are encouraged to use one of the other password passing techniques,
which are all more secure.



In particular, people writing programs that are meant to communicate
the password programatically are encouraged to use an anonymous pipe
and pass the pipe's reading end to sshpass using the -d option.







share|improve this answer






















  • The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
    – pa4080
    Sep 10 at 12:10










  • Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
    – Ravexina
    Sep 10 at 12:10











  • @Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
    – Amarjit Singh
    Sep 10 at 19:12










  • @AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
    – Ravexina
    Sep 10 at 19:41











  • I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
    – Daniel Pryden
    Sep 10 at 20:51














up vote
6
down vote



accepted











Make sure that you have read security considerations




Install sshpass it's a tool for non-interactive ssh password authentication.



sudo apt install sshpass


You can use it like:



sshpass -p 'password' ssh user@server/IP


Then use it like this to run your script with its arguments:



sshpass -p 'password' ssh user@server "bash -s" < ./script.sh arg1 arg2


If it didn't work then what I suggest is to use scp and move your script to remote server, then run your command and remove the script:



sshpass -p 'password' scp script.sh user@server:/tmp/script.sh
sshpass -p 'password' ssh user@server /tmp/script.sh arg1 ar2 arg3
sshpass -p 'password' ssh user@server rm /tmp/script.sh



Security considerations [man sshpass]




First and foremost, users of sshpass should realize that ssh's
insistance on only getting the password interactively is not without
reason. It is close to impossible to securely store the password, and
users of sshpass should consider whether ssh's public key
authentication provides the same end-user experience, while involving
less hassle and being more secure.



The -p option should be considered the least secure of all of
sshpass's options. All system users can see the password in the
command line with a simple "ps" command. Sshpass makes a minimal
attempt to hide the password, but such attempts are doomed to create
race conditions without actually solving the problem. Users of sshpass
are encouraged to use one of the other password passing techniques,
which are all more secure.



In particular, people writing programs that are meant to communicate
the password programatically are encouraged to use an anonymous pipe
and pass the pipe's reading end to sshpass using the -d option.







share|improve this answer






















  • The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
    – pa4080
    Sep 10 at 12:10










  • Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
    – Ravexina
    Sep 10 at 12:10











  • @Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
    – Amarjit Singh
    Sep 10 at 19:12










  • @AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
    – Ravexina
    Sep 10 at 19:41











  • I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
    – Daniel Pryden
    Sep 10 at 20:51












up vote
6
down vote



accepted







up vote
6
down vote



accepted







Make sure that you have read security considerations




Install sshpass it's a tool for non-interactive ssh password authentication.



sudo apt install sshpass


You can use it like:



sshpass -p 'password' ssh user@server/IP


Then use it like this to run your script with its arguments:



sshpass -p 'password' ssh user@server "bash -s" < ./script.sh arg1 arg2


If it didn't work then what I suggest is to use scp and move your script to remote server, then run your command and remove the script:



sshpass -p 'password' scp script.sh user@server:/tmp/script.sh
sshpass -p 'password' ssh user@server /tmp/script.sh arg1 ar2 arg3
sshpass -p 'password' ssh user@server rm /tmp/script.sh



Security considerations [man sshpass]




First and foremost, users of sshpass should realize that ssh's
insistance on only getting the password interactively is not without
reason. It is close to impossible to securely store the password, and
users of sshpass should consider whether ssh's public key
authentication provides the same end-user experience, while involving
less hassle and being more secure.



The -p option should be considered the least secure of all of
sshpass's options. All system users can see the password in the
command line with a simple "ps" command. Sshpass makes a minimal
attempt to hide the password, but such attempts are doomed to create
race conditions without actually solving the problem. Users of sshpass
are encouraged to use one of the other password passing techniques,
which are all more secure.



In particular, people writing programs that are meant to communicate
the password programatically are encouraged to use an anonymous pipe
and pass the pipe's reading end to sshpass using the -d option.







share|improve this answer















Make sure that you have read security considerations




Install sshpass it's a tool for non-interactive ssh password authentication.



sudo apt install sshpass


You can use it like:



sshpass -p 'password' ssh user@server/IP


Then use it like this to run your script with its arguments:



sshpass -p 'password' ssh user@server "bash -s" < ./script.sh arg1 arg2


If it didn't work then what I suggest is to use scp and move your script to remote server, then run your command and remove the script:



sshpass -p 'password' scp script.sh user@server:/tmp/script.sh
sshpass -p 'password' ssh user@server /tmp/script.sh arg1 ar2 arg3
sshpass -p 'password' ssh user@server rm /tmp/script.sh



Security considerations [man sshpass]




First and foremost, users of sshpass should realize that ssh's
insistance on only getting the password interactively is not without
reason. It is close to impossible to securely store the password, and
users of sshpass should consider whether ssh's public key
authentication provides the same end-user experience, while involving
less hassle and being more secure.



The -p option should be considered the least secure of all of
sshpass's options. All system users can see the password in the
command line with a simple "ps" command. Sshpass makes a minimal
attempt to hide the password, but such attempts are doomed to create
race conditions without actually solving the problem. Users of sshpass
are encouraged to use one of the other password passing techniques,
which are all more secure.



In particular, people writing programs that are meant to communicate
the password programatically are encouraged to use an anonymous pipe
and pass the pipe's reading end to sshpass using the -d option.








share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 10 at 20:57

























answered Sep 10 at 11:56









Ravexina

28.3k146897




28.3k146897











  • The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
    – pa4080
    Sep 10 at 12:10










  • Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
    – Ravexina
    Sep 10 at 12:10











  • @Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
    – Amarjit Singh
    Sep 10 at 19:12










  • @AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
    – Ravexina
    Sep 10 at 19:41











  • I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
    – Daniel Pryden
    Sep 10 at 20:51
















  • The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
    – pa4080
    Sep 10 at 12:10










  • Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
    – Ravexina
    Sep 10 at 12:10











  • @Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
    – Amarjit Singh
    Sep 10 at 19:12










  • @AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
    – Ravexina
    Sep 10 at 19:41











  • I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
    – Daniel Pryden
    Sep 10 at 20:51















The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
– pa4080
Sep 10 at 12:10




The password can be read from a file: sshpass -f /path/to/passwordfile ..., reference: askubuntu.com/a/982438/566421
– pa4080
Sep 10 at 12:10












Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
– Ravexina
Sep 10 at 12:10





Yeah, there are other ways too (like using -e to read from $SSHPASS) I didn't add them to keep the answer clean. man sshpass
– Ravexina
Sep 10 at 12:10













@Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
– Amarjit Singh
Sep 10 at 19:12




@Ravexina your solution only works with the known hosts. what about the new hosts that are not added to the list of known hosts.
– Amarjit Singh
Sep 10 at 19:12












@AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
– Ravexina
Sep 10 at 19:41





@AmarjitSingh that's another question. Anyway you can use -o StrictHostKeyChecking=no option ;)
– Ravexina
Sep 10 at 19:41













I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
– Daniel Pryden
Sep 10 at 20:51




I feel like this answer is incomplete without a mention of the (very serious) drawbacks mentioned under Security Considerations in the sshpass manpage.
– Daniel Pryden
Sep 10 at 20:51

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1073940%2fexecute-bash-script-on-remote-server-non-interactively%23new-answer', 'question_page');

);

Post as a guest













































































6 vxe,WfqOtk J hD PPkSYjLl D0 aof8KAeuJtf ZUNw,tk6,Bx,7EMk As yG
81OuT6CKcwSOFR1v4

這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Propositional logic and tautologies

Distribution of Stopped Wiener Process with Stochastic Volatility