{"id":287,"date":"2013-03-11T00:04:43","date_gmt":"2013-03-11T00:04:43","guid":{"rendered":"https:\/\/www.nicktailor.com\/?p=287"},"modified":"2022-10-21T11:59:54","modified_gmt":"2022-10-21T11:59:54","slug":"how-to-pass-a-password-to-su-as-a-variable-in-script","status":"publish","type":"post","link":"https:\/\/nicktailor.com\/tech-blog\/how-to-pass-a-password-to-su-as-a-variable-in-script\/","title":{"rendered":"How to pass a &#8220;password&#8221; to su as a variable in a script"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><strong>How to pass a &#8220;password&#8221; to su as a variable in script and execute tasks to an array of hosts<\/strong><\/span><\/p>\n<p>Some of you may work for organizations that do access control for linux servers. In which case they do not use ssh keys for root, and are still doing the unthinkable allowing the use of password authentication.<\/p>\n<p>So this means you have to log into a server and the \u201csu \u2013\u201c to root before you can execute commands, and if you have an array of servers this could be tedious and time consuming. I was told by everyone that you can\u2019t pass a \u201cpassword\u201d as a variable in script to su, as it\u2019s not allowed.<\/p>\n<p>Guess what\u2026that\u2019s a lie, because I\u2019m going to show you how to do it securely.<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li>So you need to install something called expect on all your servers. This tool is used for interactive testing of scripts. It makes the script pass human typing where needed. You can pass variables to this and use it as a wrapper inside another script.\n<ol>\n<li><em>\u201cYum install expect\u201d on debian \u201capt-get install expect\u201d<\/em><\/li>\n<\/ol>\n<\/li>\n<li>Now what you want to do is log into your server as the user not root, and inside the home directory you want to setup the following to scripts\n<ol>\n<li>Create a file called gotroot<\/li>\n<li><em>Vi \u00a0\u00a0gotroot<\/em>, add the following below and save.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>The script below is a wrapper script, that you will use inside another bash script later in this tutorial.<\/p>\n<p>Usage would be<\/p>\n<p><em>.\/gotroot &lt;user&gt; &lt;host&gt;&lt;userpass&gt;&lt;rootpass&gt;<\/em><\/p>\n<p>These arguments will then get passed to the remote host and it will execute the send commands below in our case \u201cls \u2013al\u201d, and then once its done it will exit and log out of the server and return you to the host you started from. This script does not account for ssh fingerprinting, so you will need to ensure you fingerprint your user to each server before using this script. I will add fingerprinting in future, just got lazy.<\/p>\n<p>What I like to do is..I will write a bash script that it going to do a bunch tasks, scp it over all the servers as my user, then comment out the ls \u2013al section and uncomment the section where you can tell to run the bash script. This will then log in to the server and su to root, execute your bash script, exit and log\u00a0 out.<\/p>\n<p><em>========================<\/em><br \/>\n<em> #!\/usr\/bin\/expect -f<\/em><br \/>\n<em> set mypassword [lindex $argv 2]<\/em><br \/>\n<em> set mypassword2 [lindex $argv 3]<\/em><br \/>\n<em> set user [lindex $argv 0]<\/em><br \/>\n<em> set host\u00a0 [lindex $argv 1]<\/em><br \/>\n<em> spawn ssh -tq $user@$host<\/em><\/p>\n<p><em>########################################################<\/em><\/p>\n<p><em>#this section is only needed if you are NOT using ssh keys<\/em><br \/>\n<em> #expect &#8220;Password:&#8221;<\/em><br \/>\n<em> #send &#8220;$mypassword\\r&#8221;<\/em><br \/>\n<em> #expect &#8220;$ &#8220;<\/em><\/p>\n<p><em>#########################################################<\/em><\/p>\n<p><em>send &#8220;su -\\r&#8221;<\/em><br \/>\n<em> expect &#8220;Password:&#8221;<\/em><\/p>\n<p><em>send &#8220;$mypassword2\\r&#8221;<\/em><br \/>\n<em> expect &#8220;$ &#8220;<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>#this will execute command on the remote host<\/em><\/p>\n<p><em>send &#8220;ls -al\\r&#8221;<\/em><br \/>\n<em> expect &#8220;$ &#8220;<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>#this will execute script you want to run on remote host<\/em><br \/>\n<em> #send &#8220;\/home\/nicktailor\/script.pl\\r&#8221;<\/em><br \/>\n<em> #expect &#8220;$ &#8220;<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>#this command will exit the remote host<\/em><br \/>\n<em> send &#8220;exit\\r&#8221;<\/em><br \/>\n<em> send &#8220;exit\\r&#8221;<\/em><\/p>\n<p><em>interact<\/em><\/p>\n<p>==============================================<\/p>\n<p><strong><span style=\"text-decoration: underline;\">How to wrap this script so it will do any array of hosts within bash.<\/span><\/strong><\/p>\n<ol>\n<li>Create a file called host\n<ol>\n<li>Vi \u00a0hosts and the following below in it.<\/li>\n<li>Also created a file called<em> logs.txt \u201ctouch logs.txt\u201d<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>This script will all you to use the above script as a wrapper and it will go and execute the command you want from got root to each host in the servers variable listed below.<\/p>\n<p>In addition you it will prompt you the user name and root pass, and will not show he passwords you enter, it will prompt you the same way if you were to do \u201csu \u2013\u201c. It will then take those credentials and use it for each host securely, the passwords will not show up in logs or history anywhere, as some security departments would have issues with that.<\/p>\n<p>So you simply type \u201c.\/hosts\u201d<\/p>\n<p>It will prompt you for whatever it requires to continue. Just be sure that you add the array of hosts you want to execute the tasks on, and that you have setup a ssh fingerprint as your user first. Expect scripts are extremely easy to learn, once you play with this.<\/p>\n<p>=======================================<br \/>\n<em>#!\/bin\/bash<\/em><\/p>\n<p><em>#########################<\/em><\/p>\n<p><em>#some colour constants #<\/em><\/p>\n<p><em>#########################<\/em><\/p>\n<p><em>\u00a0<\/em><\/p>\n<p><em>CLR_txtblk=&#8217;\\e[0;30m&#8217; # Black &#8211; Regular<\/em><br \/>\n<em>CLR_txtred=&#8217;\\e[0;31m&#8217; # Red<\/em><br \/>\n<em>CLR_txtgrn=&#8217;\\e[0;32m&#8217; # Green<\/em><br \/>\n<em>CLR_txtylw=&#8217;\\e[0;33m&#8217; # Yellow<\/em><br \/>\n<em>CLR_txtblu=&#8217;\\e[0;34m&#8217; # Blue<\/em><br \/>\n<em>CLR_txtpur=&#8217;\\e[0;35m&#8217; # Purple<\/em><br \/>\n<em>CLR_txtcyn=&#8217;\\e[0;36m&#8217; # Cyan<\/em><br \/>\n<em>CLR_txtwht=&#8217;\\e[0;37m&#8217; # White<\/em><br \/>\n<em>CLR_bldblk=&#8217;\\e[1;30m&#8217; # Black &#8211; Bold<\/em><br \/>\n<em>CLR_bldred=&#8217;\\e[1;31m&#8217; # Red<\/em><br \/>\n<em>CLR_bldgrn=&#8217;\\e[1;32m&#8217; # Green<\/em><br \/>\n<em>CLR_bldylw=&#8217;\\e[1;33m&#8217; # Yellow<\/em><br \/>\n<em>CLR_bldblu=&#8217;\\e[1;34m&#8217; # Blue<\/em><br \/>\n<em>CLR_bldpur=&#8217;\\e[1;35m&#8217; # Purple<\/em><br \/>\n<em>CLR_bldcyn=&#8217;\\e[1;36m&#8217; # Cyan<\/em><br \/>\n<em>CLR_bldwht=&#8217;\\e[1;37m&#8217; # White<\/em><br \/>\n<em>CLR_unkblk=&#8217;\\e[4;30m&#8217; # Black &#8211; Underline<\/em><br \/>\n<em>CLR_undred=&#8217;\\e[4;31m&#8217; # Red<\/em><br \/>\n<em>CLR_undgrn=&#8217;\\e[4;32m&#8217; # Green<\/em><br \/>\n<em>CLR_undylw=&#8217;\\e[4;33m&#8217; # Yellow<\/em><br \/>\n<em>CLR_undblu=&#8217;\\e[4;34m&#8217; # Blue<\/em><br \/>\n<em>CLR_undpur=&#8217;\\e[4;35m&#8217; # Purple<\/em><br \/>\n<em>CLR_undcyn=&#8217;\\e[4;36m&#8217; # Cyan<\/em><br \/>\n<em>CLR_undwht=&#8217;\\e[4;37m&#8217; # White<\/em><br \/>\n<em>CLR_bakblk=&#8217;\\e[40m&#8217; \u00a0 # Black &#8211; Background<\/em><br \/>\n<em>CLR_bakred=&#8217;\\e[41m&#8217; \u00a0 # Red<\/em><br \/>\n<em>CLR_bakgrn=&#8217;\\e[42m&#8217; \u00a0 # Green<\/em><br \/>\n<em>CLR_bakylw=&#8217;\\e[43m&#8217; \u00a0 # Yellow<\/em><br \/>\n<em>CLR_bakblu=&#8217;\\e[44m&#8217; \u00a0 # Blue<\/em><br \/>\n<em>CLR_bakpur=&#8217;\\e[45m&#8217; \u00a0 # Purple<\/em><br \/>\n<em>CLR_bakcyn=&#8217;\\e[46m&#8217; \u00a0 # Cyan<\/em><br \/>\n<em>CLR_bakwht=&#8217;\\e[47m&#8217; \u00a0 # White<\/em><br \/>\n<em>CLR_txtrst=&#8217;\\e[0m&#8217; \u00a0 \u00a0# Text Reset<\/em><\/p>\n<p><em>#<\/em><\/p>\n<p><em>#########################<\/em><\/p>\n<p><em>SERVERS=\u201dhost1 host2 host3\u201d<\/em><br \/>\n<em>#echo -e &#8220;${CLR_bldgrn}Enter Servers (space seperated)${CLR_txtrst}&#8221;<\/em><\/p>\n<p><em>#read -p &#8220;servers: &#8221; SERVERS<\/em><br \/>\n<em> echo -e &#8220;${CLR_bldgrn}Enter User${CLR_txtrst}&#8221;<\/em><br \/>\n<em>read -p &#8220;user: &#8221; USERNAME<\/em><br \/>\n<em>echo -e &#8220;${CLR_bldgrn}Enter Password${CLR_txtrst}&#8221;<\/em><br \/>\n<em>read -p &#8220;password: &#8221; -s USERPW<\/em><br \/>\n<em>echo<\/em><br \/>\n<em>echo -e &#8220;${CLR_bldgrn}Enter Root Password${CLR_txtrst}&#8221;<\/em><br \/>\n<em>read -p &#8220;password: &#8221; -s ROOTPW<\/em><br \/>\n<em>echo <\/em><br \/>\n<em>for machine in $SERVERS; do<\/em><br \/>\n<em>~\/gotroot ${USERNAME} ${machine} ${USERPW} ${ROOTPW} \u00a02&gt;&amp;1 | tee -a logs.txt<\/em><br \/>\n<em>done<\/em><\/p>\n<p>======================================<\/p>\n<p>Hope you enjoyed this tutorial and if you have any questions email nick@nicktailor.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to pass a &#8220;password&#8221; to su as a variable in script and execute tasks to an array of hosts Some of you may work for organizations that do access control for linux servers. In which case they do not use ssh keys for root, and are still doing the unthinkable allowing the use of password authentication. So this means<a href=\"https:\/\/nicktailor.com\/tech-blog\/how-to-pass-a-password-to-su-as-a-variable-in-script\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,138],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-centos","category-linux"],"_links":{"self":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":7,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":1618,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/posts\/287\/revisions\/1618"}],"wp:attachment":[{"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nicktailor.com\/tech-blog\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}