Read several variables from command output via SSH


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Read several variables from command output via SSH

Hi Folks,

I'm currently trying to read several values into different variables.
Actually, what I'm doing works, but I get an error message.

My attempts are:
Code:
read strCPROC strIPROC strAPROC <<<$(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F \"configurable_sys_proc_units installed_sys_proc_units curr_avail_sys_proc_units\"")

Code:
read strCPROC strIPROC strAPROC < <(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F \"configurable_sys_proc_units installed_sys_proc_units curr_avail_sys_proc_units\"")

and both lead to the error message:
Code:
stty: standard input: Inappropriate ioctl for device

Can someone maybe help me to avoid this or maybe have another idea how to achieve that?

Thanks and kind regards,
Kaede
# 2  
ssh calls stty to do things like turn off/on echo. When the terminal stdin device is not what ssh expected you get the stty error. Try simply executing the ssh command making sure it can use the controlling terminal device with stty and using ssh keys:
Code:
ssh me@somplace 'my command goes here ' > output_file
# output_file now has your result it in it,  so you can read the file to get your variables.

I may be missing something but this has happened to me before and my workaround was okay. Still is.

One caveat - if you are running under ksh (does appear to be in this case) then be sure there are no stty commands in the current directory's .kshrc, that also causes this problem.
# 3  
try:
Code:
read strCPROC strIPROC strAPROC <<<$(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F 'configurable_sys_proc_units installed_sys_proc_units curr_avail_sys_proc_units'"</dev/null)

# 4  
Quote:
Originally Posted by jim mcnamara
ssh calls stty to do things like turn off/on echo. When the terminal stdin device is not what ssh expected you get the stty error. Try simply executing the ssh command making sure it can use the controlling terminal device with stty and using ssh keys:
The problem with that is, that I run these SSH commands in a while loop and the while loop breaks, when I don't use at least the -n parameter with ssh.
Here is the code block where I get the error.
Code:
24 for HMC in ${strHMCLIST}
25 do
26   strNILIST=""
27
28   ssh -n -T hscroot@$HMC "lssyscfg -r sys -F 'name type_model serial_num lpar_proc_compat_modes'" > .outlist.tmp
29   while read N T S P
30   do
31     strNAME=$N
32     strIDENT=$T'*'$S
33     strNILIST=$strNILIST$strNAME" "$strIDENT"\n"
34     strCPUARCH=${P##*,}
35
36     ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F 'configurable_sys_proc_units installed_sys_proc_units curr_avail_sys_proc_units'" > .outhw.tmp
37     read strCPROC strIPROC strAPROC < .outhw.tmp
38
39     ssh -n -T hscroot@$HMC "lshwres -r mem -m $strIDENT --level sys -F 'configurable_sys_mem installed_sys_mem pend_avail_sys_mem'" > .outhw.tmp
40     read strCRAM strIRAM strARAM < .outhw.tmp
41
42     echo -e "$strNAME,$T,$S,$strCPUARCH,$strIPROC,$strCPROC,$strAPROC,$strIRAM,$strCRAM,$strARAM" >> $strOUTPUTFILEHW
43   done < .outlist.tmp
44
45 done

I'm using the Cygwin bash
GNU bash, version 4.1.17(0)-release (i686-pc-cygwin)

Quote:
Originally Posted by vgersh99
try:
Code:
read strCPROC strIPROC strAPROC <<<$(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F 'configurable_sys_proc_units installed_sys_proc_units curr_avail_sys_proc_units'"</dev/null)

Thanks, but it didn't help either.
# 5  
Your code snippet does not look like what you showed in post #1. Where do you get the error? Is it issued by ssh or by lshwres? Try changing -T to -t.


Split the problematic code into smaller pieces for a step by step trouble shooting; e.g. run the ssh command alone and check its output, then read a single value from a simple command into a single variable, then go on to more complex stuff. Is the IFS variable set correctly?
This User Gave Thanks to RudiC For This Post:
# 6  
Quote:
Originally Posted by RudiC
Your code snippet does not look like what you showed in post #1. Where do you get the error? Is it issued by ssh or by lshwres? Try changing -T to -t.
The code snippet is already adjusted to jim mcnamara's suggestion, which unfortunately didn't solve the problem.
The error is caused by the ssh
-T and -t are doing the absolutely opposite thing.
Anyway, when I use -t, I only get another error.
Code:
Pseudo-terminal will not be allocated because stdin is not a terminal.

Which I can relate to.


Quote:
Originally Posted by RudiC
Split the problematic code into smaller pieces for a step by step trouble shooting; e.g. run the ssh command alone and check its output, then read a single value from a simple command into a single variable, then go on to more complex stuff. Is the IFS variable set correctly?
Running every command for its own on the shell itself works perfectly, the codeblock itself also works as intended, I only get this error message from the ssh.
Running the ssh command on shell is also fine
Code:
# ssh -n -T hscroot@X.X.X.X "lshwres -r proc -m XXX --level sys -F 'configurable_sys_proc_units installed_sys_proc_units curr_avail_sys_proc_units'"; echo $?
16.0 16.0 10.0
0

EDIT:
IFS is set to single space, which also works fine.

Last edited by NKaede; 4 Weeks Ago at 05:45 AM..
# 7  
Does lshwres interact directly with the terminal? Are there any options to disable this? I could sort of replicate the situation with stty:
Code:
ssh -n -t user@remotehost "hostname; stty"
Pseudo-terminal will not be allocated because stdin is not a terminal.
remotehostname
echo $?
0

No error!


Looks like -n and -t are mutually exclusive (which seems logical if reading the man page). Try dropping the -n:
Code:
ssh -t user@remotehost  "hostname; stty"
remotehostname
speed 38400 baud;
.
.
.
echo $?
0

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #198
Difficulty: Easy
Bash ranked in the top 20 programming languages according to the TIOBE Index for October 2019.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

2. Shell Programming and Scripting

Psql output into array and read 2 fields into different variables

Hello Just edited the entry to make it easier to understand what i want How can i achieve this: GOAL: read 2 field from a table with PSQL result of this PSQL command is this INSTALLEDLANG=$(su - postgres -c "psql -A -t -q -c -d ${DBNAME} -t -c 'SELECT code, iso_code from res_lang'") ... (0 Replies)
Discussion started by: winston6071
0 Replies

3. Shell Programming and Scripting

Using a find command in ssh but using local variables?

I have a script like this (Yes, I know the DAY6 number isn't right - I'm just testing at this point): DAY0=`date -I` DAY1=`date -I -d "1 day ago"` DAY6=`date -I -d "2 days ago"` if then ssh root@synology1 nohup rm -rf "/volume1/Fileserver/$DAY6" fi I've tested the line to remove the... (5 Replies)
Discussion started by: Orionizer
5 Replies

4. Shell Programming and Scripting

Format output from command from variables

Hi , I have below command to that outputs from variables.. command: echo $INSTANCE $DATAB $status $TSLastBackup| awk '{printf("%-8s %-8s \t \n",$1,$2,$3,$4)}' | tee $LOGF the ouput is now: INSTANCE DATABSE BACKUP_STATUS BACKUPTIMESTAMP ------- -------- -------- ... (1 Reply)
Discussion started by: db2_usd
1 Replies

5. Shell Programming and Scripting

Environment Variables in text file and read command

I cannot get the following substitution ($ORACLE_SID) to work: The variable ORACLE_SID is set to wardin my environment. It has been exported. I have a text file called test.dat: /u07/oradata/${ORACLE_SID}/extab/finmart/summit/ps_voucher_line_crnt_ex.dbf... (2 Replies)
Discussion started by: bradyd
2 Replies

6. Shell Programming and Scripting

how to write 2 variables while using read command

Hello All i have input files contains 2 values as following 20-Oct-09 Z59408009 20-Oct-09 Z59423060 and i am using the following script cat /home/or/input.txt | awk '{print $2}' >log count=0 while read line; do count=$(( count + 1 )) echo "UPDATE SAT_JRLTRT SET AVT='X' WHERE... (6 Replies)
Discussion started by: mogabr
6 Replies

7. Fedora

ssh command to read server resources

what is the ssh command to read my server resources, like system operator, Ram installed, CPU etc....? (12 Replies)
Discussion started by: dan8354544
12 Replies

8. Shell Programming and Scripting

Interpreting Logicals/Environment Variables using the read command

Hi All I have something that from the outset seems really trivial but in practice is not quite working. I have the following code sample in my shell script which illustrates the problem echo "enter home directory" read home mkdir $home/newdir The user then enters a logical $HOME... (3 Replies)
Discussion started by: kingpin2502
3 Replies

9. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

10. Shell Programming and Scripting

Cannot read variables after ssh with rc file (KSH)

Greetings all, I'm currently making use of the $HOME/.ssh/rc file to launch an automated shell script immediately after the user has been verified through ssh. The current problem that I'm facing now is that I am unable to use the "read" command anymore... seems like the "read" statements are... (0 Replies)
Discussion started by: rockysfr
0 Replies

Featured Tech Videos