/usr/share/doc/python-asyncssh-doc/html/index.html is in python-asyncssh-doc 1.3.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AsyncSSH: Asynchronous SSH for Python — AsyncSSH 1.3.0 documentation</title>
<link rel="stylesheet" href="_static/rftheme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '1.3.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="AsyncSSH 1.3.0 documentation" href="#" />
<link rel="next" title="Change Log" href="changes.html" />
</head>
<body role="document">
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="toctree-wrapper compound">
</div>
<div class="section" id="asyncssh-asynchronous-ssh-for-python">
<h1>AsyncSSH: Asynchronous SSH for Python<a class="headerlink" href="#asyncssh-asynchronous-ssh-for-python" title="Permalink to this headline">¶</a></h1>
<p>AsyncSSH is a Python package which provides an asynchronous client and
server implementation of the SSHv2 protocol on top of the Python 3.4+
asyncio framework.</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">open_session</span><span class="p">(</span><span class="s">'echo "Hello!"'</span><span class="p">)</span>
<span class="n">output</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">stdout</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">output</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="n">status</span> <span class="o">=</span> <span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">get_exit_status</span><span class="p">()</span>
<span class="k">if</span> <span class="n">status</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Program exited with status </span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="n">status</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Program exited successfully'</span><span class="p">)</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
</pre></div>
</div>
<dl class="docutils">
<dt>Check out the <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/#client-examples">examples</a> to get started!</dt>
<dd></dd>
</dl>
<div class="section" id="features">
<h2>Features<a class="headerlink" href="#features" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>Full support for SSHv2 and SFTP client and server functions<ul>
<li>Shell, command, and subsystem channels</li>
<li>Environment variables, terminal type, and window size</li>
<li>Direct and forwarded TCP/IP channels</li>
<li>Local and remote TCP/IP port forwarding</li>
<li>SFTP protocol version 3 with OpenSSH extensions</li>
</ul>
</li>
<li>Multiple simultaneous sessions on a single SSH connection</li>
<li>Multiple SSH connections in a single event loop</li>
<li>Byte and string based I/O with settable encoding</li>
<li>A variety of <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#key-exchange-algorithms">key exchange</a>, <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#encryption-algorithms">encryption</a>, and <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#mac-algorithms">MAC</a> algorithms</li>
<li>Support for <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#compression-algorithms">gzip compression</a><ul>
<li>Including OpenSSH variant to delay compression until after auth</li>
</ul>
</li>
<li>Password, public key, and keyboard-interactive user authentication methods</li>
<li>Many types and formats of <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#public-key-support">public keys and certificates</a></li>
<li>OpenSSH-style <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#known-hosts">known_hosts file</a> support</li>
<li>OpenSSH-style <a class="reference external" href="http://asyncssh.readthedocs.org/en/stable/api.html#authorized-keys">authorized_keys file</a> support</li>
<li>Compatibility with OpenSSH “Encrypt then MAC” option for better security</li>
<li>Time and byte-count based session key renegotiation</li>
<li>Designed to be easy to extend to support new forms of key exchange,
authentication, encryption, and compression algorithms</li>
</ul>
</div>
<div class="section" id="license">
<h2>License<a class="headerlink" href="#license" title="Permalink to this headline">¶</a></h2>
<p>This package is released under the following terms:</p>
<blockquote>
<div><p>Copyright (c) 2013-2015 by Ron Frederick <<a class="reference external" href="mailto:ronf%40timeheart.net">ronf<span>@</span>timeheart<span>.</span>net</a>>.
All rights reserved.</p>
<p>This program and the accompanying materials are made available under
the terms of the <strong>Eclipse Public License v1.0</strong> which accompanies
this distribution and is available at:</p>
<blockquote>
<div><a class="reference external" href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a></div></blockquote>
</div></blockquote>
<p>For more information about this license, please see the <a class="reference external" href="https://eclipse.org/legal/eplfaq.php">Eclipse
Public License FAQ</a>.</p>
</div>
<div class="section" id="prerequisites">
<h2>Prerequisites<a class="headerlink" href="#prerequisites" title="Permalink to this headline">¶</a></h2>
<p>To use <code class="docutils literal"><span class="pre">asyncssh</span></code>, you need the following:</p>
<ul class="simple">
<li>Python 3.4 or later</li>
<li>cryptography (PyCA) 1.0.0 or later</li>
</ul>
</div>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>Install AsyncSSH by running:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre>pip install asyncssh
</pre></div>
</div>
</div></blockquote>
<div class="section" id="optional-extras">
<h3>Optional Extras<a class="headerlink" href="#optional-extras" title="Permalink to this headline">¶</a></h3>
<p>There are some optional modules you can install to enable additional
functionality:</p>
<ul class="simple">
<li>Install bcrypt from <a class="reference external" href="https://code.google.com/p/py-bcrypt">https://code.google.com/p/py-bcrypt</a>
if you want support for OpenSSH private key encryption.</li>
<li>Install libsodium from <a class="reference external" href="https://github.com/jedisct1/libsodium">https://github.com/jedisct1/libsodium</a>
and libnacl from <a class="reference external" href="https://github.com/saltstack/libnacl">https://github.com/saltstack/libnacl</a> if you want
support for curve25519 Diffie Hellman key exchange, ed25519 keys,
and the chacha20-poly1305 cipher.</li>
</ul>
<p>AsyncSSH defines the following optional PyPI extra packages to make it
easy to install any or all of these dependencies:</p>
<blockquote>
<div><div class="line-block">
<div class="line">bcrypt</div>
<div class="line">libnacl</div>
</div>
</div></blockquote>
<p>For example, to install all of these, you can run:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre>pip install 'asyncssh[bcrypt,libnacl]'
</pre></div>
</div>
</div></blockquote>
<p>Note that you will still need to manually install the libsodium library
listed above for libnacl to work correctly. Unfortunately, since
libsodium is not a Python package, it cannot be directly installed using
pip.</p>
</div>
</div>
<div class="section" id="mailing-lists">
<h2>Mailing Lists<a class="headerlink" href="#mailing-lists" title="Permalink to this headline">¶</a></h2>
<p>Three mailing lists are available for AsyncSSH:</p>
<ul class="simple">
<li><a class="reference external" href="http://groups.google.com/d/forum/asyncssh-announce">asyncssh-announce@googlegroups.com</a>: Project announcements</li>
<li><a class="reference external" href="http://groups.google.com/d/forum/asyncssh-dev">asyncssh-dev@googlegroups.com</a>: Development discussions</li>
<li><a class="reference external" href="http://groups.google.com/d/forum/asyncssh-users">asyncssh-users@googlegroups.com</a>: End-user discussions</li>
</ul>
</div>
</div>
<div class="section" id="client-examples">
<span id="clientexamples"></span><h1>Client Examples<a class="headerlink" href="#client-examples" title="Permalink to this headline">¶</a></h1>
<div class="section" id="simple-client">
<h2>Simple client<a class="headerlink" href="#simple-client" title="Permalink to this headline">¶</a></h2>
<p>The following code shows an example of a simple SSH client which logs into
localhost and lists files in a directory named ‘abc’ under the user’s home
directory. The username provided is the logged in user, and the user’s
default SSH client keys or certificates are presented during authentication.
The server’s host key is checked against the user’s SSH known_hosts file and
the connection will fail if there’s no entry for localhost there or if the
key doesn’t match.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MySSHClient</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClient</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Connection made to </span><span class="si">%s</span><span class="s">.'</span> <span class="o">%</span> <span class="n">conn</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s">'peername'</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">auth_completed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Authentication successful.'</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="n">conn</span><span class="p">,</span> <span class="n">client</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MySSHClient</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">)</span>
<span class="k">with</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span> <span class="s">'ls abc'</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>To check against a different set of server host keys, they can be read
and provided in the known_hosts argument when the <a class="reference internal" href="api.html#asyncssh.SSHClient" title="asyncssh.SSHClient"><code class="xref py py-class docutils literal"><span class="pre">SSHClient</span></code></a>
instance is created:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="n">conn</span><span class="p">,</span> <span class="n">client</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MySSHClient</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">,</span>
<span class="n">known_hosts</span><span class="o">=</span><span class="s">'my_known_hosts'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<p>Server host key checking can be disabled by setting the known_hosts
argument to <code class="docutils literal"><span class="pre">None</span></code>, but that’s not recommended as it makes the
connection vulnerable to a man-in-the-middle attack.</p>
<p>To log in as a different remote user, the username argument can be
provided:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="n">conn</span><span class="p">,</span> <span class="n">client</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MySSHClient</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">,</span>
<span class="n">username</span><span class="o">=</span><span class="s">'user123'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<p>To use a different set of client keys for authentication, they can be
read and provided in the client_keys argument:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="n">conn</span><span class="p">,</span> <span class="n">client</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MySSHClient</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">,</span>
<span class="n">client_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'my_ssh_key'</span><span class="p">])</span>
</pre></div>
</div>
</div></blockquote>
<p>Password authentication can be used by providing a password argument:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="n">conn</span><span class="p">,</span> <span class="n">client</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MySSHClient</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">,</span>
<span class="n">password</span><span class="o">=</span><span class="s">'secretpw'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<p>Any of the arguments above can be combined together as needed. If client
keys and a password are both provided, either may be used depending
on what forms of authentication the server supports and whether the
authentication with them is successful.</p>
<p>In cases where you don’t need to customize callbacks on the SSHClient class,
this code can be simplified somewhat to:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span> <span class="s">'ls abc'</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="handling-of-stderr">
<h2>Handling of stderr<a class="headerlink" href="#handling-of-stderr" title="Permalink to this headline">¶</a></h2>
<p>The above code doesn’t distinguish output going to stdout vs. stderr, but
that’s easy to do with the following change:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">if</span> <span class="n">datatype</span> <span class="o">==</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">EXTENDED_DATA_STDERR</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span> <span class="s">'ls abc'</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="simple-client-with-input">
<h2>Simple client with input<a class="headerlink" href="#simple-client-with-input" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates sending input to a remote program.
It executes the calculator program <code class="docutils literal"><span class="pre">bc</span></code> and performs some basic math
calculations.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">next_operation</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operations</span><span class="p">:</span>
<span class="n">operation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_operations</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="s">'= '</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">operation</span> <span class="o">+</span> <span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write_eof</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">session_started</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_operations</span> <span class="o">=</span> <span class="p">[</span><span class="s">'2+2'</span><span class="p">,</span> <span class="s">'1*2*3*4'</span><span class="p">,</span> <span class="s">'2^32'</span><span class="p">]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">next_operation</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">if</span> <span class="s">'</span><span class="se">\n</span><span class="s">'</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">next_operation</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span> <span class="s">'bc'</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>Note that input is not sent on the channel until the <a class="reference internal" href="api.html#asyncssh.SSHClientSession.session_started" title="asyncssh.SSHClientSession.session_started"><code class="xref py py-meth docutils literal"><span class="pre">session_started()</span></code></a> method is called, and <a class="reference internal" href="api.html#asyncssh.SSHClientChannel.write_eof" title="asyncssh.SSHClientChannel.write_eof"><code class="xref py py-meth docutils literal"><span class="pre">write_eof()</span></code></a> is used to signal the end of input, causing the
‘bc’ program to exit.</p>
<p>This example can be simplified by using the higher-level “streams” API. With
that, callbacks aren’t needed. Here’s the streams version of the above example,
using <a class="reference internal" href="api.html#asyncssh.SSHClientConnection.open_session" title="asyncssh.SSHClientConnection.open_session"><code class="xref py py-meth docutils literal"><span class="pre">open_session</span></code></a> instead of
<a class="reference internal" href="api.html#asyncssh.SSHClientConnection.create_session" title="asyncssh.SSHClientConnection.create_session"><code class="xref py py-meth docutils literal"><span class="pre">create_session</span></code></a>:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">open_session</span><span class="p">(</span><span class="s">'bc'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">op</span> <span class="ow">in</span> <span class="p">[</span><span class="s">'2+2'</span><span class="p">,</span> <span class="s">'1*2*3*4'</span><span class="p">,</span> <span class="s">'2^32'</span><span class="p">]:</span>
<span class="n">stdin</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">op</span> <span class="o">+</span> <span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">stdout</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="s">'='</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>When run, this program should produce the following output:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="mi">2</span><span class="o">+</span><span class="mi">2</span> <span class="o">=</span> <span class="mi">4</span>
<span class="mi">1</span><span class="o">*</span><span class="mi">2</span><span class="o">*</span><span class="mi">3</span><span class="o">*</span><span class="mi">4</span> <span class="o">=</span> <span class="mi">24</span>
<span class="mi">2</span><span class="o">^</span><span class="mi">32</span> <span class="o">=</span> <span class="mi">4294967296</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="checking-exit-status">
<h2>Checking exit status<a class="headerlink" href="#checking-exit-status" title="Permalink to this headline">¶</a></h2>
<p>The following example is a variation of the simple client which shows how to
receive the remote program’s exit status using the <a class="reference internal" href="api.html#asyncssh.SSHClientSession.exit_status_received" title="asyncssh.SSHClientSession.exit_status_received"><code class="xref py py-meth docutils literal"><span class="pre">exit_status_received</span></code></a> callback.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">if</span> <span class="n">datatype</span> <span class="o">==</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">EXTENDED_DATA_STDERR</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">exit_status_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">status</span><span class="p">):</span>
<span class="k">if</span> <span class="n">status</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Program exited with status </span><span class="si">%d</span><span class="s">'</span> <span class="o">%</span> <span class="n">status</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Program exited successfully'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span> <span class="s">'ls abc'</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>From servers that support it, exit signals can also be received using
<a class="reference internal" href="api.html#asyncssh.SSHClientSession.exit_signal_received" title="asyncssh.SSHClientSession.exit_signal_received"><code class="xref py py-meth docutils literal"><span class="pre">exit_signal_received</span></code></a>.</p>
<p>Exit status can be also queried after the channel has closed by using the
methods <a class="reference internal" href="api.html#asyncssh.SSHClientChannel.get_exit_status" title="asyncssh.SSHClientChannel.get_exit_status"><code class="xref py py-meth docutils literal"><span class="pre">get_exit_status</span></code></a> and
<a class="reference internal" href="api.html#asyncssh.SSHClientChannel.get_exit_signal" title="asyncssh.SSHClientChannel.get_exit_signal"><code class="xref py py-meth docutils literal"><span class="pre">get_exit_signal</span></code></a>. This is
how it is done when using the streams API, since callbacks aren’t available
there.</p>
</div>
<div class="section" id="setting-environment-variables">
<h2>Setting environment variables<a class="headerlink" href="#setting-environment-variables" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates setting environment variables
for the remote session and displaying them by executing the ‘env’
command.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span> <span class="s">'env'</span><span class="p">,</span>
<span class="n">env</span><span class="o">=</span><span class="p">{</span> <span class="s">'LANG'</span><span class="p">:</span> <span class="s">'en_GB'</span><span class="p">,</span>
<span class="s">'LC_COLLATE'</span><span class="p">:</span> <span class="s">'C'</span><span class="p">})</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>Any number of environment variables can be passed in the dictionary
given to <a class="reference internal" href="api.html#asyncssh.SSHClientConnection.create_session" title="asyncssh.SSHClientConnection.create_session"><code class="xref py py-meth docutils literal"><span class="pre">create_session()</span></code></a>.
Note that SSH servers may restrict which environment variables (if any)
are accepted, so this feature may require setting options on the SSH
server before it will work.</p>
</div>
<div class="section" id="setting-terminal-information">
<h2>Setting terminal information<a class="headerlink" href="#setting-terminal-information" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates setting the terminal type and size
passed to the remote session.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHClientSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHClientSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH session error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_session</span><span class="p">(</span><span class="n">MySSHClientSession</span><span class="p">,</span>
<span class="s">'echo $TERM; stty size'</span><span class="p">,</span>
<span class="n">term_type</span><span class="o">=</span><span class="s">'xterm-color'</span><span class="p">,</span>
<span class="n">term_size</span><span class="o">=</span><span class="p">(</span><span class="mi">80</span><span class="p">,</span> <span class="mi">24</span><span class="p">))</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>Note that this will cause AsyncSSH to request a pseudo-tty from the
server. When a pseudo-tty is used, the server will no longer send output
going to stderr with a different data type. Instead, it will be mixed
with output going to stdout (unless it is redirected elsewhere by the
remote command).</p>
</div>
<div class="section" id="port-forwarding">
<h2>Port forwarding<a class="headerlink" href="#port-forwarding" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates the client setting up a local TCP
listener on port 8080 and requesting that connections which arrive on
that port be forwarded across SSH to the server and on to port 80 on
<code class="docutils literal"><span class="pre">www.google.com</span></code>:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">listener</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">forward_local_port</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8080</span><span class="p">,</span> <span class="s">'www.google.com'</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">listener</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>To listen on a dynamically assigned port, the client can pass in <code class="docutils literal"><span class="pre">0</span></code>
as the listening port. If the listener is successfully opened, the selected
port will be available via the <a class="reference internal" href="api.html#asyncssh.SSHListener.get_port" title="asyncssh.SSHListener.get_port"><code class="xref py py-meth docutils literal"><span class="pre">get_port()</span></code></a>
method on the returned listener object:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">listener</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">forward_local_port</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s">'www.google.com'</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Listening on port </span><span class="si">%s</span><span class="s">...'</span> <span class="o">%</span> <span class="n">listener</span><span class="o">.</span><span class="n">get_port</span><span class="p">())</span>
<span class="k">yield from</span> <span class="n">listener</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>The client can also request remote port forwarding from the server. The
following example shows the client requesting that the server listen on
port 8080 and that connections arriving there be forwarded across SSH
and on to port 80 on <code class="docutils literal"><span class="pre">localhost</span></code>:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">listener</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">forward_remote_port</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8080</span><span class="p">,</span> <span class="s">'localhost'</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">listener</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>To limit which connections are accepted or dynamically select where to
forward traffic to, the client can implement their own session factory and
call <a class="reference internal" href="api.html#asyncssh.SSHClientConnection.forward_connection" title="asyncssh.SSHClientConnection.forward_connection"><code class="xref py py-meth docutils literal"><span class="pre">forward_connection()</span></code></a>
on the connections they wish to forward and raise an error on those they
wish to reject:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">def</span> <span class="nf">connection_requested</span><span class="p">(</span><span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">):</span>
<span class="k">global</span> <span class="n">conn</span>
<span class="k">if</span> <span class="n">orig_host</span> <span class="ow">in</span> <span class="p">(</span><span class="s">'127.0.0.1'</span><span class="p">,</span> <span class="s">'::1'</span><span class="p">):</span>
<span class="k">return</span> <span class="n">conn</span><span class="o">.</span><span class="n">forward_connection</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">ChannelOpenError</span><span class="p">(</span>
<span class="n">asyncssh</span><span class="o">.</span><span class="n">OPEN_ADMINISTRATIVELY_PROHIBITED</span><span class="p">,</span>
<span class="s">'Connections only allowed from localhost'</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">global</span> <span class="n">conn</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">listener</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">connection_requested</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8080</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">listener</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>Just as with local listeners, the client can request remote port forwarding
from a dynamic port by passing in <code class="docutils literal"><span class="pre">0</span></code> as the listening port and then call
<a class="reference internal" href="api.html#asyncssh.SSHListener.get_port" title="asyncssh.SSHListener.get_port"><code class="xref py py-meth docutils literal"><span class="pre">get_port()</span></code></a> on the returned listener to
determine which port was selected.</p>
</div>
<div class="section" id="direct-tcp-connections">
<h2>Direct TCP connections<a class="headerlink" href="#direct-tcp-connections" title="Permalink to this headline">¶</a></h2>
<p>The client can also ask the server to open a TCP connection and directly
send and receive data on it by using the <a class="reference internal" href="api.html#asyncssh.SSHClientConnection.create_connection" title="asyncssh.SSHClientConnection.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">create_connection()</span></code></a> method on the
<a class="reference internal" href="api.html#asyncssh.SSHClientConnection" title="asyncssh.SSHClientConnection"><code class="xref py py-class docutils literal"><span class="pre">SSHClientConnection</span></code></a> object. In this example, a connection is
attempted to port 80 on <code class="docutils literal"><span class="pre">www.google.com</span></code> and an HTTP HEAD request is
sent for the document root.</p>
<p>Note that unlike sessions created with <a class="reference internal" href="api.html#asyncssh.SSHClientConnection.create_session" title="asyncssh.SSHClientConnection.create_session"><code class="xref py py-meth docutils literal"><span class="pre">create_session()</span></code></a>, the I/O on these connections defaults
to sending and receiving bytes rather than strings, allowing arbitrary
binary data to be exchanged. However, this can be changed by setting
the encoding to use when the connection is created.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHTCPSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHTCPSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="c"># We use sys.stdout.buffer here because we're writing bytes</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">buffer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Direct connection error:'</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">chan</span><span class="p">,</span> <span class="n">session</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_connection</span><span class="p">(</span><span class="n">MySSHTCPSession</span><span class="p">,</span>
<span class="s">'www.google.com'</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="c"># By default, TCP connections send and receive bytes</span>
<span class="n">chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">'HEAD / HTTP/1.0</span><span class="se">\r\n\r\n</span><span class="s">'</span><span class="p">)</span>
<span class="n">chan</span><span class="o">.</span><span class="n">write_eof</span><span class="p">()</span>
<span class="k">yield from</span> <span class="n">chan</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>To use the streams API to open a direct connection, you can use
<a class="reference internal" href="api.html#asyncssh.SSHClientConnection.open_connection" title="asyncssh.SSHClientConnection.open_connection"><code class="xref py py-meth docutils literal"><span class="pre">open_connection</span></code></a> instead of
<a class="reference internal" href="api.html#asyncssh.SSHClientConnection.create_connection" title="asyncssh.SSHClientConnection.create_connection"><code class="xref py py-meth docutils literal"><span class="pre">create_connection</span></code></a>:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">reader</span><span class="p">,</span> <span class="n">writer</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">open_connection</span><span class="p">(</span><span class="s">'www.google.com'</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="c"># By default, TCP connections send and receive bytes</span>
<span class="n">writer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">'HEAD / HTTP/1.0</span><span class="se">\r\n\r\n</span><span class="s">'</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">write_eof</span><span class="p">()</span>
<span class="c"># We use sys.stdout.buffer here because we're writing bytes</span>
<span class="n">response</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">reader</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">buffer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="forwarded-tcp-connections">
<h2>Forwarded TCP connections<a class="headerlink" href="#forwarded-tcp-connections" title="Permalink to this headline">¶</a></h2>
<p>The client can also directly process data from incoming TCP connections
received on the server. The following example demonstrates the client
requesting that the server listen on port 8888 and forward any received
connections back to it over SSH. It then has a simple handler which
echoes any data it receives back to the sender.</p>
<p>As in the direct TCP connection example above, the default would be to
send and receive bytes on this connection rather than strings, but here
we set the encoding explicitly so all data is sent and received as strings:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHTCPSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHTCPSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">connection_requested</span><span class="p">(</span><span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Connection received from </span><span class="si">%s</span><span class="s">, port </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">))</span>
<span class="k">return</span> <span class="n">MySSHTCPSession</span><span class="p">()</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">server</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">connection_requested</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8888</span><span class="p">,</span>
<span class="n">encoding</span><span class="o">=</span><span class="s">'utf-8'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">server</span><span class="p">:</span>
<span class="k">yield from</span> <span class="n">server</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Listener couldn''t be opened.'</span><span class="p">,</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>To use the streams API to open a listening connection, you can use
<a class="reference internal" href="api.html#asyncssh.SSHClientConnection.start_server" title="asyncssh.SSHClientConnection.start_server"><code class="xref py py-meth docutils literal"><span class="pre">start_server</span></code></a> instead
of <a class="reference internal" href="api.html#asyncssh.SSHClientConnection.create_server" title="asyncssh.SSHClientConnection.create_server"><code class="xref py py-meth docutils literal"><span class="pre">create_server</span></code></a>:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">handle_connection</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">writer</span><span class="p">):</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">reader</span><span class="o">.</span><span class="n">at_eof</span><span class="p">():</span>
<span class="n">data</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">reader</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">8192</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">connection_requested</span><span class="p">(</span><span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Connection received from </span><span class="si">%s</span><span class="s">, port </span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">))</span>
<span class="k">return</span> <span class="n">handle_connection</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="n">server</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">start_server</span><span class="p">(</span><span class="n">connection_requested</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8888</span><span class="p">,</span>
<span class="n">encoding</span><span class="o">=</span><span class="s">'utf-8'</span><span class="p">)</span>
<span class="k">yield from</span> <span class="n">server</span><span class="o">.</span><span class="n">wait_closed</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH connection failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="sftp-client">
<h2>SFTP client<a class="headerlink" href="#sftp-client" title="Permalink to this headline">¶</a></h2>
<p>AsyncSSH also provides SFTP support. The following code shows an example
of starting an SFTP client and requestng the download of a file:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">run_client</span><span class="p">():</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">'localhost'</span><span class="p">))</span> <span class="k">as</span> <span class="n">conn</span><span class="p">:</span>
<span class="k">with</span> <span class="p">(</span><span class="k">yield from</span> <span class="n">conn</span><span class="o">.</span><span class="n">start_sftp_client</span><span class="p">())</span> <span class="k">as</span> <span class="n">sftp</span><span class="p">:</span>
<span class="k">yield from</span> <span class="n">sftp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'example.txt'</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">run_client</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SFTP operation failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
</pre></div>
</div>
</div></blockquote>
<p>To recursively download a directory, preserving access and modification
times and permissions on the files, the preserve and recurse arguments
can be included:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="k">yield from</span> <span class="n">sftp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'example_dir'</span><span class="p">,</span> <span class="n">preserve</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">recurse</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<p>Wild card pattern matching is supported by the <a class="reference internal" href="api.html#asyncssh.SFTPClient.mget" title="asyncssh.SFTPClient.mget"><code class="xref py py-meth docutils literal"><span class="pre">mget</span></code></a>,
<a class="reference internal" href="api.html#asyncssh.SFTPClient.mput" title="asyncssh.SFTPClient.mput"><code class="xref py py-meth docutils literal"><span class="pre">mput</span></code></a>, and <a class="reference internal" href="api.html#asyncssh.SFTPClient.mcopy" title="asyncssh.SFTPClient.mcopy"><code class="xref py py-meth docutils literal"><span class="pre">mcopy</span></code></a> methods.
The following downloads all files with extension “txt”:</p>
<blockquote>
<div><div class="code highlight-python"><div class="highlight"><pre><span class="k">yield from</span> <span class="n">sftp</span><span class="o">.</span><span class="n">mget</span><span class="p">(</span><span class="s">'*.txt'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<p>See the <a class="reference internal" href="api.html#asyncssh.SFTPClient" title="asyncssh.SFTPClient"><code class="xref py py-class docutils literal"><span class="pre">SFTPClient</span></code></a> documentation for the full list of available
actions.</p>
</div>
</div>
<div class="section" id="server-examples">
<span id="serverexamples"></span><h1>Server Examples<a class="headerlink" href="#server-examples" title="Permalink to this headline">¶</a></h1>
<div class="section" id="simple-server">
<h2>Simple server<a class="headerlink" href="#simple-server" title="Permalink to this headline">¶</a></h2>
<p>The following code shows an example of a simple SSH server which listens
for connections on port 8022, does password authentication, and prints
a message when users authenticate successfully and start a shell.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="c"># To run this program, the file ``ssh_host_key`` must exist with an SSH</span>
<span class="c"># private key in it to use as a server host key. An SSH host certificate</span>
<span class="c"># can optionally be provided in the file ``ssh_host_key-cert.pub``.</span>
<span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">crypt</span><span class="o">,</span> <span class="nn">sys</span>
<span class="n">passwords</span> <span class="o">=</span> <span class="p">{</span><span class="s">'guest'</span><span class="p">:</span> <span class="s">''</span><span class="p">,</span> <span class="c"># guest account with no password</span>
<span class="s">'user123'</span><span class="p">:</span> <span class="s">'qV2iEadIGV2rw'</span> <span class="c"># password of 'secretpw'</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">MySSHServerSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServerSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">shell_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">session_started</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Welcome to my SSH server, </span><span class="si">%s</span><span class="s">!</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s">'username'</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH connection received from </span><span class="si">%s</span><span class="s">.'</span> <span class="o">%</span>
<span class="n">conn</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s">'peername'</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">connection_lost</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc</span><span class="p">):</span>
<span class="k">if</span> <span class="n">exc</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH connection error: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">'SSH connection closed.'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">begin_auth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
<span class="c"># If the user's password is the empty string, no auth is required</span>
<span class="k">return</span> <span class="n">passwords</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="p">)</span> <span class="o">!=</span> <span class="s">''</span>
<span class="k">def</span> <span class="nf">password_auth_supported</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">validate_password</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">):</span>
<span class="n">pw</span> <span class="o">=</span> <span class="n">passwords</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="s">'*'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">crypt</span><span class="o">.</span><span class="n">crypt</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">pw</span><span class="p">)</span> <span class="o">==</span> <span class="n">pw</span>
<span class="k">def</span> <span class="nf">session_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">MySSHServerSession</span><span class="p">()</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">])</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>To authenticate with SSH client keys or certificates, the server would
look something like the following. Client and certificate authority
keys for each user need to be placed in a file in authorized_keys format
named based on the username in a directory called <code class="docutils literal"><span class="pre">authorized_keys</span></code>.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHServerSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServerSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">shell_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">session_started</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Welcome to my SSH server, </span><span class="si">%s</span><span class="s">!</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s">'username'</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_conn</span> <span class="o">=</span> <span class="n">conn</span>
<span class="k">def</span> <span class="nf">begin_auth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_conn</span><span class="o">.</span><span class="n">set_authorized_keys</span><span class="p">(</span><span class="s">'authorized_keys/</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="n">username</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">session_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">MySSHServerSession</span><span class="p">()</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">])</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>It is also possible to use a single authorized_keys file for all users.
This is common when using certificates, as AsyncSSH can automatically
enforce that the certificates presented have a principal in them which
matches the username. This would look something like the following.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHServerSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServerSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">shell_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">session_started</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Welcome to my SSH server, </span><span class="si">%s</span><span class="s">!</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s">'username'</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">session_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">MySSHServerSession</span><span class="p">()</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="simple-server-with-input">
<h2>Simple server with input<a class="headerlink" href="#simple-server-with-input" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates reading input in a server session.
It will sum a column of numbers, displaying the total and closing the
connection when it receives EOF. Note that this is not an interactive
application, so no echoing of user input is provided. You’ll need to
have the SSH client read from a file or pipe rather than the terminal
or tell it not to allocate a pty for this to work right.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHServerSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServerSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_input</span> <span class="o">=</span> <span class="s">''</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_total</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">shell_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_input</span> <span class="o">+=</span> <span class="n">data</span>
<span class="n">lines</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_input</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_total</span> <span class="o">+=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write_stderr</span><span class="p">(</span><span class="s">'Invalid number: </span><span class="si">%s</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_input</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">eof_received</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Total = </span><span class="si">%s</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">_total</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">session_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">MySSHServerSession</span><span class="p">()</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>Here’s an example of this server written using the streams API. In this
case, <a class="reference internal" href="api.html#asyncssh.listen" title="asyncssh.listen"><code class="xref py py-func docutils literal"><span class="pre">listen()</span></code></a> is used in place of <a class="reference internal" href="api.html#asyncssh.create_server" title="asyncssh.create_server"><code class="xref py py-func docutils literal"><span class="pre">create_server()</span></code></a> since a
custom subclass of <a class="reference internal" href="api.html#asyncssh.SSHServer" title="asyncssh.SSHServer"><code class="xref py py-class docutils literal"><span class="pre">SSHServer</span></code></a> is not required. The handler
coroutine to call to handle new sessions is specified using the
<code class="docutils literal"><span class="pre">session_factory</span></code> argument. When a new session is requested, the
handler coroutine is called with AsyncSSH stream objects representing
stdin, stdout, and stderr that it can use to perform I/O.</p>
<p>This example also shows how to catch exceptions thrown when break messages,
signals, or terminal size changes are received.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">handle_connection</span><span class="p">(</span><span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span><span class="p">):</span>
<span class="n">total</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">stdin</span><span class="o">.</span><span class="n">at_eof</span><span class="p">():</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">stdin</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="k">except</span> <span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">BreakReceived</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">SignalReceived</span><span class="p">):</span>
<span class="c"># Exit if the client sends a break or signal</span>
<span class="k">break</span>
<span class="k">except</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">TerminalSizeChanged</span><span class="p">:</span>
<span class="c"># Ignore terminal size changes</span>
<span class="k">continue</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">if</span> <span class="n">line</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">total</span> <span class="o">+=</span> <span class="nb">int</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Invalid number: </span><span class="si">%s</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span> <span class="n">line</span><span class="p">)</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Total = </span><span class="si">%s</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span> <span class="n">total</span><span class="p">)</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">except</span> <span class="n">BrokenPipeError</span><span class="p">:</span>
<span class="c"># The channel is already closed here, so we can't send an exit status</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span> <span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">,</span>
<span class="n">session_factory</span><span class="o">=</span><span class="n">handle_connection</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="getting-environment-variables">
<h2>Getting environment variables<a class="headerlink" href="#getting-environment-variables" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates reading environment variables set
by the client. It will show all of the variables set by the client,
or return an error if none are set. Note that SSH clients may restrict
which environment variables (if any) are sent by default, so you may
need to set options in the client to get it to do so.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">handle_connection</span><span class="p">(</span><span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span><span class="p">):</span>
<span class="n">env</span> <span class="o">=</span> <span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">get_environment</span><span class="p">()</span>
<span class="k">if</span> <span class="n">env</span><span class="p">:</span>
<span class="n">keywidth</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">len</span><span class="p">,</span> <span class="n">env</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span><span class="o">+</span><span class="mi">1</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Environment:</span><span class="se">\r\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">env</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">' </span><span class="si">%-*s</span><span class="s"> </span><span class="si">%s</span><span class="se">\r\n</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">keywidth</span><span class="p">,</span> <span class="n">key</span><span class="o">+</span><span class="s">':'</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'No environment sent.</span><span class="se">\r\n</span><span class="s">'</span><span class="p">)</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span> <span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">,</span>
<span class="n">session_factory</span><span class="o">=</span><span class="n">handle_connection</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="getting-terminal-information">
<h2>Getting terminal information<a class="headerlink" href="#getting-terminal-information" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates reading the client’s terminal
type and window size, and handling window size changes during a
session.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">handle_connection</span><span class="p">(</span><span class="n">stdin</span><span class="p">,</span> <span class="n">stdout</span><span class="p">,</span> <span class="n">stderr</span><span class="p">):</span>
<span class="n">term_type</span> <span class="o">=</span> <span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">get_terminal_type</span><span class="p">()</span>
<span class="n">width</span><span class="p">,</span> <span class="n">height</span><span class="p">,</span> <span class="n">pixwidth</span><span class="p">,</span> <span class="n">pixheight</span> <span class="o">=</span> <span class="n">stdout</span><span class="o">.</span><span class="n">channel</span><span class="o">.</span><span class="n">get_terminal_size</span><span class="p">()</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'Terminal type: </span><span class="si">%s</span><span class="s">, size: </span><span class="si">%s</span><span class="s">x</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">term_type</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">height</span><span class="p">))</span>
<span class="k">if</span> <span class="n">pixwidth</span> <span class="ow">and</span> <span class="n">pixheight</span><span class="p">:</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">' (</span><span class="si">%s</span><span class="s">x</span><span class="si">%s</span><span class="s"> pixels)'</span> <span class="o">%</span> <span class="p">(</span><span class="n">pixwidth</span><span class="p">,</span> <span class="n">pixheight</span><span class="p">))</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'</span><span class="se">\r\n</span><span class="s">Try resizing your window!</span><span class="se">\r\n</span><span class="s">'</span><span class="p">)</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">stdin</span><span class="o">.</span><span class="n">at_eof</span><span class="p">():</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">stdin</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">except</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">TerminalSizeChanged</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'New window size: </span><span class="si">%s</span><span class="s">x</span><span class="si">%s</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">exc</span><span class="o">.</span><span class="n">width</span><span class="p">,</span> <span class="n">exc</span><span class="o">.</span><span class="n">height</span><span class="p">))</span>
<span class="k">if</span> <span class="n">exc</span><span class="o">.</span><span class="n">pixwidth</span> <span class="ow">and</span> <span class="n">exc</span><span class="o">.</span><span class="n">pixheight</span><span class="p">:</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">' (</span><span class="si">%s</span><span class="s">x</span><span class="si">%s</span><span class="s"> pixels)'</span> <span class="o">%</span> <span class="p">(</span><span class="n">exc</span><span class="o">.</span><span class="n">pixwidth</span><span class="p">,</span> <span class="n">exc</span><span class="o">.</span><span class="n">pixheight</span><span class="p">))</span>
<span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'</span><span class="se">\r\n</span><span class="s">'</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span> <span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">,</span>
<span class="n">session_factory</span><span class="o">=</span><span class="n">handle_connection</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="id12">
<h2>Port forwarding<a class="headerlink" href="#id12" title="Permalink to this headline">¶</a></h2>
<p>The following example demonstrates a server accepting port forwarding
requests from clients, but only when they are destined to port 80. When
such a connection is received, a connection is attempted to the requested
host and port and data is bidirectionally forwarded over SSH from the
client to this destination. Requests by the client to connect to any
other port are rejected.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dest_host</span><span class="p">,</span> <span class="n">dest_port</span><span class="p">,</span> <span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">):</span>
<span class="k">if</span> <span class="n">dest_port</span> <span class="o">==</span> <span class="mi">80</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">ChannelOpenError</span><span class="p">(</span>
<span class="n">asyncssh</span><span class="o">.</span><span class="n">OPEN_ADMINISTRATIVELY_PROHIBITED</span><span class="p">,</span>
<span class="s">'Only connections to port 80 are allowed'</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH server failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>The server can also support forwarding inbound TCP connections back to
the client. The following example demonstrates a server which will accept
requests like this from clients, but only to listen on port 8080. When
such a connection is received, the client is notified and data is
bidirectionally forwarded from the incoming connection over SSH to the
client.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">server_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">listen_host</span><span class="p">,</span> <span class="n">listen_port</span><span class="p">):</span>
<span class="k">return</span> <span class="n">listen_port</span> <span class="o">==</span> <span class="mi">8080</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH server failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="id13">
<h2>Direct TCP connections<a class="headerlink" href="#id13" title="Permalink to this headline">¶</a></h2>
<p>The server can also accept direct TCP connection requests from the client
and process the data on them itself. The following example demonstrates a
server which accepts requests to port 7 (the “echo” port) for any host and
echoes the data itself rather than forwarding the connection:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySSHTCPSession</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHTCPSession</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_made</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chan</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span> <span class="o">=</span> <span class="n">chan</span>
<span class="k">def</span> <span class="nf">data_received</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">datatype</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_chan</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dest_host</span><span class="p">,</span> <span class="n">dest_port</span><span class="p">,</span> <span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">):</span>
<span class="k">if</span> <span class="n">dest_port</span> <span class="o">==</span> <span class="mi">7</span><span class="p">:</span>
<span class="k">return</span> <span class="n">MySSHTCPSession</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">ChannelOpenError</span><span class="p">(</span>
<span class="n">asyncssh</span><span class="o">.</span><span class="n">OPEN_ADMINISTRATIVELY_PROHIBITED</span><span class="p">,</span>
<span class="s">'Only echo connections allowed'</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH server failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>Here’s an example of this server written using the streams API. In this
case, <a class="reference internal" href="api.html#asyncssh.SSHServer.connection_requested" title="asyncssh.SSHServer.connection_requested"><code class="xref py py-meth docutils literal"><span class="pre">connection_requested()</span></code></a>
returns a handler coroutine instead of a session object. When a new
direct TCP connection is opened, the handler coroutine is called with
AsyncSSH stream objects which can be used to perform I/O on the tunneled
connection.</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">handle_connection</span><span class="p">(</span><span class="n">reader</span><span class="p">,</span> <span class="n">writer</span><span class="p">):</span>
<span class="k">while</span> <span class="ow">not</span> <span class="n">reader</span><span class="o">.</span><span class="n">at_eof</span><span class="p">():</span>
<span class="n">data</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">reader</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">8192</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">writer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="k">except</span> <span class="n">BrokenPipeError</span><span class="p">:</span>
<span class="k">break</span>
<span class="n">writer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">class</span> <span class="nc">MySSHServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SSHServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">connection_requested</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dest_host</span><span class="p">,</span> <span class="n">dest_port</span><span class="p">,</span> <span class="n">orig_host</span><span class="p">,</span> <span class="n">orig_port</span><span class="p">):</span>
<span class="k">if</span> <span class="n">dest_port</span> <span class="o">==</span> <span class="mi">7</span><span class="p">:</span>
<span class="k">return</span> <span class="n">handle_connection</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">ChannelOpenError</span><span class="p">(</span>
<span class="n">asyncssh</span><span class="o">.</span><span class="n">OPEN_ADMINISTRATIVELY_PROHIBITED</span><span class="p">,</span>
<span class="s">'Only echo connections allowed'</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">create_server</span><span class="p">(</span><span class="n">MySSHServer</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span>
<span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'SSH server failed: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="sftp-server">
<h2>SFTP server<a class="headerlink" href="#sftp-server" title="Permalink to this headline">¶</a></h2>
<p>The following example shows how to start an SFTP server with default
behavior:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">sys</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span> <span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">,</span>
<span class="n">sftp_factory</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>A subclass of <a class="reference internal" href="api.html#asyncssh.SFTPServer" title="asyncssh.SFTPServer"><code class="xref py py-class docutils literal"><span class="pre">SFTPServer</span></code></a> can be provided as the value of the SFTP
factory to override specific behavior. For example, the following code
remaps path names so that each user gets access to only their own individual
directory under <code class="docutils literal"><span class="pre">/tmp/sftp</span></code>:</p>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">asyncio</span><span class="o">,</span> <span class="nn">asyncssh</span><span class="o">,</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span>
<span class="k">class</span> <span class="nc">MySFTPServer</span><span class="p">(</span><span class="n">asyncssh</span><span class="o">.</span><span class="n">SFTPServer</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conn</span><span class="p">):</span>
<span class="n">root</span> <span class="o">=</span> <span class="s">'/tmp/sftp/'</span> <span class="o">+</span> <span class="n">conn</span><span class="o">.</span><span class="n">get_extra_info</span><span class="p">(</span><span class="s">'username'</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">conn</span><span class="p">,</span> <span class="n">chroot</span><span class="o">=</span><span class="n">root</span><span class="p">)</span>
<span class="nd">@asyncio.coroutine</span>
<span class="k">def</span> <span class="nf">start_server</span><span class="p">():</span>
<span class="k">yield from</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="s">''</span><span class="p">,</span> <span class="mi">8022</span><span class="p">,</span> <span class="n">server_host_keys</span><span class="o">=</span><span class="p">[</span><span class="s">'ssh_host_key'</span><span class="p">],</span>
<span class="n">authorized_client_keys</span><span class="o">=</span><span class="s">'ssh_user_ca'</span><span class="p">,</span>
<span class="n">sftp_factory</span><span class="o">=</span><span class="n">MySFTPServer</span><span class="p">)</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">start_server</span><span class="p">())</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="n">asyncssh</span><span class="o">.</span><span class="n">Error</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="s">'Error starting server: '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">exc</span><span class="p">))</span>
<span class="n">loop</span><span class="o">.</span><span class="n">run_forever</span><span class="p">()</span>
</pre></div>
</div>
</div></blockquote>
<p>More complex path remapping can be performed by implementing the
<a class="reference internal" href="api.html#asyncssh.SFTPServer.map_path" title="asyncssh.SFTPServer.map_path"><code class="xref py py-meth docutils literal"><span class="pre">map_path</span></code></a> and
<a class="reference internal" href="api.html#asyncssh.SFTPServer.reverse_map_path" title="asyncssh.SFTPServer.reverse_map_path"><code class="xref py py-meth docutils literal"><span class="pre">reverse_map_path</span></code></a> methods. Individual
SFTP actions can also be overridden as needed. See the <a class="reference internal" href="api.html#asyncssh.SFTPServer" title="asyncssh.SFTPServer"><code class="xref py py-class docutils literal"><span class="pre">SFTPServer</span></code></a>
documentation for the full list of methods to override.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><font color=#fff>
<font size=6>AsyncSSH</font><br>
Version 1.3
</font>
<p>
<h3><a href="#">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">AsyncSSH: Asynchronous SSH for Python</a><ul>
<li><a class="reference internal" href="#features">Features</a></li>
<li><a class="reference internal" href="#license">License</a></li>
<li><a class="reference internal" href="#prerequisites">Prerequisites</a></li>
<li><a class="reference internal" href="#installation">Installation</a><ul>
<li><a class="reference internal" href="#optional-extras">Optional Extras</a></li>
</ul>
</li>
<li><a class="reference internal" href="#mailing-lists">Mailing Lists</a></li>
</ul>
</li>
<li><a class="reference internal" href="#client-examples">Client Examples</a><ul>
<li><a class="reference internal" href="#simple-client">Simple client</a></li>
<li><a class="reference internal" href="#handling-of-stderr">Handling of stderr</a></li>
<li><a class="reference internal" href="#simple-client-with-input">Simple client with input</a></li>
<li><a class="reference internal" href="#checking-exit-status">Checking exit status</a></li>
<li><a class="reference internal" href="#setting-environment-variables">Setting environment variables</a></li>
<li><a class="reference internal" href="#setting-terminal-information">Setting terminal information</a></li>
<li><a class="reference internal" href="#port-forwarding">Port forwarding</a></li>
<li><a class="reference internal" href="#direct-tcp-connections">Direct TCP connections</a></li>
<li><a class="reference internal" href="#forwarded-tcp-connections">Forwarded TCP connections</a></li>
<li><a class="reference internal" href="#sftp-client">SFTP client</a></li>
</ul>
</li>
<li><a class="reference internal" href="#server-examples">Server Examples</a><ul>
<li><a class="reference internal" href="#simple-server">Simple server</a></li>
<li><a class="reference internal" href="#simple-server-with-input">Simple server with input</a></li>
<li><a class="reference internal" href="#getting-environment-variables">Getting environment variables</a></li>
<li><a class="reference internal" href="#getting-terminal-information">Getting terminal information</a></li>
<li><a class="reference internal" href="#id12">Port forwarding</a></li>
<li><a class="reference internal" href="#id13">Direct TCP connections</a></li>
<li><a class="reference internal" href="#sftp-server">SFTP server</a></li>
</ul>
</li>
</ul>
<p>
<h3><a href="changes.html">Change Log</a></h3>
<p>
<h3><a href="contributing.html">Contributing</a></h3>
<p>
<h3><a href="api.html">API Documentation</a></h3>
<p>
<h3><a href="https://pypi.python.org/pypi/asyncssh/">Source on PyPI</a></h3>
<p>
<h3><a href="https://github.com/ronf/asyncssh">Source on GitHub</a></h3>
<p>
<h3><a href="https://github.com/ronf/asyncssh/issues">Issue Tracker</a></h3>
<p>
<h3><a href="search.html">Search</a></h3>
</font>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
© Copyright 2013-2015, Ron Frederick.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
</div>
</body>
</html>
|