Saturday 12 November 2016

Run a local script remotely with sudo

Shell scripts are an indispensable part of any system administrators toolkit. Generally in most environments there would be a bastion host which would have connectivity to the rest of the hosts in the infrastructure.
So, you've created a couple of good scripts to automate some routine tasks but copying the script to every server & then logging into that server & executing the command or copying the script & then running the script over ssh from the bastion hosts can get time consuming thereby making them less efficient.

The conventional & common way would be to copy the script & then run it over ssh like the following example:

[sahil@cserver ~]$ scp a.sh cwork:~
sahil@cwork's password:
a.sh                                                                                                                                                        100%   46     0.0KB/s   00:00
[sahil@cserver ~]$ ssh cwork 'sudo ./a.sh'
sahil@cwork's password:
sudo: sorry, you must have a tty to run sudo
[sahil@cserver ~]$ ssh -tt cwork 'sudo ./a.sh'
sahil@cwork's password:

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009717d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20971519     9972736   8e  Linux LVM

Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 9135 MB, 9135194112 bytes, 17842176 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Nov 11 15:04:44 cwork systemd: Created slice user-1001.slice.
Nov 11 15:04:44 cwork systemd: Starting Session 546 of user sahil.
Nov 11 15:04:44 cwork systemd: Started Session 546 of user sahil.
Nov 11 15:04:44 cwork systemd-logind: New session 546 of user sahil.
Nov 11 15:04:44 cwork systemd-logind: Removed session 546.
Nov 11 15:04:53 cwork systemd: Created slice user-1001.slice.
Nov 11 15:04:53 cwork systemd: Starting Session 547 of user sahil.
Nov 11 15:04:53 cwork systemd: Started Session 547 of user sahil.
Nov 11 15:04:53 cwork systemd-logind: New session 547 of user sahil.
Nov 11 15:04:53 cwork kernel: end_request: I/O error, dev fd0, sector 0
Connection to cwork closed.


But what if I don't wan to copy it over & run the script locally & execute the script content on the remote host.
Here's how to get it done:

[sahil@cserver ~]$ ssh -tt  cwork 'sudo  bash -s ' < ./a.sh
sahil@cwork's password:
#!/bin/bash

fdisk -l

tail /var/log/messages
[root@cwork sahil]# #!/bin/bash
[root@cwork sahil]#
[root@cwork sahil]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009717d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20971519     9972736   8e  Linux LVM

Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/centos-root: 9135 MB, 9135194112 bytes, 17842176 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@cwork sahil]#
[root@cwork sahil]# tail /var/log/messages
Nov 11 14:41:09 cwork systemd: Created slice user-1001.slice.
Nov 11 14:41:09 cwork systemd: Starting Session 540 of user sahil.
Nov 11 14:41:09 cwork systemd: Started Session 540 of user sahil.
Nov 11 14:41:09 cwork systemd-logind: New session 540 of user sahil.
Nov 11 14:41:09 cwork systemd-logind: Removed session 540.
Nov 11 14:41:19 cwork systemd: Created slice user-1001.slice.
Nov 11 14:41:19 cwork systemd: Starting Session 541 of user sahil.
Nov 11 14:41:19 cwork systemd: Started Session 541 of user sahil.
Nov 11 14:41:19 cwork systemd-logind: New session 541 of user sahil.
Nov 11 14:41:19 cwork kernel: end_request: I/O error, dev fd0, sector 0
[root@cwork sahil]# ^CKilled by signal 2.


The only flaw here is that you have to press ctrl+c to terminate the script & logout of the remote server.

But I was able to come up with a workaround for this. I just added an exit command in my script to log out once the script gets executed.

Here is the script content:

[sahil@cserver ~]$ cat a.sh
#!/bin/bash

fdisk -l

tail /var/log/messages

exit
[sahil@cserver ~]$

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...