Hoi everybody,
I am currently having a problem with sending commands over SSH via command line to a server. To have an easy example, I always send the "pwd" command - which should return the current folder you are in.
I build the initial SSH connection successfully with the following command:
sshg3 user@server#port 'pwd'which connects to the server and short the home folder.
Afterwards, I use sudo su - user2 - because the user has more rights as user (needed for specific tasks):
sshg3 user@server#port "echo pwd|sudo su - user2"Here, however, if I want to concat multiple commands after one another, I need to insert "" - otherwise it does not work:
sshg3 user@server#port "echo ""pwd;pwd""|sudo su - user2"If I have no or only one ", the result is:
pwd
-bash: line 1: {homefolder}: is a directoryNow, afterwards using lftp I need to upload data to that server using yet another user. Using another pipe, I get that to work with a single command the following way:
sshg3 user@server#port "echo ""echo pwd|lftp -u user3 -p 1234 server2""|sudo su - user2"And now comes the problem: Sending multiple commands to the second server does not work. If I use the same pattern I used before (""), I get this result:
sshg3 user@server#port "echo ""echo ""pwd;pwd""|lftp -u user3 -p 1234 server2""|sudo su - user2"This prints:
echo pwd
bash: pwd|lftp -u user3 -p 1234 server2: command not foundThe most part I got by using the answer in [this][1] thread - but now I am stuck.
Can someone help me with this?
32 Answers
I am not exactly sure what you are trying to do on the remote server. It looks like you want to execute commands which depend on the response of other commands (like pwd, which prints the working directory). Possibly you also want to use a password ("echo pwd") for su, I am not sure which.
I would suggest one of the following approaches:
Write a script on the remote server that does what you want. Execute that script via
ssh. Give the script additional information, like passwords, as arguments when you call it.If you can't store the script on the remote server permanently: Write the script anyway, and then pipe and execute the whole script through
ssh.Use
expectto script a complicated interactive session viassh, where you call various programs, which in turn expect user input.
In any case, I don't think you'll get far messing around with echo.
I think you've got an XY problem. If you could tell us what you're trying to achieve rather than how you're trying to achieve it, then we could probably help out...
In the meantime, as I can't help with your original problem, let's address the following:
sshg3 user@server#port "echo ""echo ""pwd;pwd""|lftp -u user3 -p 1234 server2""|sudo su - user2"Bash will break this into arguments based on unquoted whitespace, thus the arguments are:
sshg3user@server#port"echo ""echo ""pwd;pwd""|lftp -u user3 -p 1234 server2""|sudo su - user2"
After removal of the quoting from argument 3, it will look like this: (I don't think that's what you intended)
echo echo pwd;pwd|ltfp -u user3 -p 1234 server2|sudo su - user2You can confirm this with the following snippet.
Put your command into the array with X=( YOUR_COMMAND ):
$ X=(sshg3 user@server#port "echo ""echo ""pwd;pwd""|lftp -u user3 -p 1234 server2""|sudo su - user2")
$ for i in "${X[@]}"; do echo "${i}"; done
sshg3
user@server#port
echo echo pwd;pwd|lftp -u user3 -p 1234 server2|sudo su - user2The cleaned version of argument 3 will be passed to the remote system and will (probably) be interpreted like so:
Command 1 arguments:
echoechopwd
Command 2 arguments: (
stdoutwired to Command 3'sstdinbecause of pipe /|)pwd
- Command 3 arguments: (
stdoutwired to Command 4'sstdin)lftp-uuser3-p1234server2
- Command 4 arguments:
sudosu-uuser2
Again, you can confirm this by logging in and running the cleaned third line from the remote shell.
There are a few things to note about this:
- You probably didn't mean to echo
echo pwdto stdout (which is what Command 1 will do) lftpwon't expect an absolute path (and nothing else) to be given onstdinsuvery likely won't expect whatever the output oflftpis to be given onstdin...suwill invokeuser2's shell.