0

Pseudo-terminal will not be allocated because stdin is not a terminal

I have been using similar script as of above for long. I login on remote devices, have some commands ran on them and then logout. I use telnet and ssh both for remote login. so far all the commands are being echo-ed to remote terminals and the output being saved to some log files. what I need is to be able to interpreter the output generated by the remote terminal. Consider following a command sent to remote terminal after successful login via SSH/Telnet:

echo "show system date"

after running above command, I want to store the output generated by remote machine in a variable and perform some if/else statements. So far, I didn't have any kind of success. can someone please guide me how I can save the output generated by remote terminal in a variable?

Ibraheem
  • 149
  • 12
  • 1
    And `var="$(ssh user@somehost "your command")"` doesn't store the output in `$var`? What does it store instead? What happens? – ghoti Feb 01 '18 at 20:55
  • It seems it does store the output but that doesn't solve my problem, It stores the output after logging out. I want to stay logged in, get output of commands and perform actions based on that output before logging out of the terminal. – Ibraheem Feb 02 '18 at 07:22
  • Perhaps you want to pipe the output of your ssh through a script or function rather than redirecting. `ssh user@host somecommand | localcommand`. Or if you're suggesting that you want to perform actions *within the ssh session* then perhaps you're looking for [expect](https://en.wikipedia.org/wiki/Expect). Or even more likely, you should re-examine your approach. – ghoti Feb 02 '18 at 07:30
  • It does same with pipe, outputs at the end of session, I want to perform actions on command by command basis. – Ibraheem Feb 02 '18 at 07:33

1 Answers1

1

If ansible is an option you could register the output and based on it do some actions, for example:

- name: show system date
  command: date
  register: date

- debug: msg="{{ date.stdout }}"

You could read more about ansible and the conditionals here: http://docs.ansible.com/ansible/latest/playbooks_conditionals.html

nbari
  • 25,603
  • 10
  • 76
  • 131