ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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 ' '
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?
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....'?
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....
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.