LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2018, 06:55 AM   #1
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Rep: Reputation: 32
script question syntax issues


I am trying to figure out what I am doing wrong here.
I only want to log those systems I cannot ssh to.

Basically I am locking down systems and I want to see what systems are already locked down. This way I can exclude them from my testing/changes.

List file looks like
node1
node2
node3
...

#!/bin/ksh93
#
#
# Date April 30 2018
# Purpose: Find Linux servers and create list of the ones we cannot ssh to
# read in from this file find_node.ksh
set -x


cat /home/myid/scripts/linux/list_files/linux_svr.lis |while read node
do
echo "$node"
sshout="ssh $node"
if [[ $sshout =~ "You are not allowed to SSH to this server. Good bye...." ]]; then

****DOES NOT LIKE THIS TEST. KEEPS ERRORING OUT AND NOT PLACING HOSTNAMES IN THE FILE.*****

echo "No ssh"
echo "$node"
echo "$node" >>/home/myid/scripts/linux/list_files/ssh_check.out
else
echo "SSH works "
fi

echo " "
done
 
Old 05-01-2018, 07:22 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,018
Blog Entries: 13

Rep: Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462
Recommendations:
  • Add a 'v' to the "set -x" command to enable more verbose output. Therefore "set -xv"
  • Please use [code][/code] tags to enclose your code and/or output.
  • echo out the value of $sshout, and also check the result variable $? - I'm hoping that ksh93 supports similar with bash, but do not know.
  • I'll make a note for the moderator of the Linux-General forum to consider moving this question to Programming where people there have a better concentration towards this type of question.
  • What actual error are you seeing, if any, from your failed test?
 
Old 05-01-2018, 08:21 AM   #3
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Original Poster
Rep: Reputation: 32
Requested out put..
Dont get hung up on the echo statements i changed the output for obvious reasons.


node 1 and node 3 should not allow ssh and be logged to the file.

$./find_node.ksh
cat /home/myid/scripts/linux/list_files/linux_svr.lis |while read node
do
echo "$node"
sshout="ssh $node"
if [[ $sshout = "You are not allowed to SSH to this server. Good bye...." ]]; then

echo "no ssh"
echo "$node"
echo "$node" >>/home/myid/scripts/linux/list_files/ssh_check.out
else
echo "You can ssh to server"
fi

echo " "
done
+ cat /home/myid/scripts/linux/list_files/linux_svr.lis
+ read node
+ echo node1
node1
+ sshout='ssh node1'
+ [[ 'ssh node1' == 'You are not allowed to SSH to this server. Good bye....' ]]
+ echo 'You can ssh to server'
You can ssh to server
+ echo ' '

+ read node
+ echo node2
node2
+ sshout='ssh node2'
+ [[ 'ssh node2' == 'You are not allowed to SSH to this server. Good bye....' ]]
+ echo 'You can ssh to server'
You can ssh to server
+ echo ' '

+ read node
+ echo node3
node3
+ sshout='ssh node3'
+ [[ 'ssh node3' == 'You are not allowed to SSH to this server. Good bye....' ]]
+ echo 'You can ssh to server'
You can ssh to server
+ echo ' '

+ read node
+ echo node4
node4
+ sshout='ssh node4'
+ [[ 'ssh node4' == 'You are not allowed to SSH to this server. Good bye....' ]]
+ echo 'You can ssh to server'
You can ssh to server
+ echo ' '

+ read node



The out file never gets created too.

Last edited by unix1adm; 05-01-2018 at 08:26 AM.
 
Old 05-01-2018, 08:36 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,018
Blog Entries: 13

Rep: Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462Reputation: 3462
That output appears to show that you are able to ssh to nodes 1 and 3. Therefore the test result is incorrect somehow. Edit: Or a guess is that the test result is always failing and resulting in the conclusion (correct or incorrect) that you can ssh to all those servers successfully.

After you assign $sshout, echo it. What does it show? Note that you're comparing a variable to a string. Are you sure the string says what you expect it to say?

Last edited by rtmistler; 05-01-2018 at 08:38 AM.
 
Old 05-01-2018, 08:55 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,935

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
Your correct the out file is never created because the conditional is always false but that does not prove the OP can successfully ssh into the desired node.

To actually execute the command you would use a back tick i.e ` not a single '. The preferred method would be to use $( command ). However, would the actual results return 'You are not allowed to SSH to this server. Good bye....'?
Code:
sshout=$(ssh node1)
echo $sshout

Last edited by michaelk; 05-01-2018 at 08:57 AM.
 
Old 05-01-2018, 08:56 AM   #6
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Original Poster
Rep: Reputation: 32
Correct the test is not correct it it dumping out. If you look I do echo out the variables before and after the test.

I also noticed that the ssh if successful will not exit and the script will just hang on the last successful host.

So I need to figure out how I can do ssh node1 hostname and if it fails then the system name is loged or something like that.



+ cat /home/myid/scripts/linux/list_files/linux_svr.lis
+ read node
+ echo nod1
node1
+ sshout='ssh node1'
+ [[ 'ssh node1' == ~(E)You\ are\ not\ allowed\ to\ SSH\ to\ this\ server.\ \ Good\ bye.... ]] This test is wrong for some reason
+ echo 'You can ssh to server'
You can ssh to server
+ echo ' '

From the command line to confirm i cannot ssh to the box

$ssh node1

You are not allowed to SSH to this server. Good bye....

Connection to node1 closed.

Last edited by unix1adm; 05-01-2018 at 09:08 AM.
 
Old 05-01-2018, 09:07 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,935

Rep: Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859Reputation: 2859
We posted about the same to so you might not be aware what I posted...
 
Old 05-01-2018, 12:51 PM   #8
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Original Poster
Rep: Reputation: 32
After several tries I have found a solution to this.
Hope it can help someone else.

I switched to ksh93 for a shell in the script.

The other add i did was to limit the ssh timeout as some server were hung and did not respond

-o ConnectTimeout=5

--------------------------------------------
#!/bin/ksh93
for node in `cat /home/myid/scripts/linux/linux_svr.lis`
do
sshout=`ssh -o ConnectTimeout=5 $node 'hostname'`
if [[ "$sshout" =~ "You are not allowed to SSH to this server. Good bye...." ]]; then
echo "No ssh to server"
echo "$node"
echo "$node" >>/home/myid/scripts/linux/no_ssh_check.out
else
echo "$node" >>/home/myid/scripts/linux/yes_ssh_check.out
echo "Yes ssh to server"
echo "$sshout"
fi

Last edited by unix1adm; 05-01-2018 at 12:59 PM.
 
Old 05-01-2018, 05:33 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,501

Rep: Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806Reputation: 1806
Quote:
Originally Posted by unix1adm View Post
while read ...
...ssh...
I assume the problem here was the classic "ssh inside the loop reads all the input meant for the loop" thing. The usual solution is adding -n to the ssh call, or redirecting from /dev/null

ssh(1)
Code:
     -n      Redirects stdin from /dev/null (actually, prevents reading from
             stdin).
 
Old 05-01-2018, 05:40 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,082

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
Quote:
Originally Posted by unix1adm View Post
After several tries I have found a solution to this.
Hope it can help someone else.

I switched to ksh93 for a shell in the script.

The other add i did was to limit the ssh timeout as some server were hung and did not respond

-o ConnectTimeout=5

--------------------------------------------
#!/bin/ksh93
for node in `cat /home/myid/scripts/linux/linux_svr.lis`
do
sshout=`ssh -o ConnectTimeout=5 $node 'hostname'`
if [[ "$sshout" =~ "You are not allowed to SSH to this server. Good bye...." ]]; then
echo "No ssh to server"
echo "$node"
echo "$node" >>/home/myid/scripts/linux/no_ssh_check.out
else
echo "$node" >>/home/myid/scripts/linux/yes_ssh_check.out
echo "Yes ssh to server"
echo "$sshout"
fi
seems like you're selective reading here
Please use [code][/code] tags to enclose your code and/or output.
as requested more than once. thanks
 
Old 05-08-2018, 08:51 AM   #11
unix1adm
Member
 
Registered: Oct 2008
Posts: 688

Original Poster
Rep: Reputation: 32
My apologies. I was in a hurry and missed that.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Syntax question for a command within a bash script kaplan71 Linux - Software 11 10-09-2012 07:38 PM
vim syntax highlighting issues sd|| Linux - Newbie 6 04-29-2010 09:54 AM
[SOLVED] Question about bash script syntax musonio Linux - Software 9 09-10-2009 07:28 PM
[SOLVED] Question about sed syntax in bash script musonio Linux - Software 2 08-21-2009 07:17 AM
Shell script syntax question arash8m Programming 4 06-01-2007 08:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration