-1

I've got two hosts, the first sends command via ssh. The second executes that command and prints output to my xterm:

user@host_one:~# ssh user@host_two "ls"
user@host_two's password:
Document Downloads Music Pictures Videos

I want to get the text of remote command, in my case it's "ls", on my 'host_two'. Then my /etc/bash.bashrc script on 'host_two' will get the text of that command and will do something with it.

For example, my /etc/bash.bashrc will do smth like that:

if [ -z "$PS1" ] # PS1 is my own global variable that is set only if it's a non-interaction session
then
        echo $COMMAND_GOT_VIA_SSH
fi

Then I expect to get smth like this:

user@host_one:~# ssh user@host_two "ls"
user@host_two's password:
ls
  • P.S. Of course /etc/bash.bashrc is kept on my 2nd host. – Damir Gazizov Aug 14 '19 at 08:04
  • Hello, when does the output from host two get processed? When you login? – suspectus Aug 14 '19 at 08:07
  • please edit your post with additional info rather than add a comment – suspectus Aug 14 '19 at 08:08
  • Hint: If you scrap `/etc/bash.bashrc`, no user who has `bash` as login shell can log in anymore, not even root. More precisely, he'll be logged out immediately. – Cyrus Aug 14 '19 at 08:13
  • @Cyrus the line with ` if [ -z "$PS1" ] ` tells to ` bash ` that the next ` then ` statement will execute only on non-interactive mode, which I have got via calling remote command via ssh. – Damir Gazizov Aug 14 '19 at 08:19
  • @Cyrus so my code doesn't scrap `/etc/bash.bashrc` on a regular login session, it applies only for non-interactive session which I get when I run ssh with a remote command. – Damir Gazizov Aug 14 '19 at 08:30
  • if I correctly understand you question, "echo ls; ls"? – Lety Aug 14 '19 at 08:46
  • @Lety yes, I don't know exactly what will happend. Doesn't matter if it will just `echo ls` or `echo ls; ls`, or `ls; echo ls`. The gist is that I want to get the text of remote command onto my 2nd host. – Damir Gazizov Aug 14 '19 at 08:55
  • it is unclear to me what you are trying to do, but, if you want to send a list of command from server one to server two and later read this list from server two script, I suggest to save command in file and send it via scp command to server two, than in your script running con server two you can read file and do what you want, or save command into file on second server lke: "echo ls > /path/on/second/server/fileWithCommand" – Lety Aug 14 '19 at 09:04
  • @Lety I need to get that command via ssh remote command, not via scp or etc. It's too many actions: save command into file, send it via scp, too long time. – Damir Gazizov Aug 14 '19 at 09:31

1 Answers1

1

In host_two place the following code into .bashrc

if [ ! -z "$PS1" ]; then # PS1 interaction session echo $(cat /var/tmp/ssh.command) fi

Run from host_one:

user@host_one:~# ssh user@host_two "echo ls > /var/tmp/ssh.command"

Next time you login into host_two the text of the ssh command will be shown.

AAber
  • 1,562
  • 10
  • 14
  • This is too long command, I'd like to write just `ssh user@host_two ls`. – Damir Gazizov Aug 14 '19 at 10:03
  • mmmh, this works, but if you want to use command inside user .bashrc this is a problem, because .bashrc is executed before command you are sending with ssh. as far as I know this answer is the answer :) but command are read in next session, of course – Lety Aug 14 '19 at 10:13
  • 1
    On host_two create a executable script /bin/s with the following two lines: #!/bin/bash echo "$1" > /var/tmp/ssh.command Then on host_one you can run (shorter): user@host_one:~# ssh user@host_two "s ls" – AAber Aug 14 '19 at 11:01