Bash script connect to remote servers and become root


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script connect to remote servers and become root
# 1  
Old 05-17-2013
Bash script connect to remote servers and become root

Hi,

I need a script that will connect to a list of servers and first sudo to root and then run a couple of commands.
For security reasons, we can't setup ssh keys as root.
Manually I have to login to a server as user and then sudo to root.
It's not possible to use root@servername , because of security restrictions.

This is what I got so far, but the problem is that it's not sudo ing to root:

Code:
#!/bin/bash
HOSTS=$(cat filename)
SCRIPT2="sudo su -"
SCRIPT=" command ; command ; command"

for HOSTNAME in ${HOSTS} ; do
    ssh  ${HOSTNAME} "${SCRIPT2}" "${SCRIPT}"
done

# 2  
Old 05-17-2013
I recall sudo might want a terminal, so ssh -t or ssh -tt. Of course, ssh in on a utility id that has sudo privileges. Usually sudo means you do not need su. If you sudo bash you can send any number of commands, so you do not need to maintain remote scripts.
# 3  
Old 05-18-2013
Quote:
Originally Posted by DGPickett
I recall sudo might want a terminal, so ssh -t or ssh -tt. Of course, ssh in on a utility id that has sudo privileges. Usually sudo means you do not need su. If you sudo bash you can send any number of commands, so you do not need to maintain remote scripts.
sudo isn't possible
# 4  
Old 05-18-2013
sudo works without terminal if there is NOPASSWD: in sudoers. Please check which command(s)!
But it is either
Code:
sudo command; sudo command

or
Code:
sudo su - -c 'command; command'

where the optional - reads root's profile.
# 5  
Old 05-22-2013
Quote:
Originally Posted by MadeInGermany
sudo works without terminal if there is NOPASSWD: in sudoers. Please check which command(s)!
But it is either
Code:
sudo command; sudo command

or
Code:
sudo su - -c 'command; command'

where the optional - reads root's profile.

sudo su - is not allowed
I understand that we can avoid these things, by setting up ssh keys or the sudoers file, but these options are not allowed

basiclly I want to do these steps, but in a script:
login to the Unix server as my own userid
ssh to remote server
sudo to root
execute a couple of commands
exit

On google I couldn't find something similar,
but I assume I am not the first person who came across this?

we have have environments where we can login to a main server as root ,
execute scripts on remote servers as root, because SSH keys have been configured, but not for this environment
# 6  
Old 05-23-2013
You can use an expect script for interactive prompt handling.
Code:
man expect

This is probably the least secure method, because the passwords are stored in your script. At least read-protect it for others!
# 7  
Old 05-23-2013
Yes, ssh is more secure with PPKey not password, as well as allowing simpler scripting. Once in as not root user with a controlling terminal (-tt), accessing root with any interactive commands is fine, but once again, you may need expect or something like it to send the root password, or sudo so you do not need to use and expose that password.

You could write your own set-uid root compiled program to let just your trusted id or group run just scripts in a special directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Connect direct - SFTP - List of servers that I can connect

Greetings Experts, I am working for a bank client and have a question on connect-direct and SFTP. We are using Linux RedHat servers. We use connect-direct to transfer (NDM) files from one server to another server. At times, we manually transfer the files using SFTP from one server to another... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Shell Programming and Scripting

Script to connect to remote and sendmail.

Hello, Kindly guide. SendMail function on my script is not working, but it works manually. Any better way to handle the script is appreciable. #!/bin/sh GetHostConnection() { truncate --size 0 /home/web/for_mail.out while read -r lines ; do ip=`echo $lines | awk '{print... (9 Replies)
Discussion started by: sadique.manzar
9 Replies

3. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

4. Shell Programming and Scripting

Shell script to connect to multiple ssh servers

Hello, I have access to several linux servers (mostly centos based) located in a DC in another country. from day to day I need to login to each of them to do some work (they dont have gui/window manager installed, I work only from console), or even to just do a check like df -h for disc usage.... (3 Replies)
Discussion started by: MaRiOsGR
3 Replies

5. Shell Programming and Scripting

bash script to execute a command remote servers using ssh

Hello, I am running into few issues, please suggest me what I am missing. I am running this script on a linux host. Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo... (8 Replies)
Discussion started by: bobby320
8 Replies

6. Shell Programming and Scripting

Need to run a bash script that logs on as a non-root user and runs script as root

So I have a script that runs as a non-root user, lets say the username is 'xymon' . This script needs to log on to a remote system as a non-root user also and call up a bash script that runs another bash script as root. in short: user xymon on system A needs to run a file as root user and have... (2 Replies)
Discussion started by: damang111
2 Replies

7. AIX

Connect HMC to remote servers

I'm trying to connect a few servers in different remote locations to my HMC. I added static IPs to the Service Processor through ASM, and did all the necessary network configurations, then connected those servers to our switch. Now my questions are: - in our site, do I need to connect these... (4 Replies)
Discussion started by: Dardeer
4 Replies

8. Shell Programming and Scripting

Connect two servers in shell script

Hello all, I know that is a question which has made too many times, and I have been looking into the threads but all posted was not sucessfully for me so... I need a shell script which connect two unix servers, but NOT using ssh... Is there another way to do it? I've tried ssh but it didn't... (1 Reply)
Discussion started by: Geller
1 Replies

9. Shell Programming and Scripting

Shell/perl script to connect to different servers in single login in teradata

Hi, I want to write a shell script to compare two tables in teradata.these tables are present on different servers. I want to connect to both servers in single login in order to fetch and compare the data in one go. Thanks (1 Reply)
Discussion started by: monika
1 Replies

10. Shell Programming and Scripting

need script to connect sftp servers

Dear friends, i need to connect sftp server from my home directory using script . Please can anyone help me on this. (1 Reply)
Discussion started by: kittusri9
1 Replies
Login or Register to Ask a Question