Login to remote host and execute commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Login to remote host and execute commands
# 1  
Old 08-13-2013
Login to remote host and execute commands

Hi,
i want to write script where it will login into 50 hosts and if login is successful it print message "login to host1 is
successful" if not it should print message "Not able to login to host1". once connection to the host is succesful it should fire df command to check filesystem if df is stuck somewhere
then it should print message "DF got stuck otherwise print message "DF is successful"
Please advice how should i achive this

i used below approach
Code:
#!/usr/bin/ksh
for i in `cat host.txt`
do
        ssh $i
if [[ $? -eq 0 ]] then
echo "Login to $i is succesful"
        df
                if [[ $? -eq 0 ]] then
                echo "Df is successful"
                else
                echo "DF got stucked"
                fi
else
echo "Not able to Login to $i"

fi
done

Thanks

Last edited by DukeNuke2; 08-13-2013 at 09:37 AM..
# 2  
Old 08-13-2013
Ok. Assuming that ssh is listening on port 22 in each of those hosts, and that the file hosts.txt contains a list of IP addresses or hostnames this is what I'd do:
Code:
#!/usr/bin/ksh
while read line
do
ssh $line <<EOF # Use a 'here document' to pass a sequence of commands to the ssh connection
if [[ $? -eq 0 ]] then
	echo "Login to $line was succesful"
	df
		if [[ $? -eq 0 ]] then
			echo "df is successful"
		else
			echo "DF got stucked"
		fi
else
	echo "Not able to Login to $i"
fi
exit
EOF
done < hosts.txt

Let me know if this works for you. Good luck.
This User Gave Thanks to gacanepa For This Post:
# 3  
Old 08-17-2013
Hi,

Thanks for your help.

But i m getting below error and systems hangs for 2-5 seconds and then it runs the commands successfully.

Code:
Pseudo-terminal will not be allocated because stdin is not a terminal.

Also i would like to send the echo statments output to some txt file and then email should be send to user like below.

example
Code:
"Login to $line is successfult"
"DF is executed successfulyy at $line"

can u please help.

Thanks

Last edited by Scott; 08-17-2013 at 07:05 PM.. Reason: Please use code tags
# 4  
Old 08-17-2013
Please use code tags as required by forum rules!

Read man ssh. Done that, you may want to use the -T option to ssh. The "few-second-hang" is not a hang but the duration of the remote login process.

Use output redirection immediately after input redirection to create the log file. Then, mail the log file to your address list.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute commands on remote server using expect script?

I need to copy python script file to around 100 servers using expect script. 1. Copy script to my user home first(/home/myhome) on each remote server 2. change permissions on copied file to 766. 3. sudo to appuser1 account on remote server. copy script file from my user home to /usr/bin/... (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

Execute command on remote host via ssh

How should i make the following code working #!/bin/bash INPUTFILE="test.txt" while read STRING; do IP=`host -t A $STRING | awk '{print $NF}'` HOSTNAME=`ssh -oPasswordAuthentication=no -oStrictHostKeyChecking=no $IP "hostname"` echo $HOSTNAME > out.txt done < $INPUTFILE At this moment while... (3 Replies)
Discussion started by: urello
3 Replies

3. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

4. Shell Programming and Scripting

Not able to execute the file in remote host using except utility

I am automating the SFTP keys setp process: So i created the expect script for controlling the output of shell below is my main code: #!/usr/bin/expect set fd set password close $fd set df set app close $df spawn ssh servername << pb Pb file: set df set app close $df (4 Replies)
Discussion started by: Manoj Bajpai
4 Replies

5. Shell Programming and Scripting

execute remote commands with rsh

Hi, I earlier determined I cannot use FTP to execute remote commands on a server. My problem, I need to use a second server to get/put files via ftp onto my primary server and various tertiary servers. my server(A) ---> server (B) ----> server blah(c), server balh(C) I cannot directly... (1 Reply)
Discussion started by: mcclunyboy
1 Replies

6. Shell Programming and Scripting

running commands to remote host from centralized host

Gurus/Experts We have a centralized UNIX/Solaris server from where we can actually ssh to all other UNIX/Solaris servers...I need to write a script that reside on this centerlized server and do FileSystem monitoring (basically run df -h or -k) of other remote servers and then send an email to me... (6 Replies)
Discussion started by: anjum.suri
6 Replies

7. Shell Programming and Scripting

ssh to remote host and execute command

Hi, could anyone please tell me how to ssh to remote host foo and execute command on it and print the result on local host? Thanks, Paresh (1 Reply)
Discussion started by: masaniparesh
1 Replies

8. Shell Programming and Scripting

Run a shell script from one host which connext to remote host and run the commands

I want to write a script which would run from one host say A and connect to other remote host B and then run rest of commands in that host. I tried connecting from A host to B with SSH but after connecting to host B it just getting me inside Host B command prompt. Rest of the script is not running... (6 Replies)
Discussion started by: SN2009
6 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

10. UNIX for Dummies Questions & Answers

Sending Commands to a Remote Host

I am trying to write a script that logs into an SMTP server and authenticates as a user or verifies that a user exists. I can do all this from the command line but I don't know how to write a script to do this for me, I login and then the script stops, I'm sure this is some basic principle. ... (3 Replies)
Discussion started by: safetytrick
3 Replies
Login or Register to Ask a Question