<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohamed ElZayat on Medium]]></title>
        <description><![CDATA[Stories by Mohamed ElZayat on Medium]]></description>
        <link>https://medium.com/@mohamedelzayat?source=rss-1628e54cc31------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WP_qoUsWZS4XMidh.</url>
            <title>Stories by Mohamed ElZayat on Medium</title>
            <link>https://medium.com/@mohamedelzayat?source=rss-1628e54cc31------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:09:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohamedelzayat/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Python to Manage your infrastructure]]></title>
            <link>https://medium.com/@mohamedelzayat/using-python-to-manage-your-infrastructure-67e82c137870?source=rss-1628e54cc31------2</link>
            <guid isPermaLink="false">https://medium.com/p/67e82c137870</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[paramiko]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[solaris]]></category>
            <dc:creator><![CDATA[Mohamed ElZayat]]></dc:creator>
            <pubDate>Wed, 25 Jul 2018 13:08:32 GMT</pubDate>
            <atom:updated>2018-07-25T15:04:15.772Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*QrauhU2Sz15oOv3tseZL6Q.png" /><figcaption><strong>Output</strong></figcaption></figure><p>I was assigned a new task to manage the Infrastructure team in my current job taken into consideration I don’t have the proper infrastructure background yet I have a good development background</p><p>We have more than 500 servers including Solaris and Linux (Red hat, Suse Oracle, Ubuntu). The team was executing commands server by server and it was a hell of time to get things done across all servers or to retrieve certain information from certain servers. I believed there is a way to save time and effort and to get the things done easily. I asked the team to search for a way but unfortunately they did not come with the proper feedback</p><p>I searched for a way to make our life easier and I found that there is an awesome library in python called “<strong>paramiko</strong>”, so I used this library and it made managing the infrastructure servers as a piece of cake</p><p>Here are the steps:</p><ul><li>Install Python 3.7</li><li>Run command prompt (cmd)</li><li>Go to Python directory and type “ <strong>python -m pip install paramiko</strong>”</li><li>Create a directory and create a text file (IPs.txt) and include all your IPs in this file</li></ul><pre>10.157.29.75<br>10.157.29.76<br>10.157.29.77<br>10.157.29.74<br>10.157.28.86<br>10.157.28.87<br>10.157.28.96<br></pre><ul><li>Save the below code as “connect.py”</li></ul><pre><strong>import </strong>paramiko<br><strong>import </strong>socket<br><br>port = <strong>&#39;22&#39;<br></strong>username = <strong>&#39;username&#39;<br></strong>password = <strong>&#39;password&#39;</strong></pre><pre><strong>with </strong>open(<strong>&#39;IPs.txt&#39;</strong>) <strong>as </strong>f:<br>    <strong>for </strong>line <strong>in </strong>f:<br>        IP = line[:-1]<br>        <em>#print(&#39;############################ &#39; + IP)<br>        </em>port = 22<br>        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>        sock.settimeout(2)<br>        result = sock.connect_ex((IP, port))<br>        <strong>if </strong>result == 0:<br>            <em># print (IP + &#39; successful on port &#39;+ str(port))<br>            </em><strong>try</strong>:<br>                ssh_connection = paramiko.SSHClient()<br>                ssh_connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())<br>                ssh_connection.connect(<br>                    IP,<br>                    port,<br>                    username,<br>                    password,<br>                    allow_agent=<strong>False</strong>,<br>                    look_for_keys=<strong>False</strong>,<br>                    timeout=5<br>                    )</pre><pre><em>                </em>v_command=<strong>&quot;&quot;&quot;uptime&quot;&quot;&quot;</strong></pre><pre><em>                #</em>v_command__NumofCores_SunOS=<strong>&quot;&quot;&quot;/usr/bin/kstat cpu_info|grep core_id|sort -u|wc -l&quot;&quot;&quot;<br>                #</strong>v_command__NumofCores_Linux=<strong>&quot;&quot;&quot;cat /proc/cpuinfo | grep &quot;cpu cores&quot; | uniq&quot;&quot;&quot;<br><br>                </strong>v_command__PhysicalCPU_SunOS=v_command<br>                v_command__PhysicalCPU_Linux=v_command<br><br><em>                </em>(stdin, stdout, stderr) = ssh_connection.exec_command(<strong>&#39;hostname&#39;</strong>)<br>                <strong>for </strong>line <strong>in </strong>stdout.readlines():<br>                    v_hostname = line[:-1]<br>                (stdin, stdout, stderr) = ssh_connection.exec_command(<strong>&#39;uname&#39;</strong>)<br>                <strong>for </strong>line <strong>in </strong>stdout.readlines():<br>                    v_OS = line[:-1]<br><em>                </em><strong>if </strong>v_OS==<strong>&quot;SunOS&quot; </strong>:<br>                    (stdin, stdout, stderr) = ssh_connection.exec_command(v_command__PhysicalCPU_SunOS,timeout=10)<br><em>                    </em><strong>for </strong>line <strong>in </strong>stdout.readlines():<br>                        print(IP + <strong>&quot;,&quot; </strong>+ v_hostname + <strong>&quot;,&quot; </strong>+ v_OS + <strong>&quot;,&quot; </strong>+ line[:-1] , sep=<strong>&#39;\n\t&#39;</strong>)<br>                    <strong>for </strong>line <strong>in </strong>stderr.readlines():<br>                         print(IP + <strong>&quot;,&quot; </strong>+ v_hostname + <strong>&quot;,&quot; </strong>+ v_OS + <strong>&quot;,&quot; </strong>+ line[:-1], sep=<strong>&#39;\n\t&#39;</strong>)<br>                    <em># (stdin, stdout, stderr) = ssh_connection.exec_command(v_command__NumofCores_SunOS,timeout=10)<br>                    # for line2 in stdout.readlines():<br>                    #     print(IP + &quot;,&quot; + v_hostname + &quot;,&quot; + v_OS + &quot;,&quot; + line[:-1] + &quot;,&quot; + line2[:-1] ,sep=&#39;\n\t&#39;)<br>                </em><strong>if </strong>v_OS==<strong>&#39;Linux&#39;</strong>:<br>                    (stdin, stdout, stderr) = ssh_connection.exec_command(v_command__PhysicalCPU_Linux,timeout=10)<br>                    <strong>for </strong>line <strong>in </strong>stdout.readlines():<br>                        print(IP + <strong>&quot;,&quot; </strong>+ v_hostname + <strong>&quot;,&quot; </strong>+ v_OS + <strong>&quot;,&quot; </strong>+ line[:-1] , sep=<strong>&#39;\n\t&#39;</strong>)<br>                    <strong>for </strong>line <strong>in </strong>stderr.readlines():<br>                        print(IP + <strong>&quot;,&quot; </strong>+ v_hostname + <strong>&quot;,&quot; </strong>+ v_OS + <strong>&quot;,&quot; </strong>+ line[:-1], sep=<strong>&#39;\n\t&#39;</strong>)<br>                    <em># (stdin, stdout, stderr) = ssh_connection.exec_command(v_command__NumofCores_Linux,<br>                    #                                                       timeout=10)<br>                    # for line2 in stdout.readlines():<br>                    #     print(IP + &quot;,&quot; + v_hostname + &quot;,&quot; + v_OS + &quot;,&quot; + line[:-1] + &quot;,&quot; + line2[:-1], sep=&#39;\n\t&#39;)<br>            </em><strong>except </strong>paramiko.AuthenticationException:<br>                print(IP + <strong>&quot;, Authentication failed, please verify your credentials&quot;</strong>)<br>            <strong>except </strong>paramiko.SSHException:<br>                print (IP + <strong>&#39;, Unable to establish SSH connection: %s&#39; </strong>% paramiko.SSHException)<br>            <strong>except </strong>paramiko.BadHostKeyException:<br>                print (IP + <strong>&quot;, Unable to verify server&#39;s host key: %s&quot; </strong>% paramiko.BadHostKeyException)<br>            <strong>except </strong>socket.error:<br>                print (IP + <strong>&#39;, Socket Error: %s&#39; </strong>% socket.error)<br>            <strong>except </strong>Exception:<br>                print (IP + <strong>&#39;, Operation error: %s&#39;</strong>, Exception)<br>                ssh_connection.close()<br>        <strong>else</strong>:<br>            print (IP + <strong>&#39;, failed on port &#39; </strong>+ str(port))</pre><p>Run the code and Enjoy the results :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67e82c137870" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>